【问题标题】:ggplot2 multiple geom_point in a single plotggplot2 单个图中的多个 geom_point
【发布时间】:2021-01-15 17:29:33
【问题描述】:

我想使用geom_point()group 变量的10 个级别上分别可视化yABC 的关系。

我当前的情节只能针对A 绘制y,但我希望BC 在每个情节中以不同的颜色显示。

我想知道如何在ggplot2 中实现这一点?

library(ggplot2)

dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/sng.csv')

ggplot(dat)+aes(x=A, y = y, fill = group)+geom_point()+ # How can I have `B` and `C` next to `A` with other colors
  facet_wrap(~group)

【问题讨论】:

    标签: r ggplot2 plot data-visualization


    【解决方案1】:

    我们可以从ggpubr使用ggscatter

    library(ggpubr)
    library(purrr)
    ggscatter(dat, x = c("A", "B", "C"), y = "y", color = "group", palette = "jco")  %>% 
          invoke(ggarrange, ., ncol = 2, nrow = 2)
    

    -输出

    【讨论】:

      【解决方案2】:

      尝试标准方法:获取长格式数据。

      library(ggplot2)
      
      dat %>%
        tidyr::pivot_longer(cols = A:C) %>%
        ggplot()+aes(x=value, y = y, color = name) + 
        geom_point() + facet_wrap(~group)
      

      【讨论】:

      • 分分我可以想到colorsizeshape,还有什么可以分分的吗?
      • 是的,我认为这些已经足够分开了。
      猜你喜欢
      • 2020-05-06
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多