I want to plot using color for LAB
and shape for AMT
df = data.frame(xdat=c(20, 40, 75, 90),
ydat=c(2, 5, 10, 20),
LAB=c("PM1", "PM1", "PM2", "PM2"),
AMT=c(0.04, 1, 0.04, 4))
df %>%
ggplot(aes(x=xdat,y=ydat,color=LAB)) + geom_point(size=3)
Of course this generates a figure with a LAB
legend only, one color for PM1 and one color for PM2. This is fine, but I want the figure to also include a shape based on AMT
. So for this example, there would be three shapes, one for each unique AMT
value, where the color of the shape would match that of PM1 or PM2. Then, there would be two legends for the figure - one for LAB
(two colors for PM1 and PM2) and one for AMT
(three shapes for 0.04, 1, and 4).
I tried including shape
in ggplot(aes(x=xdat,y=ydat,color=LAB,shape=AMT)) + geom_point(size=3)
but this does not work.
ggplot(df, aes(x=xdat,y=ydat,color=LAB,shape=factor(AMT))) +
geom_point(size=3) +
scale_shape(name = "AMT") # to control legend name
(Or change to factor upstream as @Rui-barradas suggested.)