我需要一个包含两个以上变量的图,为此,我使用melt和ggplot2更改了我的数据框。我想把标签放在每一行的末尾,我用的是ggrapel,但是我得到了很多标签,感谢大家的帮助。
代码如下:
obs1 <- melt(obs, id.vars = "Profundidad")
ggplot(obs1, aes(Profundidad, value, col = variable)) +
geom_line() +
geom_point(alpha = 0.5)+
scale_color_manual(values = dath) +
theme_linedraw() +
theme(axis.text = element_text(size = 12),
axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16),
legend.position = "none")+
labs(x =" Profundidad de secuenciación", y = "ASVs observados")+
geom_label_repel(aes(label = variable),
nudge_x = 0.1,
na.rm = FALSE)我得到了这个:

非常感谢。
发布于 2020-08-28 15:39:18
这里有一个示例,它应该说明如何将标签放在每一行的末尾。
library(ggplot2)
library(ggrepel)
library(dplyr)
# Create a dataset similar to your obs1
n <- 10
obs1 <- data.frame(Profundidad=rep(seq(0, 20000, length.out=n), n),
value = rep(sqrt(0:9),n)*rep(1:10, each=n),
variable=factor(rep(1:10, each=n)))
# Find the x and y position of the last point for each line
xy_labs <- obs1 %>%
group_by(variable) %>%
summarize(pos = which.max(Profundidad),
x = Profundidad[pos],
y = value[pos])
ggplot(obs1, aes(Profundidad, value, group=variable, col = variable)) +
geom_line() +
geom_point(alpha = 0.5)+
theme_linedraw() +
theme(axis.text = element_text(size = 12),
axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16),
legend.position = "none") +
labs(x =" Profundidad de secuenciación", y = "ASVs observados")+
geom_label_repel(data=xy_labs, aes(x=x, y=y, label = y),
nudge_x = 0.1, inherit.aes=F,
na.rm = FALSE)

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