首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ggplot2:具有符号和形状的scale_fill_manual

ggplot2:具有符号和形状的scale_fill_manual
EN

Stack Overflow用户
提问于 2022-07-17 01:25:14
回答 1查看 119关注 0票数 0

如何在ggplot2中分配圆圈和周期形状?现在,我可以做两个形状或两个符号,但两者都不能。

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-17 01:38:54

基于这个comment,我认为你不能混合符号和数字引用。但是(相同的注释)显示了所有可能的形状及其对应的数字。周期为46,圆为1。

使用所需形状的数字:

代码语言:javascript
运行
复制
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)) 

我喜欢使用命名字符向量来定义每个组的手动形状。

代码语言:javascript
运行
复制
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,那么您可以通过以下方法实现这一点:

代码语言:javascript
运行
复制
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"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73008693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档