我想像这篇文章一样在彼此之上绘制两个图表在彼此之上绘制两个图表。
\n\n实验数据:我有一个连续变量,在名为 的列表中显示给定日期的风角expt$iso_xs[,8],然后我在 中具有与该角度相对应的风速expt$iso_xs[,2]。
df<-data.frame(expt$iso.xs)\n\nhead(expt$iso.xs)\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]\n 736105.4 16.62729 2.183740 7.234774 0.9791632 4.01 4.20 238.62\n 736105.4 18.96705 2.489668 7.036234 0.9640366 3.82 4.00 243.14\n 736105.5 20.52089 2.687636 10.355394 1.3698454 4.99 5.14 247.02\n 736105.5 19.94449 2.611556 10.306912 1.3655301 4.85 5.12 249.57\n 736105.5 19.43309 2.551787 11.098302 1.4646251 4.83 5.12 243.89\n 736105.5 20.48259 2.689075 11.928011 1.5710530 4.89 5.09 254.23\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n模拟数据:我有一个 data.frame z,其中包含上述角度的子集 (0-90\xc2\xba) 的预测。
head(z,15)\n Tracer angle treatment bigangle\n 71.101 0 S 150\n 71.101 0 S 150\n 71.105 15 S 165\n 71.105 15 S 165\n 71.098 30 S 180\n 71.098 45 S 195\n 71.114 60 S 210\n 71.114 80 S 230\n 71.110 90 S 240\nRun Code Online (Sandbox Code Playgroud)\n\n使用 bigangle 作为因子并使用 Tracer 将其绘制为:
\n\nggplot() +\n geom_boxplot(data=z, aes(y = (3600/Tracer/93.241), x = factor(bigangle)),outlier.shape = NA,outlier.colour = NA)+\n coord_cartesian(ylim=c(0, 1))+\n labs(x = "Angle", y = "Normalised ACh" )+\n scale_x_discrete(labels=seq(0,360,10))+\n theme_classic()\nRun Code Online (Sandbox Code Playgroud)\n\n看起来像这样:
\n\n\n\n我想将箱线图叠加在红点部分的顶部(150\xc2\xba 和 240\xc2\xba 之间),但以下方法不起作用:
\n\nggplot() +\n geom_boxplot(data=z, aes(y = (3600/Tracer/93.241), x = factor(bigangle)),outlier.shape = NA,outlier.colour = NA)+\n geom_point(data=df, aes(y = X2/45, x = X8),color="red")+\n coord_cartesian(ylim=c(0, 1))+\n labs(x = "Angle", y = "Normalised ACh" )+\n scale_x_discrete(labels=seq(0,360,10))+\n theme_classic()\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n任何想法将不胜感激,\n干杯
\n我认为你唯一的问题是尝试为连续数据指定离散的 x 比例。那你需要一个group用于你的箱线图几何。
作为说明性示例:
mt = mtcars
mt$wt_bin = cut(mt$wt, breaks = c(1, 3, 4.5, 6))
ggplot(mt, aes(x = wt, y = mpg)) +
geom_point() +
geom_boxplot(aes(group = wt_bin, x = wt), alpha = 0.4)
Run Code Online (Sandbox Code Playgroud)
正如geom_boxplot帮助中所说:
您还可以使用带有连续 x 的箱线图,只要提供分组变量即可。
cut_width特别有用
帮助中的示例显示了此代码:
ggplot(diamonds, aes(carat, price)) +
geom_boxplot(aes(group = cut_width(carat, 0.25)))
Run Code Online (Sandbox Code Playgroud)
当然,您可以添加一个geom_point图层(尽管diamonds数据中的点太多,无法成为一个漂亮的图)。
对于您的比例,不要使用离散比例,除非轴上有因子。你可能想要scale_x_continuous(breaks = seq(0, 360, 10))。
不同的数据集可以通过参数以通常的方式使用data。继续前面的示例,但对图层使用不同的数据geom_point:
similar_to_mt = data.frame(wt = runif(100, 1, 6), mpg = rnorm(100, 20, 4))
ggplot(mt, aes(x = wt, y = mpg)) +
geom_point(data = similar_to_mt) +
geom_boxplot(data = mt, aes(group = wt_bin, x = wt), alpha = 0.4)
Run Code Online (Sandbox Code Playgroud)