如何同时在一个y轴上绘制三个变量?

huangapple go评论94阅读模式
英文:

How to plot three variables simultenously on a y-axis?

问题

这段代码绘制了"fit.mean"变量在y轴上的图形。如果您想同时在y轴上绘制三个变量:"fit.mean"、"fit.0.025quant"和"fit.0.975quant",是否可行呢?

可以实现这个目标。您可以使用geom_point()函数来绘制这三个变量的散点图。以下是修改后的代码:

plot <- ggplot(df, aes(x = Y)) +
  geom_point(aes(y = fit.mean), color = "blue") +
  geom_point(aes(y = fit.0.025quant), color = "green") +
  geom_point(aes(y = fit.0.975quant), color = "red") +
  xlab("Observed values") +
  ylab("Fitted values")

这将在同一图中绘制三个变量的散点,分别用不同颜色表示。这样,您就可以同时可视化"fit.mean"、"fit.0.025quant"和"fit.0.975quant"这三个变量了。

英文:

Suppose the data is:

structure(list(Y = c(0.428559988737106, 0.429479986429214, 0.430400013923645, 
0.431320011615753, 0.432240009307861, 0.433160006999969, 0.434080004692078, 
0.435000002384186, 0.435920000076294, 0.436839997768402, 0.43775999546051, 
0.438679993152618, 0.439599990844727, 0.440889984369278, 0.442179977893829, 
0.443470001220703, 0.444759994745255, 0.446049988269806, 0.44734001159668, 
0.448630005121231, 0.449919998645783, 0.451209992170334, 0.452499985694885, 
0.453790009021759, 0.45508000254631, 0.456369996070862, 0.457660019397736, 
0.458950012922287, 0.460240006446838, 0.46152999997139, 0.462819993495941, 
0.464110016822815, 0.465400010347366, 0.465400010347366, 0.465400010347366, 
0.465400010347366, 0.465400010347366, 0.465400010347366, 0.465400010347366, 
0.465400010347366, 0.465400010347366, 0.465400010347366, 0.465400010347366, 
0.465400010347366, 0.465400010347366, 0.465400010347366, 0.465400010347366, 
0.465400010347366, 0.465400010347366, 0.465400010347366, 0.465400010347366, 
0.465400010347366, 0.465400010347366, 0.463084220886231, 0.460768431425095, 
0.458452641963959), fit.0.025quant = c(0.422946532356816, 0.423859589987584, 
0.424773170010371, 0.425687180462195, 0.426601620221918, 0.427516460674572, 
0.428431675138512, 0.429347238730514, 0.430263128223136, 0.431179321889862, 
0.432095799339629, 0.433012584268335, 0.433929618889501, 0.435213817782259, 
0.436498222018218, 0.437782832636857, 0.439067562870951, 0.440352415386009, 
0.441637393515729, 0.442922412496949, 0.444207476706236, 0.445492522830037, 
0.446777562490253, 0.44806259832147, 0.44934754422001, 0.450632402490787, 
0.451917174996901, 0.453201774312925, 0.454486200865493, 0.4557704245195, 
0.457054413934522, 0.458338170645985, 0.459621596773654, 0.459625438742148, 
0.459628966835677, 0.459632200875605, 0.459635158902508, 0.459637857298661, 
0.459640310890083, 0.459642533037703, 0.459644535714284, 0.459646336523744, 
0.459647938374518, 0.459649349959959, 0.459650578825532, 0.459651631418086, 
0.459652513126568, 0.459653228310758, 0.459653780320366, 0.459654171509572, 
0.459654403235285, 0.459654473761697, 0.459654384395954, 0.457357597211507, 
0.455060735132515, 0.452763900735961), fit.mean = c(0.428653603470484, 
0.429567214471664, 0.430481317762278, 0.431395821864613, 0.432310726444749, 
0.433226003808808, 0.434141628303595, 0.435057576149119, 0.435973825277488, 
0.436890355173261, 0.437807146718039, 0.438724218939259, 0.43964152075837, 
0.440926264556075, 0.442211197789549, 0.443496323806796, 0.444781558088095, 
0.446066905624984, 0.447352372087361, 0.448637874997017, 0.449923421130719, 
0.45120895227391, 0.452494479792814, 0.45378000880127, 0.455065455700345, 
0.456350825454543, 0.457636122675476, 0.458921262713308, 0.460206248961549, 
0.461491054364285, 0.462775650818992, 0.464060042754092, 0.465344136369851, 
0.465347594951185, 0.46535077222893, 0.465353686025822, 0.465356352598272, 
0.465358786745413, 0.465361001901007, 0.465363010216649, 0.465364822633452, 
0.465366455065129, 0.465367910505214, 0.465369197065295, 0.465370321881327, 
0.465371291165096, 0.465372110249922, 0.465372783627849, 0.465373314980696, 
0.465373707210119, 0.465373962457108, 0.465374080253461, 0.465374063033422, 
0.463075461498896, 0.460776819782887, 0.458478232829962), fit.0.975quant = c(0.434381210872151, 
0.435293976875512, 0.436207305430996, 0.437121098687015, 0.438035350599593, 
0.438950028415768, 0.439865101960617, 0.440780543401138, 0.441696327031761, 
0.442612429074908, 0.443528827501262, 0.444445541791781, 0.445362515454957, 
0.446646650033882, 0.447930999335083, 0.449215563301874, 0.45050025427738, 
0.45178507421198, 0.453070025890139, 0.454355024171962, 0.455640073256932, 
0.456925106403825, 0.458210138137735, 0.459495170697094, 0.460780117757596, 
0.462064981536293, 0.463349763916868, 0.464634377588866, 0.465918823187519, 
0.467203070879784, 0.468487089727734, 0.469770881701595, 0.471054349602363, 
0.47105817885652, 0.471061698562378, 0.471064928325515, 0.471067886073608, 
0.47107058817351, 0.471073049531015, 0.471075283682615, 0.471077302876126, 
0.471079124931948, 0.471080753390393, 0.471082197532489, 0.471083465628186, 
0.471084565001098, 0.471085502089176, 0.471086282498541, 0.471086911053448, 
0.471087391848884, 0.471087728296054, 0.471087921067452, 0.471087974337733, 
0.468791320009923, 0.466494651365661, 0.464198076054834)), row.names = 57:112, class = &quot;data.frame&quot;)

The code to plot it is:

plot &lt;- ggplot(df, 
aes(x = Y, y = fit.mean)) + geom_point() +  xlab(&quot;Observed values&quot;) + ylab(&quot;Fitted values&quot;)

The question is:

The code plots only "fit.mean" variable on a y-axis. What if I want to plot three variables simultenously on a y-axis: "fit.mean", "fit.0.025quant" and "fit.0.975quant". Do you guys think it is possible?

答案1

得分: 2

也许我忽略了一个明显的问题,但实现这个的几种可能方式之一是:

ggplot(df, aes(x = Y, y = fit.mean)) +
  geom_point() +
  xlab("观察值") +
  ylab("拟合值") +
  geom_point(aes(x = Y, y = fit.0.975quant)) +
  geom_point(aes(x = Y, y = fit.0.025quant))
英文:

Maybe I‘m missing an obvious problem, but one of several possible ways to do this would be:

ggplot(df, aes(x = Y, y = fit.mean)) +
geom_point() +
xlab(&quot;Observed values&quot;) +
ylab(&quot;Fitted values&quot;) +
geom_point(aes(x = Y, y = fit.0.975quant)) +
geom_point(aes(x = Y, y = fit.0.025quant))

huangapple
  • 本文由 发表于 2023年8月9日 13:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864768-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定