首页 > 解决方案 > 将 R 中的 ggplots 与常见图例相结合

问题描述

我有 4 个与此类似的图表(只是使用不同的数据框(don、don1、don2)。我想将它们组合在一个图表中,其中线条颜色指定为具有共同图例的数据框。

 ggplot(don, aes(x=poscum, y=pval)) +

 # Show all points
 geom_line() +
 # custom X axis:
 scale_x_continuous( label = axisdf$CHR, breaks= axisdf$center ) +
 scale_y_continuous(expand = c(0, 0) ) +     # remove space between plot area 
  and x axis

 # Custom the theme:
 theme_bw() +
 theme( 
   legend.position="none",
   panel.border = element_blank(),
   panel.grid.major = element_line(colour="white"),
   panel.grid.minor = element_line(colour="white"),
   axis.ticks = element_line(colour = "black"),
   axis.line = element_line(colour = "black"),
 )

标签: rggplot2

解决方案


您可以为此使用构面。像这样:

donall <- data.frame(rbind(don, don1, don2), group= rep("don", nrow(don)), rep("don1", nrow(don1)), rep("don2", nrow(don2)))

ggplot(donall, aes(x=poscum, y=pval)) +
 geom_line()+
facet_wrap(~group, ncol=2) +....

推荐阅读