绘制数据集的均值,其中每一列都是不同的一天
Plot means of a dataset where each column is a different day
嗨,我有这个可能是新手的问题,但我已经有一段时间没有接触 R 了,
我有这个大数据集,其中每一列都是一天(第 1 天、第 2 天等)的测量结果,而行是不同处理的重复。我已经设法用所有方法制作了一个 table 并将数据减少到我将治疗作为行和每天的平均值作为列的位置。现在我想将这些数据绘制为散点图或线条,但我似乎没有确定要用作 aes(x=)
的内容,有没有办法使用单个代码将它们全部绘制出来而不是添加每个 geom_point()
每天和治疗。
下面是一个例子,因为数据更长更复杂(24 天,28 次处理,每 10 次重复)
我怎样才能将数据绘制成这样(Excel 图片)
预先感谢大家,我们将不胜感激任何帮助或反馈
#df#
treatment day 1 day 2 day 3
1 t1 7.524814 8.330983 6.639391
2 t1 6.056334 6.138648 5.439239
3 t2 4.377818 4.964445 3.990593
4 t1 6.834753 7.070450 5.895462
5 t3 7.378768 8.375725 7.210010
6 t2 4.104087 4.942359 3.589360
7 t2 4.520651 4.775113 3.753422
8 t3 7.875438 8.543303 8.101697
9 t3 7.803648 8.232132 7.073342
mean<-aggregate(df[,2:4],list(df$treatment),mean)
sd<-aggregate(df[,2:4],list(df$treatment),sd)
#mean#
Group.1 day 1 day 2 day 3
1 t1 6.805300 7.180027 5.991364
2 t2 4.334185 4.893972 3.777792
3 t3 7.685951 8.383720 7.461683
ggplot()+geom_point(mean,aes(x=??,y=mean$"day 1")
ggplot
喜欢“长”格式的数据。这是对 mean
值执行此操作的一种方法,您可以对 sd
.
执行相同的操作
library(tidyverse)
df %>%
pivot_longer(cols = -treatment) %>%
group_by(treatment, name = factor(name, unique(name))) %>%
summarise(value = mean(value), .groups = 'drop') %>%
ggplot(aes(name, value, color = treatment, group = treatment)) + geom_line()
您可以通过多种方式完成任务:
- 以长格式提供您的数据。
- 一些数据争论
ggplot()
版本 1:
library(tidyverse)
df %>%
pivot_longer(
cols = -treatment,
names_to = "day",
values_to = "values"
) %>%
group_by(treatment, day) %>%
summarise(mean = mean(values)) %>%
ggplot(aes(x=day, y=mean, color=treatment, group=treatment)) +
geom_line()
版本 2
library(tidyverse)
df %>%
pivot_longer(
cols = -treatment,
names_to = "day",
values_to = "values"
) %>%
group_by(day) %>%
summarise(mean = mean(values)) %>%
ggplot(aes(x=day, y=mean, group=1)) +
geom_point() +
geom_line(colour="red")
嗨,我有这个可能是新手的问题,但我已经有一段时间没有接触 R 了,
我有这个大数据集,其中每一列都是一天(第 1 天、第 2 天等)的测量结果,而行是不同处理的重复。我已经设法用所有方法制作了一个 table 并将数据减少到我将治疗作为行和每天的平均值作为列的位置。现在我想将这些数据绘制为散点图或线条,但我似乎没有确定要用作 aes(x=)
的内容,有没有办法使用单个代码将它们全部绘制出来而不是添加每个 geom_point()
每天和治疗。
下面是一个例子,因为数据更长更复杂(24 天,28 次处理,每 10 次重复)
我怎样才能将数据绘制成这样(Excel 图片)
#df#
treatment day 1 day 2 day 3
1 t1 7.524814 8.330983 6.639391
2 t1 6.056334 6.138648 5.439239
3 t2 4.377818 4.964445 3.990593
4 t1 6.834753 7.070450 5.895462
5 t3 7.378768 8.375725 7.210010
6 t2 4.104087 4.942359 3.589360
7 t2 4.520651 4.775113 3.753422
8 t3 7.875438 8.543303 8.101697
9 t3 7.803648 8.232132 7.073342
mean<-aggregate(df[,2:4],list(df$treatment),mean)
sd<-aggregate(df[,2:4],list(df$treatment),sd)
#mean#
Group.1 day 1 day 2 day 3
1 t1 6.805300 7.180027 5.991364
2 t2 4.334185 4.893972 3.777792
3 t3 7.685951 8.383720 7.461683
ggplot()+geom_point(mean,aes(x=??,y=mean$"day 1")
ggplot
喜欢“长”格式的数据。这是对 mean
值执行此操作的一种方法,您可以对 sd
.
library(tidyverse)
df %>%
pivot_longer(cols = -treatment) %>%
group_by(treatment, name = factor(name, unique(name))) %>%
summarise(value = mean(value), .groups = 'drop') %>%
ggplot(aes(name, value, color = treatment, group = treatment)) + geom_line()
您可以通过多种方式完成任务:
- 以长格式提供您的数据。
- 一些数据争论
ggplot()
版本 1:
library(tidyverse)
df %>%
pivot_longer(
cols = -treatment,
names_to = "day",
values_to = "values"
) %>%
group_by(treatment, day) %>%
summarise(mean = mean(values)) %>%
ggplot(aes(x=day, y=mean, color=treatment, group=treatment)) +
geom_line()
版本 2
library(tidyverse)
df %>%
pivot_longer(
cols = -treatment,
names_to = "day",
values_to = "values"
) %>%
group_by(day) %>%
summarise(mean = mean(values)) %>%
ggplot(aes(x=day, y=mean, group=1)) +
geom_point() +
geom_line(colour="red")