在 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屋!