如何单独绘制geom_point加上geom_point和position_dodge

发布于 2025-01-11 01:43:06 字数 948 浏览 0 评论 0原文

我正在努力思考如何在估计值旁边绘制真实值,这些值存在于 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)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2025-01-18 01:43:06

ggplot 通常最适合数据框中的数据。因此,我们将您的 real_values 放入数据框中,并将它们绘制在单独的图层中,然后根据要求将它们“微移”到左侧:

ggplot(dat_ex) +
  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) +
  geom_point(
    data = data.frame(values = real_values, xvalues = dat_ex$xvalues),
    aes(x = xvalues, y = values), 
    position = position_nudge(x = -.4),
    color = "black")

在此处输入图像描述

更好的方法可能是把它们全部放在在同一个数据框中。这可以简化代码并自动将它们放入图例中。

library(dplyr)
dat_ex = data.frame(
  xvalues = 1:10,
  values = real_values,
  method = "real"
) %>%
  bind_rows(dat_ex) %>%
  mutate(method = factor(method, levels = c("real", "A", "B")))

ggplot(dat_ex, aes(x = xvalues, y = values, color = method)) +
  geom_point(position=position_dodge(.9), size = 3) +
  geom_errorbar(aes(ymin =  ic_0.025, ymax = ic_0.975, group = method),
                size = 1.3, position=position_dodge(.9), width = .2) +
  scale_color_manual(values = c("real" = "black", "A" = "orange", "B" = "blue"))

输入图片此处描述

ggplot generally works best with data in data frames. So we put your real_values in a data frame and plot them in a separate layer, and "nudge" them to the left, as requested:

ggplot(dat_ex) +
  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) +
  geom_point(
    data = data.frame(values = real_values, xvalues = dat_ex$xvalues),
    aes(x = xvalues, y = values), 
    position = position_nudge(x = -.4),
    color = "black")

enter image description here

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.

library(dplyr)
dat_ex = data.frame(
  xvalues = 1:10,
  values = real_values,
  method = "real"
) %>%
  bind_rows(dat_ex) %>%
  mutate(method = factor(method, levels = c("real", "A", "B")))

ggplot(dat_ex, aes(x = xvalues, y = values, color = method)) +
  geom_point(position=position_dodge(.9), size = 3) +
  geom_errorbar(aes(ymin =  ic_0.025, ymax = ic_0.975, group = method),
                size = 1.3, position=position_dodge(.9), width = .2) +
  scale_color_manual(values = c("real" = "black", "A" = "orange", "B" = "blue"))

enter image description here

深白境迁sunset 2025-01-18 01:43:06

我会将 real_values 添加到您的数据中,作为另一个级别的 method,因此它们将与“A”和“B”一起被躲避(并包含在图例中):

library(ggplot2)

dat_ex <- rbind(
  dat_ex,
  data.frame(
    xvalues = 1:10, 
    values = real_values, 
    method = "Real",
    ic_0.025 = NA_real_,
    ic_0.975 = NA_real_
  )
)

# arrange so "Real" is on the left
dat_ex$method <- factor(dat_ex$method, levels = c("Real", "A", "B"))

ggplot(dat_ex) +
  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) +
  scale_colour_manual(values = c("black", "forestgreen", "royalblue"))

< img src="https://i.sstatic.net/UTHB6.png" alt="">

I would add real_values to your data as another level of method, so they will be dodged along with "A" and "B" (and included in the legend):

library(ggplot2)

dat_ex <- rbind(
  dat_ex,
  data.frame(
    xvalues = 1:10, 
    values = real_values, 
    method = "Real",
    ic_0.025 = NA_real_,
    ic_0.975 = NA_real_
  )
)

# arrange so "Real" is on the left
dat_ex$method <- factor(dat_ex$method, levels = c("Real", "A", "B"))

ggplot(dat_ex) +
  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) +
  scale_colour_manual(values = c("black", "forestgreen", "royalblue"))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文