r - 如何向 ggplot 图例添加新的(自定义)变量

标签 r ggplot2 legend legend-properties

我运行了许多模型,每个模型有两个估计参数,有五个组和两种治疗方法。我试图在一个大面板图中绘制这些估计值的置信区间。因为,我已经模拟了这些数据集,所以我希望能够包含一条虚线,表示我在练习开始时设置的参数的“真实值”以供引用,这样我们就可以看到模型估计值包括真实值。我可以很好地做到这一点,但我想在图例中添加另一行,显示“黑色虚线”=真实值。

这是代码示例。第一组代码有效,并且不包括图例中的黑色虚线。

group = c("group1", "group2", "group3", "group4", "group5")
treatment = c("treatment1", "treatment2")
estimates = c("estim1", "estim2")
parameters = c("param1", "param2")
means = c(0, 0, 5, 0, -5, 0, 0, 7, -5, 10, -5, 0, 0, 0, 0, 0, -5, 0, 0, 10)
UL = c(.5, .5, 5.5, .5, -4.5, 0.5, 0.5, 7.5, -4.5, 10.5, -4.5, .5, .5, .5, .5, .5, -4.5, .5, .5, 10.5)
LL = c(-.5, -.5, 4.5, -.5, -5.5, -.5, -.5, 6.5, -4.5, 9.5, -4.5, -.5, -.5, -.5, -.5, -.5, -4.5, -.5, -.5, 9.5)
values = c(.2, -.2, 5.2, -.3, -4.7, -.1, -.2, 6.9, -5.3, 10.1, -4.4, 0.1, 0.2, 0.3, 0.1, -0.1, -4.9, -.2, -.2, 9.9)

df = data.frame(
  group = rep(rep(group, each = 2), 2),
  treatment = rep(treatment, each = 10),
  estimates = rep(estimates, 10),
  LL = LL,
  means = means,
  UL = UL,
  parameters = rep(parameters, 10),
  values = values
)

ggplot(data = df, aes(x = as.factor(estimates), y = means, color = estimates))+
  geom_point()+
  geom_errorbar(aes(ymin = LL, ymax = UL), width=.1, position = position_dodge(0.1))+
  geom_segment(x = rep(c(.6, 1.6), 10), xend = rep(c(1.4, 2.4), 10), 
               y =  values, yend = values, col = "black",
               linetype = 3)+
  scale_x_discrete(labels = c(expression(beta[1]), expression(beta[2])))+
  xlab("Beta coefficient type")+ylab("Confidence Interval of Estimate")+
  ggtitle("Coefficient Estimates")+
  facet_grid(row = vars(treatment), col = vars(group))+
  scale_color_manual(name = "Symbols",
                     values = c("estim1" = "#F8766D", "estim2" = "#00BFC4"),
                     labels = c(expression(beta[1]),
                                expression(beta[2])))
  scale_shape_manual(values = c("b1" = 16,
                                "b2" = 16)+
  scale_linetype_manual(values = c("b1" = 1,
                                   "b2" = 1))

第二组代码不起作用,但这是我最好的尝试,我应该做什么来尝试获得图例中的黑色虚线。

ggplot(data = df, aes(x = as.factor(estimates), y = means, color = estimates))+
  geom_point()+
  geom_errorbar(aes(ymin = LL, ymax = UL), width=.1, position = position_dodge(0.1))+
  geom_segment(x = rep(c(.6, 1.6), 10), xend = rep(c(1.4, 2.4), 10), 
               y =  values, yend = values, col = "black",
               linetype = 3)+
  scale_x_discrete(labels = c(expression(beta[1]), expression(beta[2])))+
  xlab("Beta coefficient type")+ylab("Confidence Interval of Estimate")+
  ggtitle("Coefficient Estimates")+
  facet_grid(row = vars(treatment), col = vars(group))+
  scale_color_manual(name = "Symbols",
                     values = c("estim1" = "#F8766D", "estim2" = "#00BFC4"),
                                #"" = "#00000"),
                     labels = c(expression(beta[1]),
                                expression(beta[2])))#,
                                #"True Value"))#+
  scale_shape_manual(values = c("b1" = 16,
                                "b2" = 16,
                                "" = 0))+
  scale_linetype_manual(values = c("b1" = 1,
                                   "b2" = 1,
                                   "b3" = 3))

GGplot with new variable added to legend

我还认为也许我可以尝试重新调整 df$estimates 列的级别,以包含三个级别(现有)“estim1”、“estim2”和一个没有观察结果的虚拟“真实值”级别,但我'我担心这只会在 x 轴子标签上的 12 个图上添加一个空的“真实值”刻度。

感谢您的帮助。

最佳答案

geom_segment 的线型映射到 aes 内名为“True value”的字符串,然后添加 scale_linetype_manual 调用。这将创建一个单独的图例条目,该条目与您的段的外观相匹配并具有正确的标签。

ggplot(data = df, aes(x = as.factor(estimates), y = means, color = estimates)) +
  geom_point() +
  geom_errorbar(aes(ymin = LL, ymax = UL), width=.1, 
                position = position_dodge(0.1)) +
  geom_segment(x = rep(c(.6, 1.6), 10), xend = rep(c(1.4, 2.4), 10), 
               y =  values, yend = values, col = "black",
               aes(linetype = "True value")) +
  scale_x_discrete(labels = c(expression(beta[1]), expression(beta[2]))) +
  xlab("Beta coefficient type")+ylab("Confidence Interval of Estimate") +
  ggtitle("Coefficient Estimates") +
  facet_grid(row = vars(treatment), col = vars(group)) +
  scale_color_manual(name = "Symbols",
                     values = c("estim1" = "#F8766D", "estim2" = "#00BFC4"),
                     labels = c(expression(beta[1]),
                                expression(beta[2]))) +
  scale_linetype_manual(values = 3, name = NULL)

enter image description here

关于r - 如何向 ggplot 图例添加新的(自定义)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73179099/

相关文章:

r - 带有两个数据框的ggplot条形图

R-ggplot2 "error: unexpected symbol in:"

r - dplyr:如何在函数内使用 group_by ?

r - 在ggplot2中的不同等高线图中保持相同的中断

r - 如何从 ggplot2 中的预定义颜色集中选择颜色

python - 添加输入变量以在 Python 中绘制标题/图例

r - 将箱线图的 geom_segment 和中位数添加到图例中

python - Matplotlib 图例,跨列而不是向下添加项目

r - 如何将列数据变成列名?

p#q#c# 的 R 正则表达式