“ ggarrange”在组合图中缺少颜色

时间:2018-09-11 21:52:22

标签: r ggplot2

我正在使用ggarrange组合4个图,但是在各个图的所有颜色之中:

See Here

组合图中我只有两种颜色(红色和黑色)

See Here

有没有办法保留组合图中的所有颜色?

以下是绘制各个图的功能:

library(ggplot2)
ward.graph <- function(df, na.rm = TRUE, ...){
    ggplot(df,aes(x = year, y = remained, fill=as.factor(dplyr::desc(v)))) + 
    geom_area(stat="identity", colour="black", width=1.2) +
    labs(x="", y="", title=paste("ward", df$ward)) +
    scale_x_continuous(breaks = seq(2020, 2050, by = 5)) +
    theme(axis.title.y = element_text(angle=0))  +
    theme(axis.text.x=element_text(size=rel(0.6), angle=90)) +
    theme(axis.text.y=element_text(size=rel(0.6))) +
    theme(plot.title =element_text(size=rel(0.6))) +
    theme(plot.subtitle = element_text(hjust =0.1)) +
    guides(fill=guide_legend(title="Vintages",ncol=18))  +
    theme(legend.text=element_text(size=8))+
    theme(legend.title = element_text(hjust = 0.5))  +
    theme(legend.background = element_rect(fill = "white", colour = "black")) +
    theme(legend.position="bottom")+
    theme(legend.position="bottom",
    plot.margin=unit(c(0,0,-0.2,0), "cm"))
  }
vy1 <- subset(vy, vy$ward==1 & vy$type=="APARTMENT")
p1<- ward.graph(vy1)
print(p1)

type0="APARTMENT"
city_name="Calgary"
type_title="Apartments"

循环绘制所有图:

library(gridExtra)
plot_list = list()
for(i in 1:4) {
  vy1 <- subset(vy, vy$ward==i & vy$type==type0)
  plot_list[[i]]<- ward.graph(vy1)
}

在这里,我正在合并情节:

  install.packages("ggpubr")
  library(ggpubr)
  figure<- grid.draw(ggarrange(plot_list[[1]],plot_list[[2]],plot_list[[3]],
          plot_list[[4]],
          common.legend = TRUE, legend = "bottom"))

annotate_figure(figure,
                top = text_grob(paste("Number of",type_title,"in",city_name,sep=" "), 
                                color = "red", face = "bold", size = 10))

这是我的一些数据:

> dput(head(vy1))
    structure(list(year = c(2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021), v = c(1990, 1991, 
1992, 1993, 1994, 1990, 1991, 1992, 1993, 1994, 1990, 1991, 1992, 
1993, 1994, 1990, 1991, 1992, 1993, 1994, 1990, 1991, 1992, 1993, 
1994, 1990, 1991, 1992, 1993, 1994, 1990, 1991, 1992, 1993, 1994, 
1990, 1991, 1992, 1993, 1994, 1990, 1991, 1992, 1993, 1994, 1990, 
1991, 1992, 1993, 1994, 1990, 1991, 1992, 1993, 1994, 1990, 1991, 
1992, 1993, 1994, 1990, 1991, 1992, 1993, 1994, 1990, 1991, 1992, 
1993, 1994, 1990, 1991, 1992, 1993, 1994, 1990, 1991, 1992, 1993, 
1994), ward = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 
4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 
4, 4, 4, 4, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 
4, 4, 4, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 
4, 4), remained = c(4897.50570492114, 0, 0, 0, 0, 3143.11565640186, 
0, 0, 0, 0, 2550.6593359167, 0, 0, 0, 0, 4358.00079250633, 0, 
0, 0, 0, 4894.18607808419, 0, 0, 0, 0, 3140.98519005566, 0, 0, 
0, 0, 2548.9304482556, 0, 0, 0, 0, 4355.04685283624, 0, 0, 0, 
0, 4888.8318412504, 0, 0, 0, 0, 3137.54895401342, 0, 0, 0, 0, 
2546.14191976995, 0, 0, 0, 0, 4350.28243401351, 0, 0, 0, 0, 4880.4658461976, 
0, 0, 0, 0, 3132.1798351974, 0, 0, 0, 0, 2541.78484401111, 0, 
0, 0, 0, 4342.83802960301, 0, 0, 0, 0)), row.names = c(NA, -80L
), class = c("tbl_df", "tbl", "data.frame"))

0 个答案:

没有答案