【问题标题】:creating a scatter plot using ggplot2 in r在 r 中使用 ggplot2 创建散点图
【发布时间】:2022-11-25 02:05:46
【问题描述】:
class_day <- c(1:10)  
control_group <- c(67,72,69,81,73,66,71,72,77,71) 
A_treatment_group <- c(NA,72,77,81,73,85,69,73,74,77)
B_treatment_group <- c(NA,66,68,69,67,72,73,75,79,77) 
class.df<-data.frame(class_day, control_group, A_treatment_group, B_treatment_group)

我试图将向量转换为表格,但我不确定如何在一个图中包含三个类别。

如何获得三种不同颜色的散点图? 我想将 x 轴设置为上面的 class_day,将 y 轴设置为分数。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    首先,一种更简洁的方法来制作没有中间变量的数据框。

    您可以通过将数据旋转为“长”形式来制作此类图表:

    class.df<-data.frame(class_day = c(1:10),
                         control_group     = c(67,72,69,81,73,66,71,72,77,71), 
                         A_treatment_group = c(NA,72,77,81,73,85,69,73,74,77),
                         B_treatment_group = c(NA,66,68,69,67,72,73,75,79,77) )
    library(tidyverse)
    class.df %>% 
      pivot_longer(!class_day) %>% 
      ggplot(aes(x=class_day, y=value, color=name))+
      geom_point()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      相关资源
      最近更新 更多