Ant
Ant

Reputation: 343

ggplot2 geom_point with pattern fill

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:

enter image description here

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

Answers (2)

thothal
thothal

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"))

Filled Dots

Upvotes: 1

Louis
Louis

Reputation: 3632

Use the shape option.

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

Related Questions