teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
562 stars 33 forks source link

geom_polygonraster - grey lines between pixels #113

Closed ThomasLuypaert closed 1 year ago

ThomasLuypaert commented 1 year ago

Hi Teun,

First of all, thanks a lot for making this package, you've saved me a lot of time and trouble trying to plot rasters in the polar coordinate system. I'm using your function to map daily patterns in the occurrence of sound over a certain period. I'm getting very close to the plot I want to produce, with one final hurdle: there are always grey lines in between my pixels.

I've encountered this issue before in ggplot with geom_tile, where this can be resolved by setting both the color and fill options. However, this does not seem to help using geom_polygonraster.

Here is an example of a plot I'm getting:

cover_chapter_1

As you can see, there are faint grey lines between my pixels, which creates a weird star-shaped pattern in some areas of the plot.

Here is the code I'm using to produce this plot:

polar_plot <- 

  ggplot(Fuzaca_df_long, aes(x = variable, y = as.numeric(frequency), color = value, fill = value))+

  ggh4x::geom_polygonraster()+

  scale_color_viridis_c(option = "A", end = 1, na.value = "black")+

  scale_fill_viridis_c(option = "A", end = 1, na.value = "black")+

  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+

  coord_polar()+

  theme(
    axis.line = element_blank(), 
    axis.text = element_blank(), 
    axis.title = element_blank(), 
    axis.ticks = element_blank(), 
    plot.background = element_rect(color = "black", fill = "black"), 
    panel.background = element_rect(color = "black", fill = "black"), 
    plot.margin = element_blank()
  ) 

If you have any suggestions on how this behaviour may be avoided, that would help me out a great deal!

Cheers, Thomas

teunbrand commented 1 year ago

Hi Thomas,

Thanks for filing this issue. Unfortunately, there isn't a lot that I can do as the underlying problem originates in the graphics device. In particular, the anti-aliasing setting, that for most but not all devices anti-aliases lines but not fills, causes the Moiré patterning you see as stars in your plot. I wish I had a better solution, but if you can control this with geom_tile(), wouldn't that just work better?

ThomasLuypaert commented 1 year ago

Hi Teun,

Thanks for getting back to me on this so quickly, and for explaining what is causing this issue.

Yes, technically I could achieve the same thing using geom_tile with coord_polar, however there are two issues:

  1. It is MUCH slower for a plot with this many pixels
  2. I can't seem to close the gap that connects the start of the polar coordinate plot with the end (example below)

Here is an example of what I mean by point 2 (notice the gap at 00:00):

image

I've tried setting expand = c(0,0), but that does not solve the issue...

I know this is not an issue with your package, but if you've got any idea on how I might solve this issue with geom_tile, that would be hugely helpful :)

teunbrand commented 1 year ago

Well you might be interested to try the dev version of ggplot2 for this because drawing rectangles in polar coordinates have been sped up considerably for many rectangles (see https://github.com/tidyverse/ggplot2/pull/5081). I can take a look at the gap issue, but don't know how to reproduce it, so if you have some dummy data + code that makes this gap it would be easier to test solutions.

teunbrand commented 1 year ago

I'm going to close this issue as I think that the underlying issue is with how graphics devices work, which I don't have a lot of control over from gg4hx's POV.