【问题标题】:Multiplot facet line plot in ggplot2 with two Y axes - adding points and changing line coloursggplot2中带有两个Y轴的多图面线图-添加点并更改线条颜色
【发布时间】:2019-01-02 08:43:00
【问题描述】:

我正在为 Inc 和 Ratio 准备两个 y 轴的多图。

我用不同的颜色区分每个图来代表三个区域。 有三项我没有成功:

  • 我现在有,每个图中有两条颜色相同的线。我想将其中一个更改为虚线(比例一)。

  • 我需要将 SE 条添加到 Inc 行(来自 Inc 列)

  • 我想添加 geom_points() 以便在连接线的节点处也有点,仅出于美观原因。

据我所知:

df <- data.frame(c(2009,2009,2009,2009,2010,2010,2010,2010,2011,2011,2011,2011,
                     2012,2012,2012,2012,2013,2013,2013,2013),
                 c("N","S","W","W","N","S","W","W","N","S","W","W","N","S","W","W",
                   "N","S","W","W"),
                 c("Luo","Aka","Opo","Mya","Luo","Aka","Opo","Mya",
                   "Luo","Aka","Opo","Mya","Luo","Aka","Opo","Mya",
                   "Luo","Aka","Opo","Mya"),
                 runif(20,0,1),runif(20,0,1),
                 runif(20,0,0.1))
colnames(df) <- c("Year","Region","District","Inc","Ratio","Inc_SE")

# Order of drawing in facet
df$District<- factor(df$District,
                            levels = c("Opo",
                                       "Mya",
                                       "Luo",
                                       "Aka"))

p <- ggplot(data=df, aes(x = Year))
p <- p + geom_line(aes(y = Inc)) 
p <- ggplot(df, aes(x = Year, y=df$Inc))
p <- p + geom_line(aes(y = Inc))
p <- ggplot(df, aes(x = Year))
p <- p + geom_line(aes(y = Inc, colour = Region))
p <- p + theme_bw()+
        theme(plot.title = element_text(hjust = 1))+
        theme(legend.position="none")+
        theme(axis.title.x = element_text(face ="bold", colour="black", size=11),
              axis.text.x = element_text(angle=90, vjust=0.5, size=7, family = "serif"),
              axis.title.y = element_text(face = "bold", colour = "black", size=10))

# adding Ratio
p <- p + geom_line(aes(y = Ratio, colour = Region,linetype = "dashed")) # here dashed is not recognised by R
# now adding the secondary axis
p <- p + scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Ratio"))
p <- p + scale_colour_manual(values = c("blue", "red","black"))
p <- p + 
        theme_bw()+
        theme(plot.title = element_text(hjust = 1))+
        theme(legend.position="none")+
        theme(axis.title.x = element_text(face ="bold", colour="black", size=11),
              axis.text.x = element_text(angle=90, vjust=0.5, size=9, family = "serif"),
              axis.title.y = element_text(face = "bold", colour = "black", size=10))
# Breaking down to separate graphs
p_facet = p + facet_wrap(~ df$District, 
                         ncol = 2)
p_facet

【问题讨论】:

    标签: r ggplot2 facet yaxis


    【解决方案1】:

    您可以尝试tidyverse。诀窍是将数据从宽转换为长(这里我使用了gather)。然后您可以轻松地将点、线和Inc_SE 添加为功能区。

    library(tidyverse)
    df %>% 
      gather(k,v, -Year, -Region, -District, -Inc_SE) %>% 
      ggplot(aes(Year, v, group = k, color=Region, linetype=k)) + 
        geom_ribbon(data=. %>% filter( k == "Inc"), 
                    aes(ymin=v-Inc_SE, ymax=v+Inc_SE), 
                    alpha=0.2,color=NA,
                    show.legend = F) +
        geom_line() + 
        geom_point(show.legend = F)+
        scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Ratio"))+
        facet_wrap(~ District) +
        labs(y="Inc") + 
        theme_bw() + 
        theme(legend.position = "bottom")
    

    【讨论】:

    • 还有一个问题:如何更改图例 - 将 k 替换为“变量”等有意义的内容,并将 Inc 替换为“发生率”,将 Ratio 替换为“工厂比率”
    • 你可以试试guides(linetype=guide_legend(title="New Legend Title") ,color=...)。可以在 ggplot 或使用 scale_color_manual(labels = c("North", "South", West) 之前更改 Inc 和 Ratio
    • 如何表示 v 和 k(也就是要绘制的变量)?它没有在任何地方指定。在用“-”标记剩余的列之后,它会将 v 和 k 作为 data.frame 中剩下的唯一列吗?
    • @MIH,您可以在此处阅读?gather,您可以指定任何您想要的名称,包括key = "key", value = "value"kv。我使用缩写,因为它更快;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多