简体   繁体   中英

Bubble chart without axis with labels in R

I have the following data frame:

> dput(df)
structure(list(text = structure(c(9L, 10L, 1L, 7L, 5L, 12L, 1L, 
11L, 5L, 8L, 2L, 13L, 2L, 5L, NA, 6L, 13L, 4L, NA, 5L, 4L, 3L
), .Label = c("add ", "change ", "clarify", "correct", "correct ", 
"delete", "embed", "follow", "name ", "remove", "remove ", "specifiy ", 
"update"), class = "factor"), ID = c(1052330L, 915045L, 931207L, 
 572099L, 926845L, 510057L, 927946L, 490640L, 928498L, 893872L, 
 956074L, 627059L, 508649L, 508657L, 1009304L, 493138L, 955579L, 
 144052L, 1011166L, 151059L, 930992L, 913074L)), .Names = c("text", 
 "ID"), class = "data.frame", row.names = c(NA, -22L))

I would like to have a bubble chart for my df with circles labeling with each verb in the text column and also the number of IDs that are related to each verb in the text column. This is the code I have for the circles but I don't know how to do the labeling:

> library(packcircles)
> library(ggplot2)
> packing <- circleProgressiveLayout(df)
> dat.gg <- circleLayoutVertices(packing)
> ggplot(data = dat.gg) +geom_polygon(aes(x, y, group = id, fill =  factor(id)), colour = "black",show.legend = FALSE) +scale_y_reverse() +coord_equal()

You create a data.frame for your labels with the appropriate x and y coordinate and use geom_text

library(ggplot2)
packing <- circleProgressiveLayout(df)
dat.gg <- circleLayoutVertices(packing)
cbind(df, packing) -> new_df
ggplot(data = dat.gg) +geom_polygon(aes(x, y, group = id, fill =  factor(id)), colour = "black",show.legend = FALSE) +
  scale_y_reverse() +coord_equal() +geom_text(data = new_df, aes(x, y,label = text))

在此处输入图片说明

For the Text and ID , you can do:

new_df$text2 <- paste0(new_df$text,"\n",new_df$ID)
ggplot(data = dat.gg) +geom_polygon(aes(x, y, group = id, fill =  factor(id)), colour = "black",show.legend = FALSE) +
  scale_y_reverse() +coord_equal() +geom_text(data = new_df, aes(x, y,label = text2))

在此处输入图片说明

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