简体   繁体   English

r - 如何正确添加标签到ggplot2水平箱图

[英]r - How to properly add labels to ggplot2 horizontal boxplots

Edited to provide reproducible example. 编辑提供可重复的例子。

Sample data: https://owncloud.cesnet.cz/index.php/s/oopPE2Ut4quSVOK 示例数据: https//owncloud.cesnet.cz/index.php/s/oopPE2Ut4quSVOK

Having this data 有这个数据

> head(df)
  Sample_Name        E Sentrix_ID Sentrix_ID_full
1    P129C1S1 5.636927 5058818037    5058818037_A
2    P129C1S1 5.794948 5058818037    5058818037_A
3    P129C1S1 5.608488 5058818037    5058818037_A
4    P129C1S1 5.989108 5058818037    5058818037_A
5    P129C1S1 5.570090 5058818037    5058818037_A
6    P129C1S1 5.555401 5058818037    5058818037_A

I am generating horizontal boxplots by 我正在生成水平箱图

library(ggplot2)
library(ggthemes)

df <- read.csv("sample.csv")
df$Sentrix_ID <- as.factor(df$Sentrix_ID)
df$Sentrix_ID_full <- as.factor(df$Sentrix_ID_full)

head(df)

p <- ggplot(data = df,
            aes(y = E, x = Sentrix_ID_full, color = Sentrix_ID, label = Sample_Name)) +
  geom_boxplot(outlier.shape = NA) +
  scale_colour_manual(values = c("5058818037" = "red", "5226121006" = "green")) +
  theme_few() +
  guides(colour = FALSE) +
  labs(x = "Sentrix ID", y = "E", title = "intensity values") +
  coord_flip()

p

I want to add label to each boxplot: 我想为每个boxplot添加标签:

p <- p + geom_text()

But this happens: 但这发生了:

Is there any solution? 有什么解决方案吗? Thank you in advance :) 先感谢您 :)

One suggestion : use a second dataframe to label each boxplot only one time and at a defined position : 一个建议:使用第二个数据框仅标记每个箱图一次并在定义的位置:

# mock data
set.seed(1)
df <- data.frame(var_1 = c(rep("A", 15), rep("B", 15), rep("C", 15)),
             value = sample(100, 45, replace= T))

# the kind of plot you have
ggplot(df, aes(x = var_1, y = value, label = var_1)) +
  geom_boxplot() +
  coord_flip() +
  geom_text()

plot_1

# Use of plyr package
library(plyr)
df2 <- ddply(df, "var_1", summarize, max_value = max(value, na.rm = T))
# I chose max_value as position for the label

# the new plot
ggplot(df, aes(x = var_1, y = value)) +
  geom_boxplot() +
  coord_flip() +
  geom_text(data = df2, aes(x = var_1, y = max_value, label = var_1), hjust = -0.5) # use hjust to adjust horizontal position 

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM