【问题标题】:Missing Colours in the combined plot by 'ggarrange''ggarrange' 的组合图中缺少颜色
【发布时间】:2018-09-11 21:52:21
【问题描述】:

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

在组合图中我只得到两种颜色(红色和黑色)

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

这是制作单个图的函数:

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"))

【问题讨论】:

  • ggarrange 来自哪个包?此外,您真的需要一个包含 14 个(!)子图的示例来使您的问题可重现吗?请使用您提供的示例数据使您的示例可重现(示例数据似乎仅适用于一个子图?)。
  • 'ggarrange' 来自 ggpubr 包。我还纠正了其他问题。
  • 看起来黑色的geom_area边框太厚了,覆盖了填充区域。 geom_area 中的size 参数将解决此问题。
  • 即使进行了编辑,我也无法重现给定数据集的问题。

标签: r ggplot2


【解决方案1】:

看起来 geom_area 的边框太厚了。调整方法如下:

fake <- tibble(
  x = 0:100,
  a = 1, b = 1, c = 1, d = 1,
  e = 100 - .01*x^2,
) %>% gather(series, value, -x)

ggplot(fake, aes(x, value, fill = series)) + 
  # Left version has default borders
  geom_area(color = "black", lty = "solid") + # default seems to be size = 0.5
  # Right version has thin borders
  geom_area(aes(x+110), color = "black", lty = "solid", size = 0.2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多