hafen / geofacet

R package for geographical faceting with ggplot2
https://hafen.github.io/geofacet/
Other
339 stars 45 forks source link

unused positions fill up when rescaling the y-axis with "expand_limits()" #300

Open MPietzke opened 3 years ago

MPietzke commented 3 years ago

I came across the instruction: "In this plot we are using a “free” y-axis, allowing the range of the y-axis to fill the available plotting space for each country. We do this because there is a large range of mean GDP across countries. Ideally ggplot2 would have a “sliced” axis range option that allows the y-axis range to vary but with each panel’s axis having the same length of range, making for a more meaningful comparison across countries." I thought forcing the y-axis to include 0 could improve this. ggplot(eu_gdp, aes(year, gdp_pc)) + geom_line(color = "steelblue") + facet_geo(~ name, grid = "eu_grid1", scales = "free_y") + scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) + ylab("GDP Per Capita in Relation to EU Index (100)") + expand_limits(y=0) + theme_bw()

However, this adds empty plots at all locations not being used. image

DianaDaInLee commented 12 months ago

I'm dealing with the same issue. Were you able to find a solution?

MPietzke commented 12 months ago

The issue seems to be based on the exand_limits() function, adding a geom_blank that should be filtered out by facet_geo(). Luckily, there are other options to include the 0. Both of them "scale_y_continuous(limits = c(0, NA))" and "coord_cartesian(ylim = c(0, NA))" do work fine!

ggplot(eu_gdp, aes(year, gdp_pc)) +
  geom_line(color = "steelblue") +
  # this is working:
   scale_y_continuous(limits = c(0, NA)) + 
  # this is working:
  #coord_cartesian(ylim = c(0, NA)) +
  # this does not work!  expand_limits(y=0) +
  facet_geo(~ name, grid = "eu_grid1", scales = "free_y") +
  scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
  ylab("GDP Per Capita in Relation to EU Index (100)") +
  theme_bw() 

grafik