簡體   English   中英

ggplot2中具有兩個Y軸的Multiplot刻面線圖-添加點並更改線色

[英]Multiplot facet line plot in ggplot2 with two Y axes - adding points and changing line colours

我正在為Inc和Ratio的兩個y軸准備多圖。

我用不同的顏色區分每個圖來代表三個區域。 我不能成功完成的三個項目是:

  • 我現在確實有每條情節中具有相同顏色的兩條線。 我想將其中之一更改為虛線(比率1)。

  • 我需要將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

到目前為止的結果圖

你可以嘗試一個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")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM