r - 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"))
是否有可能改变红色曲线的文本标签的行为?即在红线下方显示它们?
解决方案
您可以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"))
推荐阅读
- kotlin - 在kotlin中将两个数据类合并为一个
- c - 在子功能中使用 openmp 提高性能
- c# - 敌人射击自己 - Unity C#
- python - 查找矩阵单位根的(部分)列表
- python - 如何根据条件在列的特定范围行中插入值。熊猫
- php - Laravel 8 - 渴望加载不一致的行为
- arrays - 使数组不递减的最小操作数
- android - Android:实时 Firebase 规则
- java - 我想在最后一行打印 ainput,它应该与答案 JAVA ARRAY 形成一个总和解决方案
- azure - AKS 1.22.2 群集入口配置不适用于 Azure 应用程序网关