Code for getting different symbols on GGPlot


I just got thrown a project to complete with R and I am VERY new (I only knew some basic Command Line and Python before this), so I hope my question is a simple one. I've been googling and self teaching for the last week or so, but haven't found an example for what I'm trying to accomplish.

I'm plotting locations of different plant species according to decade and sample source. I have the decade part down and color-coded, but I have a column in my dataset called 'basisOfRecord' with two categories in it. 'HumanObservation' or 'PreservedSpecimen.'

Basically I'd just like triangles for the preserved plants and the circles I have now for the observed ones. How would I change my code to include that? Here's my script and a picture of what it produces for me.

Please let me know if I need to change my post formatting or provide any more information or attach my dataset or something. I'm sorry if my code is really messy!


americanus <- read.csv("C:/Users/MayeJ/Documents/Map Data/GBIF/Americanus/americanus.csv") 

month <- 1  
day <- 1
americanus$TrueDate <- as.Date(paste(americanus$TrueDate, month, day, sep = "-"))

americanus <- americanus[complete.cases(americanus$decimalLatitude, americanus$decimalLongitude), ]

obs.year = lubridate::year(americanus$TrueDate) 
decade.cha = as.character(floor(obs.year / 10) * 10) 
decade = (floor(obs.year / 10) * 10)
americanus <- st_as_sf(americanus, coords = c("decimalLongitude", "decimalLatitude"), crs = 4326)

crs = "WGS84"
agr = "constant"
remove = F


north.america.bound <- st_read("/Users/MayeJ/Documents/Map Data/NA_PoliticalDivisions/NA_PoliticalDivisions/data/bound_l/boundary_l_v2.shp") <- ggplot(data = north.america.bound) + 

  theme_bw() + 
  geom_sf(fill = "gray90", color = "black") + 
  geom_sf(data = americanus, # plots the data points
          size = 3, 
          shape = 21, 
          alpha = 0.7, 
          aes(fill = decade.cha)) + 
  scale_fill_viridis(option = "B", discrete = T, direction = 1) + 

  coord_sf(xlim = c(-3235000, 3062000),
           ylim = c(-900000, 4007000))

ggsave(plot =,
       "C:/Users/MayeJ/Documents/Map Data/GBIF/Americanus/americanusmap.jpg",
       device = "jpg", dpi = 300, units = "in",
       width = 10, height = 7.5)

Basically, you want to set shape to depend on basisOfRecord I think. Take a look at the font of all R graphics wisdom,, in particular 5.3 Using Different Point Shapes | R Graphics Cookbook, 2nd edition

Would it be formatted like this then?

theme_bw() + 
  geom_sf(fill = "gray90", color = "black") + 
  geom_sf(data = americanus, 
          size = 3,  
          alpha = 0.7, 
          aes(fill = decade.cha)) + 
  scale_shape_manual(data = americanus$basisOfRecord,
          values = c(21, 24))
  scale_fill_viridis(option = "B", discrete = T, direction = 1) + 

I think so. Not sure if data= should include the dataframe name or not.

Ran it with and without the dataframe. Without the dataframe it says the object isn't found.

Error in manual_scale("shape", values, breaks, ..., na.value = na.value) : 
  object 'basisOfRecord' not found

With the dataframe I get a much more complex error that's very long.

Error in discrete_scale(aesthetic, "manual", pal, breaks = breaks, limits = limits,  : 
  unused argument

And then it goes on to list everything in the column.

I just don't have formatting intuition yet. Could it be because I need to make everything else scale shape manual along with it?

I think you don't want to pass in the data, because that's already in the plot. It is possible you want to set shape= basisOfRecord.

But I suspect a better ggplot-er than I may be needed.

I'll keep messing around with it. You've given me a better starting point than my own searches so far. Thanks!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.