在ggplot2中使用pretty时未打印的所有值
我正在寻找在ggplot中设置y上限的方法,以包括大于最大值的标签。我用在ggplot2中使用pretty时未打印的所有值,r,ggplot2,R,Ggplot2,我正在寻找在ggplot中设置y上限的方法,以包括大于最大值的标签。我用pretty很快找到了答案。但是,当我使用数据绘图时,它不会绘制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", "
pretty
很快找到了答案。但是,当我使用数据绘图时,它不会绘制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 + coord_cartesian(ylim=range(py))
p
库(ggplot2)
py在@BrodieG answer的OP评论之后,简单如下(假设下限为0,并将ymax
增加25%):
我想我真正想要的是将ymax自动缓冲X%,并将其用作范围。你对此有什么建议吗?我同意@BrodieG关于在数据的精确范围内裁剪绘图的评论,这也是我添加缓冲区评论的原因。这可能是一个新问题,但是否有办法确保始终标记最后一个y轴记号?例如,现在y轴始终大于范围,但有时(使用其他数据)最后一个记号不显示,因此不会被标记。注意,这并不完全正确,在展开之前需要range(tapply(t$value,t$year,sum))
。
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
coord_cartesian(ylim=range(py))
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
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
py <- c(0, max(t$value)*1.25)