in ggplot2 can you assign the aesthetic 'shape' to more than one variable?

108 Views Asked by At

I have an ordination plot and there are four environmental parameters I want to overlay. I have used the aesthetics: shape, colour and size for three of the variables and for the fourth i want the shapes to either be filled in or not filled in. I have attached a plot as an example of what I want this to look likeplot that i sort of want it to look like. My four variables are MA, HCC, Rugosity and Depth

So far I have this (it is by no means done, i simply want to get the fill sorted out before i edit). my plot

Here is the code:

nmds.holistic <- ggplot(site.scrs, aes(x=NMDS1, y=NMDS2))+ 
  geom_point(
    aes(
      NMDS1, 
      NMDS2, 
      size = factor(site.scrs$Rugosity), 
      colour = (site.scrs$MA), 
      shape = factor(site.scrs$HCC)
    )
  )+ 
  coord_fixed() +
  theme_classic() + 
  theme(
    panel.background = element_rect(
      fill = NA, 
      colour = "black", 
      linewidth = 1, 
      linetype = "solid")
  ) +
  labs( size = "Rugosity")+ # add legend labels for Rugosity
  theme(
    legend.position = "right", 
    legend.text = element_text(size = 12), 
    legend.title = element_text(size = 12), 
    axis.text = element_text(size = 10)
  )

nmds.holistic + labs(title = "Holistic")

I know that you can assign specific shapes using scale_shape_manual, including the outlined and filled ones but you cannot apply shape to two variables as far as i'm aware (in my case the two variables are HCC and the variable depth which has two values). I wanted to know if there was a way around this? How can I assign the aesthetic shape to two variables and if I cannot is there a way around it?

I have a very basic understanding of R and this is my first time posting here so I'm sorry if I have not provided the right details or if I have used incorrect terminology, feel free to correct me.

1

There are 1 best solutions below

0
Bill Perry On

what about adding a fill with the variable you want to fill by? that would fill all based on that variable?

   nmds.holistic <- ggplot(site.scrs, aes(x=NMDS1, y=NMDS2))+ 
  geom_point(
    aes(
      NMDS1, 
      NMDS2, 
      size = factor(site.scrs$Rugosity), 
      colour = (site.scrs$MA), 
      shape = factor(site.scrs$HCC),
      ***fill = (df$z)***
    )
  )+ 
  coord_fixed() +
  theme_classic() + 
  theme(
    panel.background = element_rect(
      fill = NA, 
      colour = "black", 
      linewidth = 1, 
      linetype = "solid")
  ) +
  labs( size = "Rugosity")+ # add legend labels for Rugosity
  theme(
    legend.position = "right", 
    legend.text = element_text(size = 12), 
    legend.title = element_text(size = 12), 
    axis.text = element_text(size = 10)
    )