我一直在寻找在ggplot中设置y上限的方法,以包含大于最大值的标签。我使用pretty
快速找到this回答。但是,当我使用我的数据进行绘图时,它不会绘制' fire'在2004年。
这是我的数据:
t <- structure(list(park = structure(c(8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L), .Label = c("apis", "indu", "isro", "miss", "piro", "sacn",
"slbe", "voya"), class = "factor"), loc_01 = structure(c(16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("apis",
"indu", "isro", "miss", "non_apis", "non_indu", "non_isro", "non_miss",
"non_piro", "non_sacn", "non_slbe", "non_voya", "piro", "sacn",
"slbe", "voya"), class = "factor"), year = structure(c(1L, 2L,
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L), .Label = c("2002", "2003", "2004", "2005",
"2006", "2007"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("agriculture", "beaver",
"blowdown", "development", "fire", "flooding", "harvest_00_20",
"harvest_30_60", "harvest_70_90", "insect_disease_defo", "insect_disease_mort",
"unknown"), class = "factor"), value = c(0, 0, 0, 0, 0, 0, 0.0832931254278862,
0.0301695451904579, 0.0264171464103402, 0.0268075089513891, 0,
0.0131238087762612, 0.0900844930199918, 0, 0, 0.014770372800214,
0.0501002326005331, 0, 0, 0, 0.231917243175008, 0.108369509403789,
0, 0.000509329143326649)), row.names = c(NA, -24L), .Names = c("park",
"loc_01", "year", "variable", "value"), class = "data.frame")
这是我使用的代码......
library(ggplot2)
py <- pretty(t$value)
p <- ggplot () + geom_bar(data=t,aes(x=year,y=value,fill=variable),stat='identity')
p <- p + scale_y_continuous(breaks=py,limits=range(py))
p
产生这个......
如果我看t
我看到应该有一个“火”。 2004,2005和2007年的价值。但是在堆积的条形图中它缺失了。我在没有pretty
选项的情况下尝试了相同的图表,它运行正常。所以,我的错误必须与使用该选项有关。有什么想法吗?
park loc_01 year variable value
1 voya voya 2002 agriculture 0.0000000000
2 voya voya 2003 agriculture 0.0000000000
3 voya voya 2004 agriculture 0.0000000000
4 voya voya 2005 agriculture 0.0000000000
5 voya voya 2006 agriculture 0.0000000000
6 voya voya 2007 agriculture 0.0000000000
7 voya voya 2002 beaver 0.0832931254
8 voya voya 2003 beaver 0.0301695452
9 voya voya 2004 beaver 0.0264171464
10 voya voya 2005 beaver 0.0268075090
11 voya voya 2006 beaver 0.0000000000
12 voya voya 2007 beaver 0.0131238088
13 voya voya 2002 blowdown 0.0900844930
14 voya voya 2003 blowdown 0.0000000000
15 voya voya 2004 blowdown 0.0000000000
16 voya voya 2005 blowdown 0.0147703728
17 voya voya 2006 blowdown 0.0501002326
18 voya voya 2007 blowdown 0.0000000000
19 voya voya 2002 fire 0.0000000000
20 voya voya 2003 fire 0.0000000000
21 voya voya 2004 fire 0.2319172432
22 voya voya 2005 fire 0.1083695094
23 voya voya 2006 fire 0.0000000000
24 voya voya 2007 fire 0.0005093291
答案 0 :(得分:1)
使用:
coord_cartesian(ylim=range(py))
而不是scale_y_continuous
。后者删除了您定义的边界之外的任何数据。前者只是剪辑图纸。
library(ggplot2)
py <- pretty(t$value)
p <- ggplot () + geom_bar(data=t,aes(x=year,y=value,fill=variable),stat='identity')
p <- p + coord_cartesian(ylim=range(py))
p
请注意,在原来的 Grammar of Graphics 中,威尔金森(作者)强烈反对将空间和价值观映射混为一谈的美学表征。除其他外,他认为种植地块具有很大的误导性。他建议使用变换后的缩放(例如log)来处理难以用异常值而不是裁剪来破译的图。这是来自记忆,所以我没有具体的参考资料。
答案 1 :(得分:1)
在对@BrodieG回答OP评论后,它就像(假设下限为0,并将ymax
增加25%)一样简单:
py <- range(t$value)*1.25
p <- ggplot () + geom_bar(data=t,aes(x=year,y=value,fill=variable),stat='identity')
p <- p + scale_y_continuous(limits=py)
p
无论t$value
如何,都应将下限固定为0,然后使用:
py <- c(0, max(t$value)*1.25)