如何在 R 中调整图形内数据标签的位置?

How to adjust the position of data label inside graph in R?

我想知道如何在 R 中调整图表中数据标签的位置。这是我的数据和图表。

Genotype<- rep(c("A","B","C","D","E"), times=2)
Factor<- rep(c("Control","Treatment"), each=5)
Control<- c(100,120,115,95,110,90,70,90,85,105)
Reaction_norm <- c(0.5,2.5,1.3,0.5,0.3,0.5,2.5,1.3,0.5,0.3)

DataA<- data.frame(Genotype,Factor,Control,Reaction_norm)
DataA

ggplot(data=dataA, aes(x=norm, y=Control))+
  geom_smooth(aes(group=Factor), method=lm, level=0.95, se=FALSE, linetype=1, 
              size=0.5, formula=y~x) +
  
  geom_point (aes(shape=Factor, fill=Factor), col="Black", size=5) +
  
  geom_text(aes(label=Genotype, size=NULL)) +
              
  scale_fill_manual(values = c("Black","Dark red")) +
  scale_shape_manual(values = c(21,22)) +
  scale_x_continuous(breaks = seq(0,3,0.5),limits = c(0,3)) + 
  scale_y_continuous(breaks = seq(0,150,50), limits = c(0,150)) +
  labs(x="Genotype", y="Responsiveness (%)") +
  theme_grey(base_size=17, base_family="serif")+
  theme(legend.position= 'none',
        axis.line= element_line(size=0.5, colour="black")) +
  windows(width=5.5, height=5)

数据标签放在点的内侧,但我想把数据标签放在不同的位置,比如蓝色文本。

那么,如何将数据标签放置在不同的位置呢?

Pre-calculate 每个标签的 y 位置,然后将 aes(y = Genotype_y) 传递给 geom_text

如果您同意所有标签都严格低于或高于每个点,则将 nudge_y 传递给 geom_text,请参阅 https://ggplot2.tidyverse.org/reference/geom_text.html

一个可能的选择是使用 ggrepel package,例如

library(ggplot2)
library(ggrepel)

Genotype<- rep(c("A","B","C","D","E"), times=2)
Factor<- rep(c("Control","Treatment"), each=5)
Control<- c(100,120,115,95,110,90,70,90,85,105)
Reaction_norm <- c(0.5,2.5,1.3,0.5,0.3,0.5,2.5,1.3,0.5,0.3)

DataA <- data.frame(Genotype,Factor,Control,Reaction_norm)
DataA
#>    Genotype    Factor Control Reaction_norm
#> 1         A   Control     100           0.5
#> 2         B   Control     120           2.5
#> 3         C   Control     115           1.3
#> 4         D   Control      95           0.5
#> 5         E   Control     110           0.3
#> 6         A Treatment      90           0.5
#> 7         B Treatment      70           2.5
#> 8         C Treatment      90           1.3
#> 9         D Treatment      85           0.5
#> 10        E Treatment     105           0.3

ggplot(data=DataA, aes(x=Reaction_norm, y=Control))+
  geom_smooth(aes(group=Factor), method=lm, level=0.95, se=FALSE, linetype=1, 
              size=0.5, formula=y~x) +
  geom_point(aes(shape=Factor, fill=Factor), col="Black", size=5) +
  geom_text_repel(aes(label=Genotype),
                  min.segment.length = 0,
                  box.padding = 2,
                  max.overlaps = 100,
                  size = 8) +
  scale_fill_manual(values = c("Black","Dark red")) +
  scale_shape_manual(values = c(21,22)) +
  scale_x_continuous(breaks = seq(0,3,0.5),limits = c(0,3)) + 
  scale_y_continuous(breaks = seq(0,150,50), limits = c(0,150)) +
  labs(x="Genotype", y="Responsiveness (%)") +
  theme_grey(base_size=17, base_family="serif")+
  theme(legend.position= 'none',
        axis.line= element_line(size=0.5, colour="black"))

reprex package (v2.0.1)

于 2022-05-29 创建

要仅显示“治疗”组的标签,一种解决方案是在因子 =“对照”时将基因型设为空白:

library(ggplot2)
library(ggrepel)

Genotype<- rep(c("A","B","C","D","E"), times=2)
Factor<- rep(c("Control","Treatment"), each=5)
Control<- c(100,120,115,95,110,90,70,90,85,105)
Reaction_norm <- c(0.5,2.5,1.3,0.5,0.3,0.5,2.5,1.3,0.5,0.3)

DataA <- data.frame(Genotype,Factor,Control,Reaction_norm)
DataA$Genotype <- ifelse(DataA$Factor == "Treatment", DataA$Genotype, "")

ggplot(data=DataA, aes(x=Reaction_norm, y=Control))+
  geom_smooth(aes(group=Factor), method=lm, level=0.95, se=FALSE, linetype=1, 
              size=0.5, formula=y~x) +
  geom_point(aes(shape=Factor, fill=Factor), col="Black", size=5) +
  geom_text_repel(aes(label=Genotype),
                  min.segment.length = 0,
                  box.padding = 2,
                  max.overlaps = 100,
                  size = 8,
                  ylim = 60) +
  scale_fill_manual(values = c("Black","Dark red")) +
  scale_shape_manual(values = c(21,22)) +
  scale_x_continuous(breaks = seq(0,3,0.5),limits = c(0,3)) + 
  scale_y_continuous(breaks = seq(0,150,50), limits = c(0,150)) +
  labs(x="Genotype", y="Responsiveness (%)") +
  theme_grey(base_size=17, base_family="serif")+
  theme(legend.position= 'none',
        axis.line= element_line(size=0.5, colour="black"))

reprex package (v2.0.1)

于 2022-05-29 创建