在x轴的直方图中设置限制,包括0值

时间:2018-04-25 09:58:34

标签: r ggplot2 histogram limits

这是我的子集数据,其中我绘制了一个突出显示0值的直方图并绘制正态分布:

values <- c(25.222222, 6.000000, 2.057143, 0.000000, 2.142857, 0.000000, 73.666667, 4.081081, 43.133333, 18.937500, 60.822222, 23.379310, 54.954412, 8.492308, 67.646250, 15.885000, 38.585859, 46.810606, 31.565152, 39.813889, 40.620000, 25.958000, 54.821429, 9.000000, 33.040476, 50.329670, 43.525641, 33.508696, 34.265385, 57.003544, 36.690434, 48.074074, 70.372222, 77.602564, 29.997436, 71.739683, 11.320000, 2.938776, 10.101562, 35.037956)

df <- data.frame(variable = "TH_part", value=values)

ggplot(df, aes(value)) + 
    geom_histogram(aes(fill = value==0), colour = "black", binwidth = 10, position="stack") + 
    scale_x_continuous(breaks = seq(0, 140, by = 20)) + 
    theme_bw() + 
    stat_function(fun=function(value, mean, sd) 10*nrow(df)*dnorm(value, mean, sd), 
                  args=fitdistr(df$value, "normal")$estimate)

normal xaxis

但是,我想将x轴扩展到140,使其具有与具有更多数据的另一个直方图相同的比例。不幸的是,我的代码丢弃了0值:

ggplot(df, aes(value)) + 
    geom_histogram(aes(fill = value==0), colour = "black", binwidth = 10, position="stack") + 
    scale_x_continuous(breaks = seq(0, 140, by = 20), limits = c(0,140)) + 
    theme_bw() + 
    stat_function(fun=function(value, mean, sd) 10*nrow(df)*dnorm(value, mean, sd), 
                  args=fitdistr(df$value, "normal")$estimate)

drops 0 values

我不明白,因为0仍在我的极限范围内。还有另一个控制xaxis的声明吗?

0 个答案:

没有答案