Home > Enterprise >  vjust and hjust do not work in annotate in ggplot2
vjust and hjust do not work in annotate in ggplot2

Time:09-05

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
  )

above is right and run well, but when I use vjust/hjust instead of x=/y= to control location of annotate, there is no annotate in blew code running

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
  )

why? and how to

CodePudding user response:

You must provide x and y arguments to locate the position of you annotation. Hjust and vjust are are just alignment parameters. So define both (x,y and hjust, vjust and it will work.

  • Related