Home > Enterprise >  how can I draw a connecting line between the label and the plot in R
how can I draw a connecting line between the label and the plot in R

Time:01-21

This is my script for the plot,

data = data.frame(Kingdom = c("Bacteria", "Archaea"),
                     Total = c(273523, 2616))
sizeRange <- c(0,30)

library(ggplot2)

ggplot(data, aes(x=0,y=Kingdom,color=Kingdom))   
  geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2)   
  scale_size(range = sizeRange)  
  theme_bw()   
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "white"))

somebody, please tell me how can I get a connecting line between my y-axis label and the plot My plot looks like this

enter image description here

I want something like thisenter image description here

CodePudding user response:

A clean alternative would be to label the points directly, and remove the y-axis if wanted. e.g.:

ggplot(data, aes(x=0,y=Kingdom,color=Kingdom))   
  ggrepel::geom_text_repel(aes(label = Kingdom), vjust = -1,colour="black")   
  geom_point(aes(size = Total),colour="blue",stroke=2)  
  scale_size(range = sizeRange)  
  theme_bw()   
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "white"),
        axis.text.y=element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y=element_blank())

enter image description here

CodePudding user response:

you can manually add segments, but then the alpha of your points will kind of show them.

Here is a try, altought it's not perfect if the x axis expend.

ggplot(data, aes(x=0,y=Kingdom,color=Kingdom))  
  # Added the segments here before the points.
  # I tried to alpha it but I can't figure out how to limit the 
  # segment to the point border.
  geom_segment(x = rep(-100,2), xend = rep(0,2), 
               y = c(1, 2), yend = c(1,2),colour="blue", alpha = 10)  
  geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2)  
  scale_size(range = sizeRange)  
  theme_bw()   guides(alpha = "none")   # remove alpha from legend.
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "white"))
  •  Tags:  
  • Related