我有这个假数据框。我正在寻找一种更快的矢量化,以便在平均值的条形图上添加数据点。当存在许多列时,我的解决方案很难应用。我的问题是在“点”函数中只允许一个向量而不是一个矩阵。你有智能解决方案吗?
df <- data.frame(Test = 1:5,
Factor= c("A","A","B","B","A"),
V1=c(3.2,5.4,6.0,6.5,2),
V2=c(5,5,8.6,7,1))
str(df, list.len=ncol(df))
colnames(df)
dim(df)
df.agg <- aggregate(df[c(3,4)], by = list(Factor = df$Factor), mean)
df.agg <- df.agg[order(df.agg$Factor),]
df.agg
mat.agg <- as.matrix(df.agg[c(2,3)])
barx <- barplot(mat.agg,
beside = T,
ylim = c(0, 1.3*max(mat.agg)),
col = colors()[c(5,16)][df.agg$Factor],
legend.text = as.character(df.agg$Factor))
barx
barx <- as.vector(barx)
barx
points(
rep(barx[1], length(df[df$Factor == levels(df$Factor)[1], "V1"])),
df[df$Factor == levels(df$Factor)[1], "V1"])
points(
rep(barx[2], length(df[df$Factor == levels(df$Factor)[2], "V1"])),
df[df$Factor == levels(df$Factor)[2], "V1"])
points(
rep(barx[3], length(df[df$Factor == levels(df$Factor)[1], "V2"])),
df[df$Factor == levels(df$Factor)[1], "V2"])
points(
rep(barx[4], length(df[df$Factor == levels(df$Factor)[2], "V2"])),
df[df$Factor == levels(df$Factor)[2], "V2"])
答案 0 :(得分:1)
您可以尝试使用tidyverse
:
library(tidyverse)
df %>%
gather(key, value, -Test, -Factor) %>%
ggplot(aes(x = key, y = value, fill=Factor)) +
geom_bar(stat = "summary", fun.y = "mean",position = "dodge") +
geom_point(position=position_dodge(0.9))
在基地R我会这样做:
library(reshape2)
df_wide <- melt(df[,-1]) # make your data wide
df_wide <- df_wide[ order(df_wide$variable,df_wide$Factor),] # order appropriate
# add the x-positions using interaction
df_wide$X <- barx[as.numeric(interaction(df_wide$Factor, df_wide$variable))]
# add the points to the bars
points(df_wide$X, df_wide$value)