Creating new shape palettes in ggplot2 and other R graphics

爷,独闯天下 提交于 2019-12-17 19:04:55

问题


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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/30742379/creating-new-shape-palettes-in-ggplot2-and-other-r-graphics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!