在R中使用ggplot2和facet_wrap显示多个轴标签

发布于 2024-08-07 21:36:04 字数 739 浏览 2 评论 0原文

我用 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

返回:

alt 文本
(来源:cerebralmastication.com

看起来这应该很简单,但我的 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:

alt text
(source: cerebralmastication.com)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2024-08-14 21:36:04

您可以通过在facet_wrap调用中包含scales =“free”选项来实现此目的:

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

在此处输入图像描述

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

enter image description here

很糊涂小朋友 2024-08-14 21:36:04

简短的回答:你不能那样做。对于 3 个图可能有意义,但是如果您有一个包含 32 个图的大网格怎么办?那样会显得又吵又糟糕。 GGplot 的理念是用最少的定制做正确的事情,这自然意味着你不能像其他包那样定制太多东西。

长答案:您可以通过构建三个单独的 ggplot 对象并将它们组合起来来伪造它。但这不是一个非常通用的解决方案。以下是 Hadley 书中的一些代码,假设您已经创建了 ggplot 对象 a、b 和 c。它将 a 放在顶行,将 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))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文