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