在R中使用ggplot2和facet_wrap显示多个轴标签 [英] Showing multiple axis labels using ggplot2 with facet_wrap in R

查看:567
本文介绍了在R中使用ggplot2和facet_wrap显示多个轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用ggplo2创建了一个很好的facet_wrap密度图。我希望每个面板都有x和y轴标签,而不是只有沿着左侧的y轴标签和沿着底部的x标签。我现在的样子是这样的:

pre $ library $ g
myGroups< - sample(c( (myGroups,myValues)
(m),Larry,Curly),100,replace = T)
myValues< - rnorm(300)
df<

p < - ggplot(df)+
geom_density(aes(myValues),fill = alpha(#335785,.6))+
facet_wrap(〜myGroups)
p

返回:

替代文字http://www.cerebralmastication.com/wp -content / uploads / 2009/10 / 3stooges.png



看来这应该很简单,但我的Google Fu太穷了,找不到回答。

解决方案

简短回答:你不能那样做。这可能对3个图有意义,但如果你有一个32格的大格子呢?这看起来很嘈杂和不好。 GGplot的理念是用最少的定制来做正确的事情,这意味着自然不能像其他软件包那样定制事物。

长答案:你可以通过构建三个单独的ggplot对象并将它们组合来伪造它。但这不是一个通用的解决方案。下面是Hadley书中的一些代码,假设你已经创建了ggplot对象a,b和c。它放在第一行,b和c在最后一行。

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


I've got a nice facet_wrap density plot that I have created with ggplo2. I would like for each panel to have x and y axis labels instead of only having the y axis labels along the left side and the x labels along the bottom. What I have right now looks like this:

library(ggplot2)
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)
p

Which returns:

alt text http://www.cerebralmastication.com/wp-content/uploads/2009/10/3stooges.png

It seems like this should be simple, but my Google Fu has been too poor to find an answer.

解决方案

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

这篇关于在R中使用ggplot2和facet_wrap显示多个轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆