ggplot2-线条图下方的标签 请考虑以下代码: library(ggplot2) data<-mtcars data$hp2<-mtcars$hp+50 data_long <- reshape2::melt(data[c('cyl', 'hp', 'hp2')], id.vars = 'cyl') head(data_long) ggplot(data_long, aes(x = cyl, y = value, colour = variable)) + stat_summary(fun.y = mean, geom = "line", show.legend = FALSE) + stat_summary(fun.y = mean, geom = "text", show.legend = FALSE, vjust=-0.7, aes( label=round(..y.., digits=0))) + scale_color_manual(values = c("red", "blue")) 库(ggplot2) 数据

ggplot2-线条图下方的标签 请考虑以下代码: library(ggplot2) data<-mtcars data$hp2<-mtcars$hp+50 data_long <- reshape2::melt(data[c('cyl', 'hp', 'hp2')], id.vars = 'cyl') head(data_long) ggplot(data_long, aes(x = cyl, y = value, colour = variable)) + stat_summary(fun.y = mean, geom = "line", show.legend = FALSE) + stat_summary(fun.y = mean, geom = "text", show.legend = FALSE, vjust=-0.7, aes( label=round(..y.., digits=0))) + scale_color_manual(values = c("red", "blue")) 库(ggplot2) 数据,r,ggplot2,R,Ggplot2,您可以为每个值分配一个向量到vjust=,以分配其位置 ggplot(data_long, aes(x = cyl, y = value, colour = variable)) + stat_summary(fun.y = mean, geom = "line", show.legend = FALSE) + stat_summary(fun.y = mean, geom = "text", show.legend = FALSE, vjust= r

您可以为每个值分配一个向量到
vjust=
,以分配其位置

ggplot(data_long, aes(x = cyl, y = value, colour = variable)) +
  stat_summary(fun.y = mean, geom = "line", show.legend = FALSE) + 
  stat_summary(fun.y = mean, geom = "text", show.legend = FALSE,  
               vjust= rep(c(1.4, -0.7), each = 3), aes( label=round(..y.., digits=0))) +
  scale_color_manual(values = c("red", "blue"))

如果您正在使用my to另一个问题中的代码,您应该。让代码更通用的小建议:
each=length(unique(data\u long$cyl))