R 将列的平均值添加为直线图中的点
我想以特定间隔将列的平均值添加为点/文本 我有这样一个情节: 我的数据帧由以下数据组成:R 将列的平均值添加为直线图中的点,r,ggplot2,plot,R,Ggplot2,Plot,我想以特定间隔将列的平均值添加为点/文本 我有这样一个情节: 我的数据帧由以下数据组成: structure(list(optz = c(1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 106
structure(list(optz = c(1067, 1067, 1067, 1067, 1067, 1067, 1067,
1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067,
1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067,
1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067,
1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067,
1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067, 1067), e_val = c(6.1565,
5.4915, 4.647, 3.7871, 5.2397, 5.8252, 4.647, 5.8252, 4.5617,
4.3906, 5.0711, 5.9082, 4.7321, 4.9866, 4.8171, 3.7003, 5.4078,
4.0467, 4.7321, 4.647, 4.647, 5.0711, 4.0467, 4.9019, 5.3238,
4.9866, 5.2397, 4.0467, 5.2397, 5.3238, 4.7321, 3.5262, 6.1565,
3.4389, 5.6587, 5.742, 4.7321, 5.3238, 4.7321, 4.647, 4.0467,
5.0711, 4.4762, 4.5617, 4.9019, 4.3906, 3.2638, 4.8171, 3.2638,
4.647, 4.3049, 3.5262, 4.3906, 4.1329, 4.4762, 4.0467, 5.1555,
5.1555, 3.3514, 3.3514), Ctime = c(100.8656, 105.7428, 113.8558,
108.7743, 103.1432, 111.9795, 108.8254, 107.0829, 99.2327, 100.002,
88.4304, 105.3633, 106.6242, 98.2432, 102.5163, 110.1165, 95.3913,
95.3288, 108.5169, 114.2813, 152.393, 164.0396, 135.2379, 161.8865,
162.6032, 139.7406, 163.0824, 167.3038, 155.7465, 166.0113, 159.9243,
147.9156, 153.5766, 160.7025, 168.5825, 138.6539, 140.4861, 135.3083,
153.4059, 152.83, 214.338, 214.0513, 192.4158, 203.7792, 216.6077,
202.8908, 205.3991, 216.3706, 215.6574, 210.9652, 190.4783, 182.6495,
205.7483, 199.6231, 194.0155, 203.9086, 196.9656, 205.351, 186.1295,
173.8961), step = c(20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L),
method = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("QV", "VN"), class = "factor")), row.names = 2:61, class = "data.frame")
我的代码是:
ggplot(data = df, aes(x = Ctime, y = e_val, group = method, color = method)) +
stat_smooth(data = df, level = 0.8) +
ggtitle('M1 vs M2 _ time')+
ylab('e_val')+
xlab('Ctime')
我想在轴x
上(动态)显示的间隔上添加step
列的平均值。
例如,对于轴x上显示的自动生成的间隔{120、160、200}
:
mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 120 == 0),]$step) # 20
mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 160 == 0),]$step) # 24.44
mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 200 == 0),]$step) # 28.88
mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 120 == 0),]$step) # 20
mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 160 == 0),]$step) # 22.85
mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 200 == 0),]$step) # 25.71
因此,所需的绘图应如下所示:
尝试这种方法,计算所需点的
leash()
,并使用这两个选项中的任何一个添加它们。关键是值。代码如下:
library(tidyverse)
#Code
p1 <- ggplot(data = df, aes(x = Ctime, y = e_val, group = method, color = method)) +
stat_smooth(data = df, level = 0.8) +
ggtitle('M1 vs M2 _ time')+
ylab('e_val')+
xlab('Ctime')
#Create labels
v1 <- mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 120 == 0),]$step) # 20
v2 <- mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 160 == 0),]$step) # 24.44
v3 <- mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 200 == 0),]$step) # 28.88
w1 <- mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 120 == 0),]$step) # 20
w2 <- mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 160 == 0),]$step) # 22.85
w3 <- mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 200 == 0),]$step) # 25.71
这样,我们计算位置和标签:
#Labels and positions 1
df1 <- data.frame(x=c(120,160,200),
y=pred1,
lab=c(v1,v2,v3))
df2 <- data.frame(x=c(120,160,200),
y=pred2,
lab=c(w1,w2,w3))
#Labels and positions 2
df1 <- data.frame(x=c(120,160,200),
y=pred1,
lab=c(v1,v2,v3),
method='VN')
df2 <- data.frame(x=c(120,160,200),
y=pred2,
lab=c(w1,w2,w3),
method='QV')
df3 <- rbind(df1,df2)
产出:
您尝试过注释功能吗?非常感谢这将非常有效,但问题是x轴上的点从一开始就不知道。这些是从R动态生成的。有没有办法从ggplot中获取信息,添加标签并计算点数?@PanosKal。我认为您可以使用scale\u y\u continuous()
中的限制和中断设置您自己的中断,然后输入黄土模型。这样,您就可以随心所欲地更改限制@帕诺斯卡尔。很抱歉,它是scale\u x\u continuous()
,您可以对定义的向量使用breaks
。我将添加一个更新。谢谢@Duck:)@PanosKal。永远是我的荣幸!只是个离题的问题。您个人资料中的图片,该图片来自何处?这是一个非常好的里昂!
#Labels and positions 1
df1 <- data.frame(x=c(120,160,200),
y=pred1,
lab=c(v1,v2,v3))
df2 <- data.frame(x=c(120,160,200),
y=pred2,
lab=c(w1,w2,w3))
#Labels and positions 2
df1 <- data.frame(x=c(120,160,200),
y=pred1,
lab=c(v1,v2,v3),
method='VN')
df2 <- data.frame(x=c(120,160,200),
y=pred2,
lab=c(w1,w2,w3),
method='QV')
df3 <- rbind(df1,df2)
#Option 1
q1 <- p1+annotate(geom = 'point',x=df1$x,y=df1$y,color='blue',size=3,)+
annotate(geom = 'point',x=df2$x,y=df2$y,color='red',size=3,)+
annotate(geom = 'text',x=df1$x,y=df1$y,label=round(df1$lab,2),
size=3,hjust=-0.5,color='cyan4',fontface='bold')+
annotate(geom = 'text',x=df2$x,y=df2$y,label=round(df2$lab,2),
size=3,hjust=-0.5,color='tomato',fontface='bold')
#Option 2
q1 <- p1+geom_point(data=df3,aes(x=x,y=y,group=method,color=method),size=3,show.legend = F)+
geom_text(data=df3,aes(x=x,y=y,group=method,label=round(lab,2)),
hjust=-0.5,size=3,fontface='bold',show.legend = F)
library(tidyverse)
#Code
mybreaks <- c(100,150,200)
#Plot
p1 <- ggplot(data = df, aes(x = Ctime, y = e_val, group = method, color = method)) +
stat_smooth(data = df, level = 0.8) +
scale_x_continuous(breaks = mybreaks)+
ggtitle('M1 vs M2 _ time')+
ylab('e_val')+
xlab('Ctime')
#Create labels
v1 <- mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 120 == 0),]$step) # 20
v2 <- mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 160 == 0),]$step) # 24.44
v3 <- mean(df[df$method=='QV' & (df$Ctime - df$Ctime %% 200 == 0),]$step) # 28.88
w1 <- mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 120 == 0),]$step) # 20
w2 <- mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 160 == 0),]$step) # 22.85
w3 <- mean(df[df$method=='VN' & (df$Ctime - df$Ctime %% 200 == 0),]$step) # 25.71
#Models and labels
m1 <- loess(e_val~Ctime,data=df[df$method=='VN',])
m2 <- loess(e_val~Ctime,data=df[df$method=='QV',])
pred1 <- predict(m1,data.frame(Ctime=mybreaks))
pred2 <- predict(m2,data.frame(Ctime=mybreaks))
#Labels and positions 1
df1 <- data.frame(x=mybreaks,
y=pred1,
lab=c(v1,v2,v3))
df2 <- data.frame(x=mybreaks,
y=pred2,
lab=c(w1,w2,w3))
#Labels and positions 2
df1 <- data.frame(x=mybreaks,
y=pred1,
lab=c(v1,v2,v3),
method='VN')
df2 <- data.frame(x=mybreaks,
y=pred2,
lab=c(w1,w2,w3),
method='QV')
df3 <- rbind(df1,df2)
#Option 1
q1 <- p1+annotate(geom = 'point',x=df1$x,y=df1$y,color='blue',size=3,)+
annotate(geom = 'point',x=df2$x,y=df2$y,color='red',size=3,)+
annotate(geom = 'text',x=df1$x,y=df1$y,label=round(df1$lab,2),
size=3,hjust=-0.5,color='cyan4',fontface='bold')+
annotate(geom = 'text',x=df2$x,y=df2$y,label=round(df2$lab,2),
size=3,hjust=-0.5,color='tomato',fontface='bold')
#Option 2
q1 <- p1+geom_point(data=df3,aes(x=x,y=y,group=method,color=method),size=3,show.legend = F)+
geom_text(data=df3,aes(x=x,y=y,group=method,label=round(lab,2)),
hjust=-0.5,size=3,fontface='bold',show.legend = F)