使用facet_grid时,ggplot2直方图变化图?

时间:2023-02-03 20:26:24

I'm trying to wrap my head around why a histogram using qplot is changing when I add a facet_wrap. You can find the data I used for this example here

当我添加facet_wrap时,我试图让我的头脑明白为什么使用qplot的直方图会发生变化。你可以在这里找到我在这个例子中使用的数据

The factors in my facet_grid come from using cut:

facet_grid中的因素来自于使用cut:

library(ggplot2)
data <- read.csv("data.csv")
data$cuts <- cut(data$y, c(0, 50, 100, 500, Inf))

So now I have this:

现在我有了这个:

> summary(data)
       y                 x                cuts    
 Min.   :  10.00   Min.   :0.000   (0,50]   :530  
 1st Qu.:  20.75   1st Qu.:1.000   (50,100] :179  
 Median :  46.00   Median :1.000   (100,500]:258  
 Mean   : 110.18   Mean   :0.834   (500,Inf]: 33  
 3rd Qu.: 121.00   3rd Qu.:1.000                  
 Max.   :1526.00   Max.   :1.000                  

If I look at only the section where cuts=="(0,50]", it looks fine.:

如果我只看cut ="(0,50) "的部分,看起来没问题。

qplot(x, data=subset(data, cuts=="(0,50]"))

使用facet_grid时,ggplot2直方图变化图?

But when I add a facet grid, the y-axes are all wrong:

但是当我添加一个面网格时,y轴都是错的:

qplot(x, data=data) + facet_grid(cuts~., scales="free_y")

使用facet_grid时,ggplot2直方图变化图?

Notice that the y-axis on the top facet is now only 40ish instead of over 450. The only facet that seems to be right is (500,Inf].

注意,上面的y轴现在只有40多,而不是450多。唯一正确的方面是(500,Inf)。

edit: I'm using ggplot 0.9.0 in R 2.14.2

编辑:我在r2.14.2中使用ggplot 0.9.0。

2 个解决方案

#1


3  

ggplot 9.1 was release a day or so ago and this was taken care of in that package release. So my answer is download the ggplot2 0.9.1 bug fix version.

ggplot 9.1是大约一天前发布的,这在那个包版本中得到了处理。我的答案是下载ggplot2 0.9.1 bug修复版本。

Also you may want to play with the right argument in geom_histogram as in: geom_histogram(binwidth = 0.2, right = TRUE) +

同样,您可能想要在geom_histogram中使用正确的参数,如:geom_histogram(binwidth = 0.2, right = TRUE) +

#2


1  

It seems to be the ggplot facet_grid "ignoring duplicate row" bug as Sandy indicates. A simple work around is to add this do-nothing line,

这似乎是ggplot facet_grid“忽略重复的行”错误,就像Sandy指出的那样。一个简单的工作就是添加这一行,

data$index = seq.int(1,1000,1)

to make every row unique and you get what you want.

为了使每一行都是唯一的,你得到了你想要的。

#1


3  

ggplot 9.1 was release a day or so ago and this was taken care of in that package release. So my answer is download the ggplot2 0.9.1 bug fix version.

ggplot 9.1是大约一天前发布的,这在那个包版本中得到了处理。我的答案是下载ggplot2 0.9.1 bug修复版本。

Also you may want to play with the right argument in geom_histogram as in: geom_histogram(binwidth = 0.2, right = TRUE) +

同样,您可能想要在geom_histogram中使用正确的参数,如:geom_histogram(binwidth = 0.2, right = TRUE) +

#2


1  

It seems to be the ggplot facet_grid "ignoring duplicate row" bug as Sandy indicates. A simple work around is to add this do-nothing line,

这似乎是ggplot facet_grid“忽略重复的行”错误,就像Sandy指出的那样。一个简单的工作就是添加这一行,

data$index = seq.int(1,1000,1)

to make every row unique and you get what you want.

为了使每一行都是唯一的,你得到了你想要的。