在ggplot2和其他R图形中创建新的形状调色板 [英] Creating new shape palettes in ggplot2 and other R graphics
问题描述
我想为 ggplot2
指定我自己的形状调色板,至少作为输入到 scale_shape_manual
。我非常喜欢JMP的配对形状调色板,但注意到R没有这些形状。尤其是,横向三角形(例如 |>>
或< |
)或者倒立三角形(例如 \ /
)缺失。这些可在任何地方使用如果没有,我怎么能指定这些额外的形状,并有办法让 ggplot2
在绘图中使用它们?
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?
推荐答案
您可以通过为所需字符指定Unicode值来创建自己的形状调色板。您可以在此处找到各种几何形状的Unicode值。例如:
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"))
你当然也可以在基本图形中使用Unicode字符:
You can, of course, use Unicode characters in base graphics as well:
with(mtcars, plot(wt, mpg, pch="\u25BC"))
不是每个Unicode字符都能正确呈现。我不确定为什么,但它可能与您安装的字体有关。
Not every Unicode character renders correctly. I'm not sure why, but it may have to do with which fonts you have installed.
这篇关于在ggplot2和其他R图形中创建新的形状调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!