我有一个ggplot,我想在其中添加一条连接均值的线。然而,我一直收到一个错误消息:"geom_path:每个组只包含一个观察值。您需要调整组美学吗?“
我尝试了这里建议的解决方案,但这些似乎在几年前就不起作用了。因此,我开了一个新的帖子。
#some packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(here, readr, cowplot, tidyr, ggplot2, dplyr)
#some functions from https://github.com/RainCloudPlots/RainCloudPlots
source("R_rainclouds.R")
source("summarySE.R")
source("simulateData.R")
#some data
df3 <- structure(list(participant = c(1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L), condition = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("RT_Cau", "RT_Moro",
"RT_Asi"), class = "factor"), RT = c(1.44248448543333, 2.73934002973517,
1.89927013090706, 1.32510448686595, 2.44101598069973, 2.84290772015973,
1.19419819108836, 2.12124958877175, 1.14707311860052, 2.72286767178203,
1.15914495363538, 1.5340050993702, 1.62616192435053, 1.32694796283192,
1.2720800304128, 0.99275928310549, 1.04329096409593, 1.43288644582691,
1.60302970699442, 1.3393626055176, 1.24088162033185, 2.42448868318791,
1.6398716779282, 1.53816275909702, 1.51033130413559, 3.226993255043,
2.1915727996463, 1.39240057519678, 3.0538809712989, 2.52658416881183,
1.16366335020089, 2.33377114484134, 1.39357978132538, 2.691606623485,
1.21999657945028, 1.72195011524003, 1.38834235226937, 1.44350802586345,
1.29563539425317, 0.909762618509679, 1.13583585924538, 1.58240957515452,
1.82142351906117, 1.3644415734435, 1.32141664778601, 2.23277562688125,
1.5773976029336, 1.43200172590417, 1.68991681725, 2.9617422858462,
1.60886625604519, 1.38647850513866, 3.46156610375971, 2.96950698342897,
1.17905107770577, 2.36256332626113, 1.31254065801458, 3.204902618708,
1.21067325368702, 1.80371515914087, 1.57816183853565, 1.40761655308155,
1.27304559913463, 1.07621914272144, 1.04203150853998, 1.58958820979388,
1.79859778873147, 1.19249820050996, 1.4116357628608, 2.15806795062162,
1.70597872926531, 1.66135756110131)), row.names = c(NA, -72L), class = "data.frame")
#make a summary of the data
df4 <- summarySE(df3, measurevar = "RT", groupvars = c("condition"))
#a working plot that shows dots, boxplot, distribution, and mean+SE
#I want to have lines connecting the mean dots.
ggplot(df3,aes(x=condition,y=RT,fill=condition,col=condition))+
geom_flat_violin(position = position_nudge(x = .2, y = 0), alpha = .6,adjust =4)+
geom_point(aes(x = as.numeric(condition)-.15, y = RT, colour = condition),position = position_jitter(width = .05), size = .25, shape = 20)+
geom_boxplot(aes(x = condition, y = RT, fill = condition),outlier.shape = NA, alpha = .5, width = .1, colour = "black") +
geom_point(data = df4, aes(x = as.numeric(condition)+.1, y = RT_mean, group = condition, colour = condition), shape = 18) +
geom_errorbar(data = df4, aes(x = as.numeric(condition)+.1, y = RT_mean, group = condition, colour = condition, ymin = RT_mean-se, ymax = RT_mean+se), width = .05) +
ylab('RT')+
scale_fill_brewer(palette = "Dark2")+scale_colour_brewer(palette = "Dark2")+
guides(fill = FALSE, col = FALSE) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "transparent",colour = NA),
plot.background = element_rect(fill = "transparent",colour = NA)
) # +
# geom_line(data = df4, aes(x = as.numeric(condition)+.1, #y = RT_mean, group = condition, colour = condition), #linetype = 3)
#The commented outpart is my attempt to connect it with lines, which produces the described error.
#If you do not want to load the package from github, here is the raw code of the summariseSE function:
# summarySE function
summarySE <- function(data = NULL, measurevar, groupvars = NULL, na.rm = FALSE,
conf.interval = .95, .drop = TRUE) {
library(plyr)
# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function(x, na.rm = FALSE) {
if (na.rm) {
sum(!is.na(x))
} else {
length(x)
}
}
# This does the summary. For each group's data frame, return a vector with
# N, mean, median, and sd
datac <- plyr::ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N = length2(xx[[col]], na.rm=na.rm),
mean = mean(xx[[col]], na.rm=na.rm),
median = median(xx[[col]], na.rm=na.rm),
sd = sd(xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# Rename the "mean" and "median" columns
datac <- plyr::rename(datac, c("mean" = paste(measurevar, "_mean", sep = "")))
datac <- plyr::rename(datac, c("median" = paste(measurevar, "_median", sep = "")))
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
ciMult <- qt(conf.interval / 2 + .5, datac$N - 1)
datac$ci <- datac$se * ciMult
return(datac)
}最终的曲线图应该有连接均值的线,类似于https://wellcomeopenresearch.org/articles/4-63/v2的曲线图11。
谢谢你的帮助!
发布于 2021-01-23 00:05:38
这可能很有用:
#Code
ggplot(df3,aes(x=condition,y=RT,fill=condition,col=condition))+
geom_flat_violin(position = position_nudge(x = .2, y = 0),
alpha = .6,adjust =4)+
geom_point(aes(x = as.numeric(condition)-.15, y = RT,
colour = condition),
position = position_jitter(width = .05), size = .25, shape = 20)+
geom_boxplot(aes(x = condition, y = RT, fill = condition),
outlier.shape = NA, alpha = .5,
width = .1, colour = "black") +
geom_point(data = df4, aes(x = as.numeric(condition)+.1,
y = RT_mean,
group = condition, colour = condition), shape = 18) +
geom_errorbar(data = df4, aes(x = as.numeric(condition)+.1, y = RT_mean, group = condition, colour = condition, ymin = RT_mean-se, ymax = RT_mean+se), width = .05) +
ylab('RT')+
scale_fill_brewer(palette = "Dark2")+scale_colour_brewer(palette = "Dark2")+
guides(fill = FALSE, col = FALSE) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "transparent",colour = NA),
plot.background = element_rect(fill = "transparent",colour = NA)
) +
geom_line(data = df4, aes(x = as.numeric(condition)+.1,
y = RT_mean, group = 1)输出:

https://stackoverflow.com/questions/65846618
复制相似问题