在R中使用ggplot2和facet_wrap显示多个轴标签
我用 ggplot2 创建了一个漂亮的facet_wrap 密度图。我希望每个面板都有 x 和 y 轴标签,而不是仅在左侧有 y 轴标签,在底部有 x 轴标签。我现在所拥有的看起来像这样:
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
返回:
看起来这应该很简单,但我的 Google Fu 太差了,找不到答案。
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 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:
(source: cerebralmastication.com)
It seems like this should be simple, but my Google Fu has been too poor to find an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在facet_wrap调用中包含scales =“free”选项来实现此目的:
You can do this by including the scales="free" option in your facet_wrap call:
简短的回答:你不能那样做。对于 3 个图可能有意义,但是如果您有一个包含 32 个图的大网格怎么办?那样会显得又吵又糟糕。 GGplot 的理念是用最少的定制做正确的事情,这自然意味着你不能像其他包那样定制太多东西。
长答案:您可以通过构建三个单独的 ggplot 对象并将它们组合起来来伪造它。但这不是一个非常通用的解决方案。以下是 Hadley 书中的一些代码,假设您已经创建了 ggplot 对象 a、b 和 c。它将 a 放在顶行,将 b 和 c 放在底行。
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.