如何单独绘制geom_point加上geom_point和position_dodge
我正在努力思考如何在估计值旁边绘制真实值,这些值存在于 real_values
向量中。我的问题是,估计值有一个范围(通过 geom_errorbar ),对于实际值,我只想在 10 个点的左侧以黑色绘制点估计。
这是我尝试过的示例:
est_values = rnorm(20)
real_values = rnorm(10)
dat_ex = data.frame(
xvalues = 1:10,
values = est_values,
method = c(rep("A",10),rep("B",10)),
ic_0.025 = c(est_values - rnorm(20,1,0.1)),
ic_0.975 = c(est_values + rnorm(20,1,0.1)))
ggplot(dat_ex) +
#geom_point(aes(x = 1:10, y= real_values), size = 2) +
geom_point(aes(x = xvalues, y= values, group = method, colour = method), position=position_dodge(.9), size = 3) +
geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin = ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2)
I struggling on how I can plot my real values, present in the real_values
vector, next to the estimates values. My problem here is that the estimates values have a range (via the geom_errorbar
), and for the real values I would like to plot just the point, in black, on the left side of each of the 10 estimates.
Here's an example of what I tried:
est_values = rnorm(20)
real_values = rnorm(10)
dat_ex = data.frame(
xvalues = 1:10,
values = est_values,
method = c(rep("A",10),rep("B",10)),
ic_0.025 = c(est_values - rnorm(20,1,0.1)),
ic_0.975 = c(est_values + rnorm(20,1,0.1)))
ggplot(dat_ex) +
#geom_point(aes(x = 1:10, y= real_values), size = 2) +
geom_point(aes(x = xvalues, y= values, group = method, colour = method), position=position_dodge(.9), size = 3) +
geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin = ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ggplot 通常最适合数据框中的数据。因此,我们将您的
real_values
放入数据框中,并将它们绘制在单独的图层中,然后根据要求将它们“微移”到左侧:更好的方法可能是把它们全部放在在同一个数据框中。这可以简化代码并自动将它们放入图例中。
ggplot
generally works best with data in data frames. So we put yourreal_values
in a data frame and plot them in a separate layer, and "nudge" them to the left, as requested:A nicer method might be to put them all in the same data frame. This can simplify the code and will automatically put them in the legend.
我会将
real_values
添加到您的数据中,作为另一个级别的method
,因此它们将与“A”和“B”一起被躲避(并包含在图例中):< img src="https://i.sstatic.net/UTHB6.png" alt="">
I would add
real_values
to your data as another level ofmethod
, so they will be dodged along with "A" and "B" (and included in the legend):