user1977802
user1977802

Reputation: 353

Combining ggplots in R with common legend

I have 4 graphs that are similar to this one (just use a different dataframe (don, don1, don2). I want to combine them in one graph with the line colors designated as the dataframe with a common legend.

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

Upvotes: 0

Views: 68

Answers (1)

Luke Hayden
Luke Hayden

Reputation: 712

You can use facets for this. Like so:

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) +....

Upvotes: 1

Related Questions