首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在线条末尾绘制标签

在线条末尾绘制标签
EN

Stack Overflow用户
提问于 2020-08-28 05:48:35
回答 1查看 76关注 0票数 1

我需要一个包含两个以上变量的图,为此,我使用meltggplot2更改了我的数据框。我想把标签放在每一行的末尾,我用的是ggrapel,但是我得到了很多标签,感谢大家的帮助。

代码如下:

代码语言:javascript
复制
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)

我得到了这个:

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-28 15:39:18

这里有一个示例,它应该说明如何将标签放在每一行的末尾。

代码语言:javascript
复制
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)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63624561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档