Different breaks per facet in ggplot2 histogram

前端 未结 4 1643
青春惊慌失措
青春惊慌失措 2020-11-30 07:40

A ggplot2-challenged latticist needs help: What\'s the syntax to request variable per-facet breaks in a histogram?

library(ggplot2)
d = data.frame(x=c(rnorm(         


        
4条回答
  •  一向
    一向 (楼主)
    2020-11-30 08:17

    I don't think that it is possible to give different break points in each facet.

    As workaround you can make two plots and then with grid.arrange() function from library gridExtra put them together. To set break points in geom_histogram() use binwidth= and set one value for width of bin.

    p1<-ggplot(subset(d,par=="a"), aes(x=x) ) + 
      geom_histogram(binwidth=0.1) +
      facet_wrap(~ par)
    
    p2<-ggplot(subset(d,par=="b"), aes(x=x) ) + 
      geom_histogram(binwidth=0.2) +
      facet_wrap(~ par)
    library(gridExtra)
    grid.arrange(p1,p2,ncol=2)
    

    enter image description here

提交回复
热议问题