R语言 vjust和hjust在ggplot2的annotate中不起作用

1l5u6lss  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(194)
ggplot(data = datae31, aes(x = increased_fitness31, y = increased_fitness26)) +
  geom_point( size = 2) + 
  xlab(NULL) + 
  ylab(NULL) + 
  geom_abline(slope = 1, intercept = 0, size = 1, color = "grey", linetype = "dashed") +
  theme_bw()+
  theme(text = element_text(size = 20))+
  theme(legend.position = "none")+
  coord_equal(xlim=c(-0.3,1.8),ylim=c(-0.3,1.8))+
  annotate(
    "text",
    x = 0.75,
    y = 0.75,
    label = "Group 1",
    family = "serif",
    fontface = "italic",
    colour = "darkred",
    size = 5
  )

上面是正确的,运行良好,但是当我使用vjust/hjust而不是x=/y=来控制annotate的位置时,下面的代码中没有annotate运行:

ggplot(data = datae31, aes(x = increased_fitness31, y = increased_fitness26)) +
  geom_point( size = 2) + 
  xlab(NULL) + 
  ylab(NULL) + 
  geom_abline(slope = 1, intercept = 0, size = 1, color = "grey", linetype = "dashed") +
  theme_bw()+
  theme(text = element_text(size = 20))+
  theme(legend.position = "none")+
  coord_equal(xlim=c(-0.3,1.8),ylim=c(-0.3,1.8))+
  annotate(
    "text",
    vjust = 0.5, #only change this
    hjust = 0.5,  #and this
    label = "Group 1",
    family = "serif",
    fontface = "italic",
    colour = "darkred",
    size = 5
  )

为什么?以及如何

jk9hmnmh

jk9hmnmh1#

必须提供x和y参数来定位注解的位置。Hjust和Vjust只是对准参数。所以定义(x,y和hjust,vjust,它就可以工作了。

相关问题