为什么facet_grid将分布放在错误的象限中? [英] Why is facet_grid placing the distributions in the wrong quadrants?

查看:113
本文介绍了为什么facet_grid将分布放在错误的象限中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2中使用facet_grid(x ~ y)时,我已经在各种示例中看到并阅读了文档,其中x变量垂直放置,而y变量水平放置.但是,当我运行以下命令时:

When using facet_grid(x ~ y) with ggplot2 I've seen in various examples and read in the documentation that the x variable is laid out vertically and the y variable horizontally. However, when I run the following:

set.seed(1)
b = c(rnorm(10000,mean=0,sd=0.5),rnorm(10000,mean=5,sd=0.5),
      rnorm(10000,mean=7,sd=0.5),rnorm(10000,mean=10,sd=0.5))
x = c(rep('xL', 20000), rep('xR',20000))
y = c(rep('yL',10000), rep('yR',20000), rep('yL',10000))
foo = data.frame(x=x,y=y,b=b)

ggplot(data=foo, aes(foo$b)) + 
  geom_histogram(aes(y=..density..),breaks=seq(-5,12,by=.2),col='steelblue',fill='steelblue2') + 
  geom_density(col='black') + 
  facet_grid(x ~ y, scales='free_y')

我得到以下信息(对不起,质量).即使从上方看,均值为10的分布是(x,y)为'xR,xL'的分布,但其中一个出现在右下象限中,其标签为'xR,yR'.我在做什么错了?

I get the below (sorry for the quality). And even though, from above, the distribution with mean 10 is the one with (x,y) of 'xR,xL' that one appears in the bottom right quadrant which has labels 'xR,yR'. What am I doing wrong?

推荐答案

aes(foo$b)更改为aes(x = b),以确保外观正确映射.

Change aes(foo$b) to aes(x = b) to make sure the aesthetics are mapping correctly.

您要确保ggplot从正确的范围(即从已传递的数据)中找到标记为b的列.例如,可能是ggplot在传递数据时重新排列了数据的情况,因此映射变量foo$b不再与所需内容对齐.

You want to make sure ggplot is finding the column labelled b from the correct scope i.e. from the data that it has been passed. For example, it may be the case that ggplot rearranged your data when you passed it, so mapping the variable foo$b no longer aligns with what you want.

我并不是说这是发生了什么-只是为什么在正确的范围内调用美学很重要的一个例子.

I'm not saying this is what happened - just an example of why calling the aesthetic from the correct scope is important.

这篇关于为什么facet_grid将分布放在错误的象限中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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