ggplot2 annotate text with a plot symbol

985 Views Asked by At

I would like to a legend to a ggplot2 plot. The legend should identify the plotting symbol with the data. For example, data1 is plotted with a filled square and data2 is plotted with a filled circle.

Is there a way to do this?

The example below from a previous stackoverflow question worked where one adds a math (perp.) symbol.

p <- p + annotate("text",2005,5, label="E(y)*symbol('\\136')*b", parse=TRUE) 
1

There are 1 best solutions below

1
On

Maybe this example helps:

df <- mtcars[,c(5,6,9)]
df[,3] <- as.factor(df[,3])
p <- ggplot(df, aes(x=drat,y=wt, fill=am, shape=am)) +  
            geom_point(size=4)+scale_shape_manual(values=c(21,22))

Here's the plot: enter image description here

The values 21 and 22 refer to filled circle and filled square, respectively. See ?points for the complete list of available symbols.