Add a geom layer for a single panel in a faceted plot

前端 未结 1 1380
灰色年华
灰色年华 2021-02-01 23:25

Taking a cue from the following link Aligning two plots with ggplot2, I was able to plot 2 \"y\" variables faceted against a common x axis. What I want to do now is to be able t

相关标签:
1条回答
  • 2021-02-01 23:53

    Just add the panel column to d3 with the panel you want to add the point set to. In your case:

    d3$panel = "a"
    
    p <- ggplot(data = d, mapping = aes(x = x, y = y))
    p <- p + facet_grid(panel ~ ., scale = "free")
    p <- p + layer(data = d1,  geom = c( "line"), stat = "identity")
    p <- p + layer(data = d3,  geom = c( "point"))
    p <- p + layer(data = d2,  geom = "line", stat = "identity")
    p
    

    which yields the correct output:

    enter image description here

    If the column mentioned in the call to facet_grid is not present, ggplot2 assumes it needs to be printed on all facets. When you specify panel, ggplot2 will take it into account.

    0 讨论(0)
提交回复
热议问题