【问题标题】:Moving error bars in a line graph with three factors在具有三个因素的折线图中移动误差线
【发布时间】:2020-01-25 06:14:01
【问题描述】:

我正在尝试将具有三个因素的折线图中的误差线设为黑色....

structure(list(pot = c(41L, 42L, 43L, 44L, 61L, 62L, 63L, 64L, 
45L, 46L, 47L, 48L, 65L, 66L, 67L, 68L, 49L, 50L, 51L, 52L, 69L, 
70L, 71L, 72L, 53L, 54L, 55L, 56L, 73L, 74L, 75L, 76L, 57L, 58L, 
59L, 60L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 101L, 102L, 
103L, 104L, 85L, 86L, 87L, 88L, 105L, 106L, 107L, 108L, 89L, 
90L, 91L, 92L, 109L, 110L, 111L, 112L, 93L, 94L, 95L, 96L, 113L, 
114L, 115L, 116L, 97L, 98L, 99L, 100L, 117L, 118L, 119L, 120L
), rep = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), cultivar = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Dinninup", "Riverina", 
"Seaton Park", "Yarloop"), class = "factor"), Waterlogging = structure(c(2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Non-waterlogged", 
"Waterlogged"), class = "factor"), P = c(12.1, 12.1, 12.1, 12.1, 
12.1, 12.1, 12.1, 12.1, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 
15.17, 15.17, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 
18.24, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 
48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 12.1, 
12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 15.17, 15.17, 15.17, 
15.17, 15.17, 15.17, 15.17, 15.17, 18.24, 18.24, 18.24, 18.24, 
18.24, 18.24, 18.24, 18.24, 24.39, 24.39, 24.39, 24.39, 24.39, 
24.39, 24.39, 24.39, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 
48.35, 48.35), form = c(2.81, 2.64, 2.59, 3.28, 3.18, 2.57, 2.9, 
3, 2.38, 2.72, 2.58, 2.73, 3.06, 3.01, 3.01, 2.77, 2.95, 2.36, 
2.91, 2.38, 3.33, 3.19, 3.17, 3.16, 3.16, 3.2, 2.58, 3.71, 3.11, 
2.7, 2.92, 1.93, 2.95, 2.57, 2.68, 2.48, 3.34, 2.75, 2.52, 1.88, 
1.19, 0.57, 0.64, 0.66, 1.13, 1.28, 0.85, 0.96, 1.34, 2.14, 0.63, 
1.27, 1.13, 0.64, 1.21, 1.95, 1.11, 0.91, 0.75, 0.63, 1.06, 1.07, 
1.05, 0.8, 1.41, 1.13, 0.75, 0.89, 1.98, 1.27, 1.01, 1, 1.16, 
0.64, 0.64, 1.02, 1.03, 1.13, 0.79, 0.6)), row.names = 41:120, class = "data.frame")
library(Rmisc)
library(ggplot2)

tglf3 <- summarySE(yar, measurevar="form", 
groupvars=c("P","cultivar","Waterlogging"),na.rm=TRUE)
pd <- position_dodge(0.5)

ggplot(tglf3,aes(x=P, y=form, colour=Waterlogging, shape=cultivar))+ 
  geom_errorbar(aes(ymin=form-se, ymax=form+se), width=.6,position=pd) +
  geom_point(size=3.5,position=pd) +
  geom_line(position=pd) +
  theme_bw() +
  theme(legend.position = 'top', legend.direction="vertical") 

当我尝试更改颜色时,会发生这种情况...

ggplot(tglf3,aes(x=P, y=form, colour=Waterlogging, shape=cultivar)) + 
  geom_errorbar(aes(ymin=form-se, ymax=form+se), colour="black", width=.6,position=pd) +
  geom_point(size=3.5,position=pd) +
  geom_line(position=pd) +
  theme_bw() +
  theme(legend.position = 'top', legend.direction="vertical")

三个因素是涝渍、品种和磷。我尝试添加“group=cultivar”,这通常在只有两个因素时有效。

ggplot(tglf3,aes(x=P, y=form, colour=Waterlogging, shape=cultivar,group=cultivar)) + ...

【问题讨论】:

  • 在您的示例中,position = pd 是什么?另外,se 是什么?两者都没有在您的代码中定义。
  • 你想要不同的颜色吗?在两列上?
  • 在黑白示例所示的图例中,我们可以看到 Riverina 标签旁边有两个符号,这两个符号实际绘制在图表中。符号按行排列,并在行首标有因子水平的名称。它不是最清晰的显示,但是,它比 ggplot 产生的要好。它比 ggplot 的图例要好,因为符号与绘制的符号相同。
  • 我编辑了我的答案,看起来和你期望的一样吗?

标签: r ggplot2 linegraph errorbar


【解决方案1】:

使用以下代码,您可以将colorshape 分组在图例中,并为每个类别设置一个独特的图例形状,如您的示例所示。

要将您的图例分为两列,您可以使用guides

ggplot(DF2, aes(x = P, y = form, 
                 shape = interaction(cultivar, Waterlogging), 
                 color = interaction(cultivar, Waterlogging),
                 group = interaction(cultivar, Waterlogging)))+
    geom_errorbar(aes(ymin=form-se, ymax=form+se),color = "black", width=.6, 
                  position = pd)+
    geom_point(size = 3.5)+
    geom_line()+
    theme_bw() +
    theme(legend.position = 'top', 
          legend.direction="vertical")+
    guides(color=guide_legend(ncol=2, title = "Legend"), 
           shape = guide_legend(ncol =2, title = "Legend"))

它回答了你的问题吗?

【讨论】:

  • position = pd 在那里,因为我希望错误栏“躲避”,这样它们就不会重叠。所以我试图实现顶部的第一个情节,但误差线应该是黑色的
  • 我需要图例中的符号与图中显示的符号相同。
  • 您是否希望将cultivarwaterlogging 放入同一个图例中?有 4 个标签?
  • 我在帖子中添加了一个示例图例。我需要图例图标与图中显示的图标相同。这是通常的标准,但 ggplot 不会用上图生成它
  • 不要将其添加到我的答案中,而是编辑您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
相关资源
最近更新 更多