首页 > 解决方案 > R:ggplot2 更改注释中文本框的边距(geom="label"...)

问题描述

有没有办法控制文本周围框的边距大小?

x <- data.frame(x = c(5, 10), y = c(0.5, 1))

ggplot(data=x, aes(x, y)) +
  geom_bar(stat = 'identity', fill=c("red4","cornflowerblue"))+
  annotate(geom= "label", x=5, y=.6, label="Just\ntext\nhere\nwith\ndifferent\nmargins", size=5)

在此处输入图像描述

标签: rggplot2annotate

解决方案


有参数label.padding。请参阅文档

ggplot(data=x, aes(x, y)) + 
  geom_bar(stat = 'identity', fill=c("red4","cornflowerblue")) + 
  annotate(geom= "label", x=5, y=.6, 
           label="Just\ntext\nhere\nwith\ndifferent\nmargins", size=5,
           label.padding=unit(4, "lines"))    # <------------

推荐阅读