我有下面的ggplot图,由这段代码产生:
ggplot(PATpols, aes(Period, value, color=IUCN)) +
geom_line(aes(color = IUCN)) +
facet_grid(tag ~., scales = "free_y", labeller=label_wrap_gen(width=15)) +
scale_x_continuous(breaks= seq(1940, 2015, by=10)) +
scale_y_continuous(labels = scales::comma) +
scale_color_viridis(discrete=T) +
theme_bw()+
theme(strip.text.y = element_text(size = 8, colour = "black", angle = 0))
为了更好地了解趋势,我想将x轴(时间)分组,比方说,10年。这些天我发现了ggplot自动分组的特性,我认为它对于数据可视化来说是非常棒和非常实用的。
今天,我还了解到它可以通过stat = "bin", binwidth = 10
在geom_line中使用,这样就可以做我需要的事情了。但是,bin可以进行计数,在本例中,我有两个已经计算过的不同的"y“变量。每10年的值必须从我整洁的tibble中名为"value“的列中求和。尝试使用stat = "bin", binwidth = 10
时会出现一个错误,指出它不工作,因为不应该提供"y“变量。
Error: stat_bin() must not be used with a y aesthetic.
使用这段代码,我可以得到下面的图,显然是错误的,因为ggplot计算的是行数而不是值。
ggplot(PATpols, aes(Period, color=IUCN)) +
geom_line(aes(color = IUCN), stat = "bin", binwidth = 10) +
facet_grid(tag ~., scales = "free_y", labeller=label_wrap_gen(width=15)) +
scale_x_continuous(breaks= seq(1940, 2015, by=10)) +
scale_y_continuous(labels = scales::comma) +
scale_color_viridis(discrete=T) +
theme_bw()+
theme(strip.text.y = element_text(size = 8, colour = "black", angle = 0))
在这一点上,我怀疑是否有可能在ggplot中做到这一点。可能不会..。而且我自己对数据进行分组也不是那么困难。
尽管如此,我还是想问一下,以防我遗漏了什么。谢谢你的帮助!
这是表格的一个子集:
PATpols <- structure(list(Period = c(1980, 1980, 1980, 1980, 1980, 1980,
1990, 1990, 1990, 1990, 1990, 1990, 2000, 2000, 2000, 2000, 2000,
2000, 2010, 2010, 2010, 2010, 2010, 2010, 1980, 1980, 1980, 1980,
1980, 1980, 1990, 1990, 1990, 1990, 1990, 1990, 2000, 2000, 2000,
2000, 2000, 2000, 2010, 2010, 2010, 2010, 2010, 2010), variable = c("new.PA",
"new.PA", "new.PA", "new.PA", "new.PA", "new.PA", "new.PA", "new.PA",
"new.PA", "new.PA", "new.PA", "new.PA", "new.PA", "new.PA", "new.PA",
"new.PA", "new.PA", "new.PA", "new.PA", "new.PA", "new.PA", "new.PA",
"new.PA", "new.PA", "new.area", "new.area", "new.area", "new.area",
"new.area", "new.area", "new.area", "new.area", "new.area", "new.area",
"new.area", "new.area", "new.area", "new.area", "new.area", "new.area",
"new.area", "new.area", "new.area", "new.area", "new.area", "new.area",
"new.area", "new.area"), value = c(0, 1, 2, 0, 0, 1, 0, 0, 17,
0, 0, 0, 0, 1, 0, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 5575.58852902375,
0, 0, 0, 0, 0, 0, 19008.4210385919, 0, 0, 0, 0, 616.617197104555,
0, 232.522843017563, 0, 3351.82112023738, 234.321752235977, 0,
0, 42.7373095251387, 42.7094617704834, 6383.74665457854), tag = c("n",
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n",
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "km2", "km2",
"km2", "km2", "km2", "km2", "km2", "km2", "km2", "km2", "km2",
"km2", "km2", "km2", "km2", "km2", "km2", "km2", "km2", "km2",
"km2", "km2", "km2", "km2"), IUCN = 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, 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("I",
"II", "III", "IV", "V", "VI"), class = "factor")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -48L), .Names = c("Period",
"variable", "value", "tag", "IUCN"))
发布于 2017-07-12 04:36:58
多亏了@A Gore提供的提示,我才能解决这个问题。
可以在"geom_line“中使用"summary_bin”作为stat
参数。这段代码产生了我所追求的输出,这里选择一个宽度为10的bin:
ggplot(PATpols, aes(Period, value, color=IUCN)) +
geom_line(aes(color = IUCN), stat = "summary_bin", binwidth = 10) +
facet_grid(tag ~., scales = "free_y", labeller=label_wrap_gen(width=15)) +
scale_x_continuous(breaks= seq(1940, 2015, by=10)) +
scale_y_continuous(labels = scales::comma) +
scale_color_viridis(discrete=T) +
theme_bw()+
theme(strip.text.y = element_text(size = 8, colour = "black", angle = 0))
谢谢你的帮助!
https://stackoverflow.com/questions/45041220
复制相似问题