如何在 ggplot2 中将标签写入绘图。

3
我有一个带有y轴标签的图表(在使用coord_flip之后) 这是一个可复现的例子
Names <- c("ExampleA","ExampleB","ExampleC","ExampleD", "ExampleE", "ExampleF","ExampleG", "ExampleH")
Counts <- c(4,3,2,1,-1,-2,-3,-4)
Type <- c("X","X","X","X", "Y","Y","Y","Y")
df <-data.frame(Names,Counts, Type)

ggplot(df, aes(x=reorder(Names,Counts), y=Counts, fill=Type))+
  geom_col()+
  coord_flip()+
  theme_minimal()

我希望标签写在柱状图旁边。也就是说,“ExampleA/B/C/D”等应该紧挨着柱状图写,而不是列在y轴上。 或者用可视化的方式来表达: 我现在的情况

What I have:

我想要的是 我想要的是: (Li等人,2021年 https://doi.org/10.3389/fimmu.2021.694801)。
我尝试了: ggplot:将轴文本放在图内 但没有成功。
这个。
geom_label(label=Names, parse=TRUE)

接近但不是我想要的(而且对于真实数据而言,只有在我将每个名称逐个键入向量时才起作用,如果不必要的话我希望避免这样做)。 我希望有一种类似的方法

geom_label(label=Names, position="inside")

或者

scale_x_discrete(position = "inside")

可能是可以的,但到目前为止我还没有找到任何相关的东西。

3个回答

3

更新以匹配OP所需的输出和长名称。

重新排列了原始的x和y主题,这样我们就可以避免使用coord_flip并按原样读取图形,而不是转置x和y。

使用hjustnudge_x使文本定位更加稳健,前者对齐文本,后者提供文本与条形图末端之间的填充。

至于长文本,通过在调用scale_x_continuous时使用expansion来创建绘图两侧的额外空间,并在调用geom_text时将文本大小设置为3来进行管理。另一种方法是编辑文本以包含换行符,但在有许多条形图的情况下可能会出现问题。

library(ggplot2)

Names <- c("ribonucleoside triphosphate biosynthetic process" ,"purine ribonucleoside triphosphate biosynthetic process" ,"ATP biosynthetic process" ,"proton motive force-driven ATP synthesis" ,"NADH dehydrogenase complex assembly" ,"mitochondrial respiratory chain complex I assembly" ,"innate immune response" ,"antigen processing and presentation of peptide antigen")
Counts <- c(4,3,2,1,-1,-2,-3,-4)
Type <- c("X","X","X","X", "Y","Y","Y","Y")

df <-data.frame(Names,Counts, Type)

ggplot(df, aes(Counts, reorder(Names,Counts), fill=Type))+
  geom_col()+
  geom_text(aes(label = Names, x = 0),
            size = 3,
            nudge_x = ifelse(Counts < 0, 0.05, -0.05),
            hjust = ifelse(Counts < 0, 0, 1)) +
  scale_x_continuous(expand = expansion(mult = c(0.75, 0.75)))+
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        legend.position = "bottom")

2023-06-26创建,使用reprex v2.0.2生成


1
抱歉,我漏掉了,我会更改答案! - Peter
1
抱歉我错过了,会更改答案! - Peter
1
抱歉,我错过了那个问题,我会更改答案! - undefined
1
感谢提供新的名称设置,查看更新的答案,似乎已经涵盖了。 - Peter
1
感谢提供新的名称集,已经更新了答案,看起来已经涵盖了。 - Peter
显示剩余16条评论

0
或者,你可以按照下面的方式创建新的变量x2y2,并在annotate函数中使用它们来生成预期的图表。
df <-data.frame(Names,Counts, Type) %>% arrange(desc(Type), desc(Names), Counts) %>% 
mutate(x2=row_number(),
       y2=ifelse(Type=='X',-0.4,0.4)) %>% 
  arrange(Type,Names,Counts)

ggplot(df, aes(x=reorder(Names,Counts), y=Counts, fill=Type))+
  geom_col()+
  coord_flip()+
  theme_minimal() + annotate(geom = 'text', y=df$y2, x=df$x2, label=df$Names) + 
  theme(axis.text.y = element_blank())

# output

      Names Counts Type x2   y2
1 ExampleA      4    X  8 -0.4
2 ExampleB      3    X  7 -0.4
3 ExampleC      2    X  6 -0.4
4 ExampleD      1    X  5 -0.4
5 ExampleE     -1    Y  4  0.4
6 ExampleF     -2    Y  3  0.4
7 ExampleG     -3    Y  2  0.4
8 ExampleH     -4    Y  1  0.4

enter image description here


这对于示例数据也适用。但是对于真实数据(具有较长名称,请参见上面的注释),由于某种原因它失败了。在那里,名称被拆分并部分写在列内。 - LCD
这对于示例数据也适用。但是对于真实数据(具有较长名称,请参见上面的评论),由于某种原因它失败了。在那里,名称被拆分并部分写在列内。 - LCD
这对于示例数据也适用。但是对于真实数据(具有较长名称,请参见上面的评论),由于某种原因它失败了。在那里,名称被拆分并部分写在列中。 - undefined

0
如果Names的大小很长,那么我们可以通过以下方式减小annotate()中标签的大小。
ggplot(df, aes(x=reorder(Names,Counts), y=Counts, fill=Type))+
  geom_col()+
  coord_flip()+
  theme_minimal() + annotate(geom = 'text', y=df$y2, x=df$x2, label=df$Names, size=3) + 
  theme(axis.text.y = element_blank())

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接