R语言 如何在ggplot2中将标签写入图中

ujv3wf0j  于 2023-06-27  发布在  其他
关注(0)|答案(3)|浏览(119)

我有一个在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()

我希望标签写在酒吧旁边。意味着“示例A/B/C/D”等应该写在条形旁边,而不是列在y轴上。或者在视觉上:我所拥有的

我想要什么

(Credit Li et al. 2021 https://doi.org/10.3389/fimmu.2021.694801
我试过了:ggplot: put axis text inside plot,没有用。

geom_label(label=Names, parse=TRUE)

是接近的,但不是我真正想要的(而且对于真实的数据,只有当我将每个名字都输入到一个向量中时才有效,如果不是必要的话,我想避免)。我希望你能告诉我

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

scale_x_discrete(position = "inside")

这是可能的,但到目前为止,我什么也没找到。

gdrx4gfi

gdrx4gfi1#

更新以匹配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

67up9zun

67up9zun2#

或者,您可以创建新变量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

w1e3prcc

w1e3prcc3#

如果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())

相关问题