如何在ggplot2中分配圆圈和周期形状?现在,我可以做两个形状或两个符号,但两者都不能。
data.frame(
x = rnorm(10),
y = rnorm(10),
group = gl(2, 5, labels = c("circle", "period"))
) %>%
ggplot(aes(x = x, y = y, shape = group)) +
geom_point(size = 4) +
# scale_shape_manual(values = c("a", ".")) + # okay
# scale_shape_manual(values = c("circle", "square")) + # okay
scale_shape_manual(values = c("circle", ".")) # error发布于 2022-07-17 01:38:54
基于这个comment,我认为你不能混合符号和数字引用。但是(相同的注释)显示了所有可能的形状及其对应的数字。周期为46,圆为1。
使用所需形状的数字:
data.frame(
x = rnorm(10),
y = rnorm(10),
group = gl(2, 5, labels = c("circle", "period"))
) %>%
ggplot(aes(x = x, y = y, shape = group)) +
geom_point(size = 4) +
scale_shape_manual(values = c(1, 46)) 我喜欢使用命名字符向量来定义每个组的手动形状。
plotting_shapes <- c("group_1" = 1,
"group_2" = 46)
data.frame(
x = rnorm(10),
y = rnorm(10),
group = gl(2, 5, labels = c("group_1", "group_2"))
) %>%
ggplot(aes(x = x, y = y, shape = group)) +
geom_point(size = 4) +
scale_shape_manual(values = plotting_shapes)因此,如果您真的想使用"."来引用shape 46,那么您可以通过以下方法实现这一点:
plotting_shapes <- c("circle" = 1,
"." = 46)
data.frame(
x = rnorm(10),
y = rnorm(10),
group = gl(2, 5, labels = c("group_1", "group_2"))
) %>%
ggplot(aes(x = x, y = y, shape = group)) +
geom_point(size = 4) +
scale_shape_manual(values = plotting_shapes[c("circle", ".")] %>%
unname()) # unname() is required because
# the names don't correspond to
# the group names "group_1" and "group_2"https://stackoverflow.com/questions/73008693
复制相似问题