0

I have a sf dataframe called polyg_sf that I'd like to plot with geom_sf. On every line there is the test value at location and the layer corresponds to identical locations (a square on my map at resolution 0.05). Here's how the head of the dataframe is looking :

layer Month  test geometry 
75    01     5.92 POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    02     4.37 POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    03     5.63 POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    04     8.13 POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    05     6.86 POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    06     9.46 POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    07    10.5  POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    08    19.7  POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    09    12.9  POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))
75    10    10.4  POLYGON ((-2.025 43.625, -1.975 43.625, -1.975 43.675, -2.025 43.675, -2.025 43.625))

There are 44136 observations in the dataframe with multiple layer numbers.

I want to plot this data using geom_sf.

Here is how I do it :

pred_plot <- ggplot(polyg_sf) +
  geom_sf(aes(col = test)) +
  coord_sf(xlim = c(-6,0), ylim = c(43,48), expand = FALSE)+
  scale_color_distiller(palette = "Spectral") +
  labs(caption= "k = 0.75")+
  xlab("Longitude (deg)")+
  ylab("Latitude (deg)")+
  guides(col=guide_colorbar(ticks.colour = NA))+
  facet_wrap(. ~ Month)

This is the plot I get with this command :

enter image description here

As you can see, the plot is checkered, leaving some empty spots where there is no data. I would like something where every pixels are next to each other. Do you happen to know how to solve this issue ?

Is it related to my grid set up ?

Thanks !

Edit : Thanks to margusl, I was able to fill in the polygons.

Here is the updated code :

pred_plot <- ggplot(polyg_sf) +
  geom_sf(aes(fill = test, col=test)) +
  coord_sf(xlim = c(-6,0), ylim = c(43,48), expand = FALSE)+
  scale_color_distiller(palette = "Spectral") +
  scale_fill_distiller(palette = "Spectral")+                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  labs(caption= "k = 0.75")+
  xlab("Longitude (deg)")+
  ylab("Latitude (deg)")+
  guides(col=guide_colorbar(ticks.colour = NA))+
  facet_wrap(. ~ Month)

I now get this plot enter image description here But there are regular lines that don't seem to fit to my data. Any tips on why is that ?

JulietteTC
  • 11
  • 2
  • With `aes(col = test)` and `scale_color_distiller()` you only target polygon line color. Consider setting `fill` as well. – margusl Jun 21 '23 at 14:02
  • Thanks ! It filled the empty squares ! Although, it solved this issue, there is another one arising .. I'll add it to my question as an edit ! – JulietteTC Jun 21 '23 at 14:35
  • It's probably related to polygon line aesthetics, try to remove cell grid borders altogether by setting color to NA and/or adjusting line width, perhaps start with something like `geom_sf(aes(fill = test), color = NA, linewidth = 0))` . – margusl Jun 21 '23 at 15:55
  • Hello ! I tried removing the the cell grid borders with the line you proposed but it pretty much did nothing to my plot (visually). I'll keep searching ! Any tips or ideas are greatly appreciated ! – JulietteTC Jun 22 '23 at 07:51
  • Is that dataset publically available or could you provide a small sample from there so that the issue is reproducible for others? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example provides detail on how to make a great reproducible example, the preferred method is to include `dput()` output of part of your dataset. – margusl Jun 22 '23 at 08:29
  • I understand that providing reprex (reproducible example) might not be so straightforward in this case, but perhaps start small & simple: first hundred polygons from a single month: `dput(polyg_sf[polyg_sf$Month == "01",][1:100,])`, it would be great if you could also test if that sample generates similar artefacts with ggplot: `polyg_sf_sample <- _dput_output_here; ggplot(polyg_sf_sample) + ...` . Just a single facet and partial plot is fine as long as those stripes are still there. – margusl Jun 22 '23 at 08:34
  • 1
    I made a repex, I'll add it to my question as an edit ! I can reproduce my issue with the sample even though it is less clear ! – JulietteTC Jun 22 '23 at 09:47
  • My repex for 100 lines is too big, I can't add it as is. Is there a way I could send you my data ? – JulietteTC Jun 22 '23 at 09:56
  • You could use some paste service like https://gist.github.com/ or https://pastebin.com/. If that issue visible on a smaller subset, you can also try to reduce it to e.g. 50 of 25 polygons. – margusl Jun 22 '23 at 10:05
  • Sorry for the delay ! Here is the dput of my dataframe : https://pastebin.com/fY9V5rWj . You should be able to reproduce my problem this way ! – JulietteTC Jun 22 '23 at 12:44
  • With that data sample and updated code from the question I get this: https://i.stack.imgur.com/m5Qyq.png With limits removed and added labels, I'd say it just reflects the actual values in the dataset: https://i.stack.imgur.com/CRUYr.png . If the (spatial) distribution of values is not quite what was expected, perhaps there are some data processing issues, but ggplot part seems fine. – margusl Jun 22 '23 at 20:57

0 Answers0