简体   繁体   中英

Group x-axis in bins with ggplot's geom_line

I have the following ggplot figure, produced by this code:

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))

by_year_figure

For better visualisation of the trends, I would like to group the x-axis (time) in bins of, say, 10 years. I discovered these days the characteristic of ggplot to group in bins automatically, and I think that it is awesome and very practical for data visualization.

Today, I also learnt that it can be used within geom_line, with stat = "bin", binwidth = 10 , what could do something like what I need. However, bins work counting, and, in this case, I have the 2 different "y" variables that had been already calculated. The values of each 10 years would have to be summed from the column named "value" in my tidy tibble. Trying to use stat = "bin", binwidth = 10 brings an error saying that it does not work because the "y" variable shouldn't be provided.

Error: stat_bin() must not be used with a y aesthetic.

Using this code, I was able to get the following figure, obviously wrong because ggplot is counting the number of rows instead of the values.

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))

错误_10_year_figure

At this point, I doubt whether it is possible to do this within ggplot or not. Probably not... and it is not that difficult to group data myself.

Nonetheless, I wanted to ask just in case I am missing something. Thanks for the help!

This is a subset of the table:

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"))

I was able to solve the question thanks to the hint provided by @A Gore.

It can be done using "summary_bin" as stat parameter inside "geom_line". This code produces the output I was pursuing, here choosing a bin of width 10:

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))

right_figure

Thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM