Showing multiple axis labels using ggplot2 with facet_wrap in R

前端 未结 2 367
面向向阳花
面向向阳花 2021-01-04 18:26

I\'ve got a nice facet_wrap density plot that I have created with ggplot2. I would like for each panel to have x and y axis labels instead of only having the y

相关标签:
2条回答
  • 2021-01-04 18:33

    You can do this by including the scales="free" option in your facet_wrap call:

    myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replace=T)
    myValues <- rnorm(300)
    df <- data.frame(myGroups, myValues)
    
    
    p <- ggplot(df)  + 
      geom_density(aes(myValues), fill = alpha("#335785", .6)) + 
      facet_wrap(~ myGroups, scales="free")
    p
    

    0 讨论(0)
  • 2021-01-04 18:50

    Short answer: You can't do that. It might make sense with 3 graphs, but what if you had a big lattice of 32 graphs? That would look noisy and bad. GGplot's philosophy is about doing the right thing with a minimum of customization, which means, naturally, that you can't customize things as much as other packages.

    Long answer: You could fake it by constructing three separate ggplot objects and combining them. But it's not a very general solution. Here's some code from Hadley's book that assumes you've created ggplot objects a, b, and c. It puts a in the top row, with b and c in the bottom row.

    grid.newpage()
    pushViewport(viewport(layout=grid.layout(2,2)))
    vplayout<-function(x,y)
        viewport(layout.pos.row=x,layout.pos.col=y)
    print(a,vp=vplayout(1,1:2))
    print(b,vp=vplayout(2,1))
    print(c,vp=vplayout(2,2))
    
    0 讨论(0)
提交回复
热议问题