Reputation: 343
I have a chart that is measuring a risk variable through time for a number of people - I've added a snippet of the plot below:
The colours here represent risk - I would like add an additional variable to the plot and represent this by changing the pattern on individual points. In excel, it is possible to fill points with patterns such as stripes, hashed etc. Is this possible within the geom_point aesthetic in ggplot2?
I realise varying the shape of the points would accomplish the same thing but this is not as visually immediate as varying the pattern.
Upvotes: 1
Views: 1695
Reputation: 20409
Besides changing the shape you can use filled dots, where you can change color
and fill
separately. For this you need a symbol (shape
) in the range between 21
and 25
:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, fill = as.factor(am), color = as.factor(vs))) +
geom_point(shape = 21, size = 4) +
scale_fill_manual(values = c("white", "lightgray")) +
scale_color_manual(values = c("black", "orange"))
Upvotes: 1
Reputation: 3632
Yes, it is possible to change the shape of points according to a variable you have.
You have to use the option shape
when mapping. This is a quick example with mtcars
dataframe:
library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg, shape=as.factor(cyl))) +
geom_point()
Note: Just remember that a continuous variable can not be mapped to shape.
Hope this helps.
Upvotes: 2