我有以下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的特点是自动分组,我认为它对于数据可视化来说非常棒且非常实用。
今天,我还了解到它可以在geom_line中使用,stat = "bin", binwidth = 10
,可以做我喜欢的事情。但是,垃圾箱工作计数,在这种情况下,我有两个不同的" y"已经计算过的变量。每10年的值必须从名为" value"的列中求和。在我整洁的tibble。
尝试使用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"))
答案 0 :(得分:1)
由于@A Gore提供的提示,我能够解决这个问题。
可以使用“summary_bin”作为“geom_line”中的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))
感谢您的帮助!