1

Using ggplot2 (v 3.2.1) and sf (v. 0.7-7) I can easily plot a map of my raw data with coloured points in the shape I choose, with no issues:

  P1 <- ggplot() +
  geom_sf(data = Map.nb) +
  geom_sf(data = Df_raw, shape = 22, size = 2, fill = 'purple') +
  theme_bw()

enter image description here In the raw data plotted above, each location has multiple rows of data (e.g. three traps per site). I create a second data frame summarizing the data per site using dplyr (v. 0.8.3)

Df.summarize <- Df_raw %>%
  group_by(plot_id) %>% 
  summarise(moth_per_plot = sum(moth_count), traps = n_distinct(trap)) %>% 
  mutate (avg_moth_per_trap = moth_per_plot/traps)

If I plot the summarized data, it maps just fine, but I cannot change the shape or fill of the points.

P2 <- ggplot() +
  geom_sf(data = Map.nb) +
  geom_sf(data = Df.summarize, shape = 22, size = 2, fill = 'purple') +
  theme_bw()

enter image description here Oddly enough if I replace fill = 'purple' with col = 'purple' the outline of the points changes colour (as expected). I'm at a loss why I can change col but not fill or shape.

Does anyone know how I can change the fill & shape in my plot of summarized data? Or have any explanation why it works with my raw data but not for the summary data?

Note: class(Df.summarize) is 'sf', 'tbl_df', 'tbl' & 'data.frame' class(Df.raw) is 'sf' & 'data.frame'

I've converted the class of Df.summarize to match Df.raw, but that doesn't help.

Edit str() of each data frame:

str(Df_raw)
Classes ‘sf’ and 'data.frame':  390 obs. of  3 variables:
 $ plotid    : chr  "4592 6665" "4566 6698" "4546 6653" "4571 6617" ...
 $ moth_count: num  22 41 4 20 129 1 8 2 95 35 ...
 $ SHAPE     :sfc_POINT of length 390; first list element:  'XY' num  2487757 7436473
 - attr(*, "sf_column")= chr "SHAPE"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA
  ..- attr(*, "names")= chr  "plotid" "moth_count"


str(Df_summarized)
Classes ‘sf’, ‘tbl_df’, ‘tbl’ and 'data.frame': 156 obs. of  5 variables:
 $ plotid           : chr  "4538 6587" "4538 6715" "4545 6561" "4546 6653" ...
 $ moth_per_plot    : num  7 135 17 12 2 18 59 152 13 19 ...
 $ traps            : int  3 3 2 2 2 3 3 3 3 3 ...
 $ SHAPE            :sfc_GEOMETRY of length 156; first list element:  'XY' num [1:3, 1:2] 2548882 2548899 2548920 7376785 7376824 ...
 $ avg_moth_per_trap: num  2.33 45 8.5 6 1 ...
 - attr(*, "sf_column")= chr "SHAPE"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA
  ..- attr(*, "names")= chr  "plotid" "moth_per_plot" "traps" "avg_moth_per_trap"
Keith Stein
  • 6,235
  • 4
  • 17
  • 36
Sara
  • 61
  • 5
  • Can you provide a reproducible example of your dataset ? (see this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Apr 27 '20 at 20:42
  • This is likely an issue with the specific data. Could you upload an example dataset where this isn't working? Or the results of `str(Df.raw)` and `str(Df.Summarize)`? – m.evans Apr 27 '20 at 20:45
  • I added the `str()` of each data frame to the original question. The only difference I see is the class, I've changed the class of the summarized data to match the raw, but that doesn't seem to help. – Sara Apr 27 '20 at 21:11
  • Is it because the summarized data is sfc_GEOMETRY and the raw data is sfc_POINT? Not sure why that has changed...going to look into changing that see if that resolves the issue. – Sara Apr 27 '20 at 21:19
  • Maybe when you perform `summarize` it make the changes on your `geometry`. Maybe you should use `mutate` to add the mean per points and then use `distinct` to keep only distinct coordinates. So it should preserve the last column. Not sure.. just a guess – dc37 Apr 27 '20 at 23:18
  • Figured it out, the gps points were not entered exactly the same per site so summarize combined them to create a multipoint object. `st_cast(Df.Summarize, to = "POINT")` fixed it. I feel dumb for not noticing this before, but glad I got it sorted out it was driving me crazy. Thanks for the help! – Sara Apr 28 '20 at 12:04
  • @Sara I think if you've found the answer then it'll be nice, if you can post the same. It might help someone else. Besides, it may as well, attract possible revisions from the community to make improvements if any. – mnm May 03 '20 at 02:20

0 Answers0