【问题标题】:Color and shape coding within ggplotggplot 中的颜色和形状编码
【发布时间】:2020-07-30 17:02:02
【问题描述】:

使用化学数据集,我想要做的是根据采样深度对 geom_points 进行颜色编码,然后根据采样时间制作形状。我还想在所有 geom_points 上添加一个细黑色边框以区分它们。

这是一个示例表:

    ID   Depth(m)   Sampling Date   Cl    Br
    1       1             May       4.0   .05
    2       1             June      5.0   .07
    3       2             May       6.0   .03
    4       2             June      7.0   .05
    5       3             May       8.0   .01
    6       3             June      9.0   .03
    7       4             May       10.0  .00
    8       4             June      11.0  .01

我正在尝试使用代码

    graph <- df %>%
    ggplot(aes(x = Cl, y = Br, fill = Depth, shape = Sampling Date), color = black) +
    geom_point(shape = c(21:24, size = 4) +
    labs(x = "Cl", y = "Br")

    graph

但每次我这样做时,它只会填充黑色形状而忽略颜色规范。我还需要使用形状 21:25 但每次我尝试指定形状的数量时,它总是说它与我的数据集中的变量数量不匹配。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您的代码充满了……挑战。 删除所有空格!这让你的生活更轻松。还将形状 aes 添加到 geom_point 并使用 scale 调用指定形状。

    library(ggplot2)
    df <- read.table(text = "ID   Depth   SamplingDate   Cl    Br
        1       1             May       4.0   .05
        2       1             June      5.0   .07
        3       2             May       6.0   .03
        4       2             June      7.0   .05
        5       3             May       8.0   .01
        6       3             June      9.0   .03
        7       4             May       10.0  .00
        8       4             June      11.0  .01", header = T)
    
    
      ggplot(df, aes(x = Cl, y = Br, fill = Depth, shape = SamplingDate)) +
      geom_point(aes(shape = SamplingDate), size = 4) +
        scale_shape_manual(values = 21:24)
    

    reprex package (v0.3.0) 于 2020 年 7 月 30 日创建

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多