r-绘图时的错误意味着数字x轴使用geom_point



这里是我的复制数据帧的一个子集。我试图画出一个数字的、非因子的x轴的点,然后是点的平均值。

structure(list(SITE_ID = c("AZ1", "AZ2", 
"AZ3", "AZ4", "AZ5", 
"AZ6", "AZ7", "AZ8", 
"AZ9", "AZ10", "KS1", "KS2", 
"KS3", "KS4", "KS5", "KS6", "KS7", "KS8", "KS9", "KS10", "KS11", 
"NMEX4", "NMEX3", "NMEX2", 
"NMEX1"), DATASET = c("AZ_B", "AZ_B", 
"AZ_B", "AZ_B", "AZ_B", "AZ_B", "AZ_B", "AZ_B", 
"AZ_B", "AZ_B", "KS_B", "KS_B", "KS_B", "KS_B", 
"KS_B", "KS_B", "KS_B", "KS_B", "KS_B", "KS_B", 
"KS_B", "NM_B", "NM_B", "NM_B", "NM_B"), num_temp_reps = c(7L, 
7L, 6L, 7L, 8L, 6L, 7L, 7L, 8L, 8L, 10L, 10L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 8L, 7L, 6L, 13L), stdv = c(0.0834575651234484, 
0.0611317465539431, 0.0965091506381606, 0.0805498856928655, 0.114537557812694, 
0.116984871610183, 0.0843571657410382, 0.0917016831235985, 0.106580906285764, 
0.0820200212513281, 0.0671432513890294, 0.034230833394556, 0.0660673914191362, 
0.0975109600124084, 0.0278177652794605, 0.162136833740925, 0.144981840255872, 
0.0529289869310672, 0.0555414059402315, 0.0722218413310142, 0.0654033698276827, 
0.0714675814159411, 0.0387847054136062, 0.13519289227636, 0.0379318889746079
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 110L, 
111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 65L, 
66L, 67L, 68L), class = "data.frame")

这会抛出一个错误

library(ggplot2)
library(ggpubr)
library(ggpmisc)
df_means <- df %>% 
group_by(DATASET,num_temp_reps) %>% 
summarise(mean_stdv=mean(stdv))
ggplot(data=df, 
aes(x=round(num_temp_reps), 
y=stdv)) + 
geom_point() + 
geom_point(data=df_means,col="red") +
geom_smooth(method="lm",se=F) + 
stat_fit_glance(method = "lm",
method.args = list(formula = (y) ~ (x)),
aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
stat(r.squared), stat(p.value))),
parse = TRUE) +
scale_color_viridis_d() + facet_wrap(~DATASET,scales="free_x")
Error in FUN(X[[i]], ...) : object 'stdv' not found
ggplot(data=df, 
aes(x=round(num_temp_reps), 
y=stdv)) + 
geom_point() + 
geom_point(data=df_means,aes(x=round(num_temp_reps), 
y=mean_stdv),col="red") +
geom_smooth(method="lm",se=F) + 
stat_fit_glance(method = "lm",
method.args = list(formula = (y) ~ (x)),
aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
stat(r.squared), stat(p.value))),
parse = TRUE) +
scale_color_viridis_d() + facet_wrap(~DATASET,scales="free_x")

并在这里进行了修复。我不得不添加means df的具体列信息。起初,我认为这个错误是因为我用来总结y轴值的组是数字而不是因子。然而,事实并非如此!

最新更新