简体   繁体   中英

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

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

在此处输入图像描述

I want something like this在此处输入图像描述

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

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

在此处输入图像描述

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM