Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new shape palettes in ggplot2 and other R graphics

I'd like to specify my own shape palettes for ggplot2 at least as a vector for input into scale_shape_manual. I really like the paired shapes palette from JMP, but noticed R doesn't have some of these shapes. In particular, sideways triangles (e.g. |> or <|) or a filled upside down triangle (e.g. \/) are missing. Are these available anywhere? If not, how can I specify these extra shapes and is there a way to get ggplot2 to use them in plots?

like image 687
wdkrnls Avatar asked Jun 09 '15 20:06

wdkrnls


1 Answers

You can create your own shape palette by specifying the Unicode values for the characters you want. You can find Unicode values for various geometric shapes here. For example:

library(ggplot2)

ggplot(mtcars[mtcars$carb %in% 1:4,], 
       aes(wt, mpg, shape=factor(carb), colour=factor(carb))) +
  geom_point(size=5) +
  scale_shape_manual(values=c("\u25BA","\u25C4","\u25BC","\u25B2"))

enter image description here

You can, of course, use Unicode characters in base graphics as well:

with(mtcars, plot(wt, mpg, pch="\u25BC"))

Not every Unicode character renders correctly. I'm not sure why, but it may have to do with which fonts you have installed.

like image 198
eipi10 Avatar answered Nov 09 '22 03:11

eipi10