将 ggplot 和 GGally 堆叠在彼此之上?

时间:2021-04-20 13:30:53

标签: r ggplot2 ggally

我有两个图要突出环境问题的高分和低分之间的差异。一个图是用 GGally 包制作的,另一个图是用 ggplot 制作的。
图 2(汇总均值,使用 ggplot)是图 1(所有数据点,使用 GGally)的汇总。
是否可以将它们堆叠在一起?这样粗线就可以在小线之上?

图 1(基于原始 df)all datapoints

library(GGally)
plot1 <-  ggparcoord(cbc,
             columns = 28:31, groupColumn = 11,
             scale="globalminmax",
             showPoints = TRUE,
             alphaLines = 0.2
  ) + 
    xlab("levels of TYPE") +
    labs(color = "enviornmental concern") +  
    theme_bw() +

Plot2(基于一个 pivot_longer df)means

cbc_type <- cbc_longer_u %>% 
  filter(levels_ordered %in% c("diesel", "petrol", "plug-in hybrid", "100% electric")) %>% 
  dplyr::select(eco, levels_ordered, utilities) %>% 
  group_by(eco, levels_ordered) %>% 
  summarise_all(.funs = mean) %>%
  mutate(ecofact=factor(eco))

plot2 <- ggplot(cbc_type, aes(x=as.numeric(levels_ordered), y=utilities, color=ecofact)) + 
    geom_point() + 
    geom_line(size=2) +
    xlab("levels of TYPE") +
    labs(color = "enviornmental concern") + 
    scale_color_manual(values=c( "red", "red", "#E8E8E8", "#E8E8E8", "#E8E8E8", "#E8E8E8", "#228B22","#228B22") ) +
    theme_bw()
  
plot2 +   
    scale_x_continuous(breaks = c(13, 14, 15, 16),
                       labels = c("diesel", "petrol", "plug-in hybrid", "100% electric"))

我尝试将美学放入 plot1,但它给出了错误:

plot1 + geom_line(cbc_type, aes(x=as.numeric(levels_ordered), y=utilities, color=ecofact))

谢谢!!

0 个答案:

没有答案