如何在bar chat中为多个变量创建错误条
我希望有人能帮我解决以下问题: 我试图制作一个组合条形图,显示为二元变量(性别)记录的3个不同连续变量(体温、长度、质量)的平均值和标准误差 我已经能够绘制每个变量的平均值,但我似乎无法使用我尝试过的任何代码成功地计算这3个变量的标准误差。 我尝试了很多方法,但我认为我的方法是正确的:如何在bar chat中为多个变量创建错误条,r,ggplot2,bar-chart,standard-error,errorbar,R,Ggplot2,Bar Chart,Standard Error,Errorbar,我希望有人能帮我解决以下问题: 我试图制作一个组合条形图,显示为二元变量(性别)记录的3个不同连续变量(体温、长度、质量)的平均值和标准误差 我已经能够绘制每个变量的平均值,但我似乎无法使用我尝试过的任何代码成功地计算这3个变量的标准误差。 我尝试了很多方法,但我认为我的方法是正确的: View(test4) test4 <- aggregate(test4, by = list(Sex = test4$Sex), F
View(test4)
test4 <- aggregate(test4,
by = list(Sex = test4$Sex),
FUN = function(x) c(mean = mean(x), sd = sd(x),
n = length(x)))
test4
#this produced mean, sd, length for ALL variables (including sex)
test4<-do.call(test4)
test4$se<-test4$x.sd / sqrt(test4$x.n)
我试图在聚合(test4…)后重新编码以针对我的3个变量,但我无法让它工作…然后我通过生成的数据帧进行子集以排除sex,但这不起作用。然后我试着把它定义为一个矩阵或向量,但仍然不起作用
我希望我的最后一张图表是y轴=平均值,x轴=变量(3个子组(Tb、质量、长度),两条横线并排显示男性和女性值,以便进行比较
任何人能提供的任何帮助或指导都将不胜感激
非常感谢!:)
aggregate
在您尝试输出多个列时,确实会提供一些疯狂的输出。
如果您希望使用aggregate
,我将使用mean和SE作为对aggregate
的单独调用
然而,这里有一个使用tidyr和dplyr的解决方案,我认为这并不太糟糕
我已经创建了一些数据。我希望它看起来像你的。在你的问题中加入一个模拟数据集是非常有用的
library(tidyr)
library(dplyr)
library(ggplot2)
# Create some data
test4 <- data.frame(Sex = rep(c('M', 'F'), 50),
bodytemp = rnorm(100),
length = rnorm(100),
mass = rnorm(100))
# Gather the data to 'long' format so the bodytemp, length and mass are all in one column
longdata <- gather(test4, variable, value, -Sex)
head(longdata)
# Create the summary statistics seperately for sex and variable (i.e. bodytemp, length and mass)
summary <- longdata %>%
group_by(Sex, variable) %>%
summarise(mean = mean(value), se = sd(value) / length(value))
# Plot
ggplot(summary, aes(x = variable, y = mean, fill = Sex)) +
geom_bar(stat = 'identity', position = 'dodge') +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
width = 0.2,
position = position_dodge(0.9))
library(tidyr)
图书馆(dplyr)
图书馆(GG2)
#创建一些数据
测试4
更新:我能够通过将timcdlucas脚本的初始部分与我在绘制一个输出时使用的另一部分结合起来来回答我的问题。对于可能正在寻求类似问题答案的任何其他人,我已经发布了我的脚本和结果图(见上面的链接):
View(test3)#此数据帧被组织为“sex”、“tb”、“mass”、“svl”
newtest目前这读起来像是关于聚合的问题,而不是关于绘图的问题。对于密谋,您可以尝试玩一些类似的东西。谢谢您的帮助!不幸的是,当我遵循这个脚本时,它没有生成我想要的图形(可能是因为我们使用的是不同的数据集),但它确实让我开始长期组织我的数据,然后我能够将它与我在只有一个输出时使用的另一个脚本连接起来。我一定会包括一个数据集下一次!再次感谢您的帮助:)
library(tidyr)
library(dplyr)
library(ggplot2)
# Create some data
test4 <- data.frame(Sex = rep(c('M', 'F'), 50),
bodytemp = rnorm(100),
length = rnorm(100),
mass = rnorm(100))
# Gather the data to 'long' format so the bodytemp, length and mass are all in one column
longdata <- gather(test4, variable, value, -Sex)
head(longdata)
# Create the summary statistics seperately for sex and variable (i.e. bodytemp, length and mass)
summary <- longdata %>%
group_by(Sex, variable) %>%
summarise(mean = mean(value), se = sd(value) / length(value))
# Plot
ggplot(summary, aes(x = variable, y = mean, fill = Sex)) +
geom_bar(stat = 'identity', position = 'dodge') +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
width = 0.2,
position = position_dodge(0.9))
View(test3) #this dataframe was organized as 'sex', 'tb', 'mass', 'svl'
newtest<-test3
View(newtest)
#transform data to 'long' combining all variables in one column
longdata<-gather(newtest, variable, value, -Sex)
View(longdata)
#set up table in correct format
longdata2 <- aggregate(longdata$value,
by = list(Sex = longdata$Sex, Variable = longdata$variable),
FUN = function(x) c(mean = mean(x), sd = sd(x),
n = length(x)))
longdata2 <- do.call(data.frame, longdata2)
longdata2$se<-longdata2$x.sd / sqrt(longdata2$x.n)
colnames(longdata2)<-c("Sex", "Variable", "mean", "sd", "n", "se")
longdata2$names<-c(paste(longdata2$Variable, "Variable /", longdata2$Sex, "Sex"))
View(longdata2)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = longdata3$mean + longdata3$se,
ymin = longdata3$mean - longdata3$se)
#To order the bars in the way I desire *might not be necessary for future scripts*
positions<-c("Tb", "SVL", "Mass")
#To plot new table:
bfinal <- ggplot(data = longdata3, aes(x = factor(Variable), y = mean,
fill = factor(Sex)))+
geom_bar(stat = "identity",
position = position_dodge(0.9))+
geom_errorbar(limits, position = position_dodge(0.9),
width = (0.25)) +
labs(x = "Variable", y = "Mean") +
ggtitle("")+
scale_fill_discrete(name = "",
labels=c("Male", "Female"))+
scale_x_discrete(breaks=c("Mass", "SVL", "Tb"),
labels=c("Mass", "SVL", "Tb"),
limits=(positions))
bfinal