使用facet_grid时ggplot2直方图更改图吗? [英] ggplot2 histogram changes plot when using facet_grid?
问题描述
我试图围绕为什么使用qplot的直方图在添加facet_wrap时发生变化。你可以找到我用于这个例子的数据 here
我的facet_grid中的因素来自使用 cut
:
库(ggplot2)
data <-read.csv(data.csv)
data $ cuts < - cut(data $ y,c(0,50,100,500,Inf ))
所以现在我有了这个:
>摘要(数据)
x x削减
最小值。 :10.00分钟。 :0.000(0,50):530
1曲线:20.75第一曲线:1.000(50,100):179
中位数:46.00中位数:1.000(100,500):258
平均值:110.18平均值:0.834(500,Inf):33
3曲线:121.00 3曲线:1.000
最大值:1526.00最大值:1.000
如果我只查看削减==(0,50)
的部分,看起来没问题。:
qplot(x,data = subset(data,cuts ==(0,50)))
但是,当我添加一个facet grid时,y轴都是错误的:
qplot(x,data = data)+ facet_grid(cuts〜。,scales =free_y)
请注意,轴上的顶点现在只有40个而不是450个。似乎是正确的唯一方面是(500,Inf)
。
编辑:我在R 2.14.2中使用ggplot 0.9.0
你也可以使用右键
参数在 geom_histogram
中,如: geom_histogram(binwidth = 0.2,right = TRUE)+
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
The factors in my facet_grid come from using 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.:
qplot(x, data=subset(data, cuts=="(0,50]"))
But when I add a facet grid, the y-axes are all wrong:
qplot(x, data=data) + facet_grid(cuts~., scales="free_y")
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]
.
edit: I'm using ggplot 0.9.0 in R 2.14.2
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.
Also you may want to play with the right
argument in geom_histogram
as in: geom_histogram(binwidth = 0.2, right = TRUE) +
这篇关于使用facet_grid时ggplot2直方图更改图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!