【问题标题】:showing three factors in line graph在折线图中显示三个因素
【发布时间】:2020-01-25 03:28:32
【问题描述】:

我正在尝试制作包含三个因素的折线图。另外,我想让我的图例标签有两个符号。

这是我试图模仿的已发布示例....

如您所见,每个标签都有两个符号,一个代表一个因子的每个级别。此外,相同的因子在每个级别的同一面板上显示两次,即具有两个不同级别的因子的品种。

我正在尝试一次制作一个面板,稍后我将加入它们并将图例放在顶部,就像上面的示例一样。

这是我尝试的开始......

我在图表上得到了我的观点,尽管我对如何通过每个品种连接线、显示误差线以及使图例类似于上面的示例感到困惑。我希望图例为每个品种有一个标签,但有两个符号,一个是涝渍符号,一个是非涝渍符号

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)

ggplot(tglf3, aes(x=P, y=form)) + 
geom_point(aes(colour = factor(Waterlogging), 
shape=factor(cultivar)),size=3.5,position=position_dodge(1))+
geom_errorbar(aes(ymin=form-se, ymax=form+se),colour="black", width=.1,position=position_dodge(1)) 

【问题讨论】:

    标签: r legend linegraph errorbar


    【解决方案1】:

    这是否接近您正在寻找的东西?

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

    大多数情况下,利用dplyr::group_by可以解决点线配对:

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


    更多格式使用scale_x_manual:

    tglf3 %>% 
      unite(new, cultivar, Waterlogging, sep = ', ') %>% 
      group_by(new) %>% 
      ggplot(aes(x=P, y=form, colour=new, shape=new, linetype=new)) + 
      geom_errorbar(aes(ymin=form-se, ymax=form+se), colour="black", width=.6) +
      geom_line(color="black") + 
      geom_point(size=3.5) +
      scale_colour_manual(name = "Cultivar, Waterlogging",
                          labels = c("Riverina, Non-waterlogged", "Riverina, Waterlogged", "Yarloop, Non-waterlogged", "Yarloop, Waterlogged"),
                          values = c("blue", "red", "blue", "red")) +
      scale_shape_manual(name = "Cultivar, Waterlogging",
                         labels = c("Riverina, Non-waterlogged", "Riverina, Waterlogged", "Yarloop, Non-waterlogged", "Yarloop, Waterlogged"),
                         values = c(19, 19, 17, 17)) +
      scale_linetype_manual(name = "Cultivar, Waterlogging", values=c("longdash", "solid", "longdash", "solid")) +
      theme_classic() +
      theme(legend.position = 'top', legend.direction="vertical") +
      guides(color=guide_legend(ncol=2))
    


    没有%&gt;%

    tglf4 <- unite(data = tglf3, new, cultivar, Waterlogging, sep = ', ')
    tglf5 <- group_by(.data = tglf4, new)
    
    ggplot(tglf5, aes(x=P, y=form, colour=new, shape=new, linetype=new)) + 
    geom_errorbar(aes(ymin=form-se, ymax=form+se), colour="black", width=.6) +
    geom_line(color="black") + 
    geom_point(size=3.5) +
    scale_colour_manual(name = "Cultivar, Waterlogging",
                        labels = c("Riverina, Non-waterlogged", "Riverina, Waterlogged", "Yarloop, Non-waterlogged", "Yarloop, Waterlogged"),
                        values = c("blue", "red", "blue", "red")) +
    scale_shape_manual(name = "Cultivar, Waterlogging",
                       labels = c("Riverina, Non-waterlogged", "Riverina, Waterlogged", "Yarloop, Non-waterlogged", "Yarloop, Waterlogged"),
                       values = c(19, 19, 17, 17)) +
    scale_linetype_manual(name = "Cultivar, Waterlogging", values=c("longdash", "solid", "longdash", "solid")) +
    theme_classic() +
    theme(legend.position = 'top', legend.direction="vertical") +
    guides(color=guide_legend(ncol=2))
    

    使用facet_grid(使用假数据的示例):

    tglf3 %>% 
      mutate(
        form = form * 1.5
      ) %>% 
      bind_rows(tglf3, .id = 'species') %>% # Add fake data to the real data as an example
      mutate(
        species = case_when(species == 1 ~ 'O. sativus',
                            T ~ 'O. compressus')
      ) %>% 
      unite(new, cultivar, Waterlogging, sep = ', ') %>% 
      group_by(new) %>% 
      ggplot(aes(x=P, y=form, colour=new, shape=new, linetype=new)) + 
      geom_errorbar(aes(ymin=form-se, ymax=form+se), colour="black", width=.6) +
      geom_line(color="black") + 
      geom_point(size=3.5) +
      scale_colour_manual(name = "Cultivar, Waterlogging",
                          labels = c("Riverina, Non-waterlogged", "Riverina, Waterlogged", "Yarloop, Non-waterlogged", "Yarloop, Waterlogged"),
                          values = c("blue", "red", "blue", "red")) +
      scale_shape_manual(name = "Cultivar, Waterlogging",
                         labels = c("Riverina, Non-waterlogged", "Riverina, Waterlogged", "Yarloop, Non-waterlogged", "Yarloop, Waterlogged"),
                         values = c(19, 19, 17, 17)) +
      scale_linetype_manual(name = "Cultivar, Waterlogging", values=c("longdash", "solid", "longdash", "solid")) +
      theme_classic() +
      theme(legend.position = 'top', legend.direction="vertical") +
      guides(color=guide_legend(ncol=2)) +
      facet_grid(.~species)
    

    【讨论】:

    • 太棒了。是否可以更改图例,使其具有与我上面发布的示例类似的结构?
    • 每个图例标签的两个符号结构相同
    • ...或者更确切地说,使符号代表图中实际显示的内容。所以圆圈是两种颜色,三角形是两种颜色......
    • 我可能对示例的解释有误,但是可以使用规模手动功能来完成这种事情:ggplot2.tidyverse.org/reference/scale_manual.html。编辑后的答案中有一个示例。此外,您必须(?)事先使用tidyr::unite 来组合这两个变量。
    • 符号“%>%”的含义和作用是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多