【问题标题】:Using ggplot in R to create a line graph for two different groups在 R 中使用 ggplot 为两个不同的组创建折线图
【发布时间】:2018-07-09 20:23:25
【问题描述】:

我正在尝试创建一个线图,描绘两组/条件随时间推移的不同轨迹。我有两组在五个时间点(1、2、3、4、5)收集数据“吃”。 我希望这些线连接每个组在五个时间点的平均点,所以我在时间 1 有两个点,在时间 2 有两个点,依此类推。

这是一个可重现的例子:

#Example data
library(tidyverse)
library(ggplot2)
eat <- sample(1:7, size = 30, replace = TRUE)
df <- data.frame(id = rep(c(1, 2, 3, 4, 5, 6), each = 5),
                 Condition = rep(c(0, 1), each = 15),
                 time = c(1, 2, 3, 4, 5),
                 eat = eat
)
df$time <- as.factor(df$time)
df$Condition <- as.factor(df$Condition)

#Create the plot.
library(ggplot2)
ggplot(df, aes(x = time, y = eat, fill = Condition)) + geom_line() +
  geom_point(size = 4, shape = 21) +
  stat_summary(fun.y = mean, colour = "red", geom = "line")

问题是,我需要我的线条水平移动(即显示两条不同颜色的线条在 x 轴上移动)。但是这段代码只是垂直连接点:

如果我不将Time 转换为一个因子,而只将Condition 转换为一个因子,我会得到一堆乱码。同样的事情也发生在我的实际数据中。

我希望它在美学上看起来像这样,透明的错误信封包裹着每一行。但是,我不希望它是弯曲的,我希望线条是直的,连接每个点的手段。

【问题讨论】:

  • factors 是离散的。您似乎想要一个连续的 x 轴,所以一个好的步骤是time 转换为一个因子。
  • 请说明您希望如何计算“错误信封”。
  • 我希望有一种方法可以计算 +/-1 标​​准误差(或标准差)。
  • 我也对您数据中“id”的含义感到困惑。你想要的两条线是什么 - 每个条件一条线? id 需要在剧情中说明吗?
  • 哦,不,id是参与者的id。它不需要被绘制。是的,我想要两行 - 每个条件一行。 x=时间,y=吃。

标签: r ggplot2 linegraph


【解决方案1】:

这是通过每次均值的直线段,范围设置为当时点的标准偏差。一个stat.summarycolour 美学相得益彰,另一个则使用继承的fill 美学形成该区域。 ggplot2::mean_se 是一个方便的函数,它接受一个向量并返回一个具有平均值和 +/- 一些标准误差的数据帧。这是stat_summaryfun.data 参数的正确格式,它将这些值传递给指定的geom。在这里,geom_ribbon 接受 yminymax 值以在图表上绘制一条丝带。

library(tidyverse)
set.seed(12345)
eat <- sample(1:7, size = 30, replace = T)
df <- data.frame(
  Condition = rep(c(0, 1), each = 15),
  time = c(1, 2, 3, 4, 5),
  eat = eat
)
df$Condition <- as.factor(df$Condition)

ggplot(df, aes(x = time, y = eat, fill = Condition)) +
  geom_point(size = 4, shape = 21, colour = "black") +
  stat_summary(geom = "ribbon", fun.data = mean_se, alpha = 0.2) +
  stat_summary(
    mapping = aes(colour = Condition),
    geom = "line",
    fun.y = mean,
    show.legend = FALSE
    )

reprex package (v0.2.0) 于 2018 年 7 月 9 日创建。

【讨论】:

  • 打败我! (只需在第二个stat_summary 中添加show.legend = FALSE 我会投票);)
  • @Calum 你太棒了——太棒了。谢谢!不过有一个问题:我猜 'stat_summary(fun.data = mean_se, geom = "ribbon", alpha = 0.2)' 在每一行周围创建标准错误功能区?我不太明白每行如何定义从哪个组计算平均值和 SE?
  • 添加了更多解释。我建议您使用geom_ribbon 来看看它是如何工作的。通常,先进行转换然后应用几何数据更简单,但统计信息可让您一次性完成所有操作。
【解决方案2】:

这是我对你想要的最好的猜测:

# keep time as numeric
df$time = as.numeric(as.character(df$time))
ggplot(df, aes(x = time, y = eat, group = Condition)) +
    geom_smooth(
        aes(fill = Condition, linetype = Condition),
        method = "lm",
        level = 0.65,
        color = "black",
        size = 0.3
    ) +
    geom_point(aes(color = Condition))

设置level = 0.65 大约是线性模型拟合的 +/- 1 个标准差。

【讨论】:

    【解决方案3】:

    我认为这段代码可以帮助你完成大部分工作

      library(tidyverse)
    
      eat <- sample(1:7, size = 30, replace = TRUE)  
      tibble(id = rep(c(1, 2, 3, 4, 5, 6), each = 5),             
             Condition = factor(rep(c(0, 1), each = 15)),
             time = factor(rep(c(1, 2, 3, 4, 5), 6)),
             eat = eat) %>%
      ggplot(aes(x = time, y = eat, fill = Condition, group = Condition)) +
      geom_point(size = 4, shape = 21) +
      geom_smooth()
    

    geom_smooth 是你要找的,我想。这会从点中创建一个线性模型,只要您的 x 值是一个因素,它就应该使用平均值并以这种方式连接点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多