R中不带标签的无轴气泡图

时间:2018-11-07 18:31:23

标签: r ggplot2 visualization bubble-chart

我有以下数据框:

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

我想为我的df制作一个气泡图,并在圆盘中标注带有文本列中每个动词的标签,以及与文本列中每个动词相关的ID数。这是我为圈子准备的代码,但我不知道如何做标签:

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

1 个答案:

答案 0 :(得分:2)

您使用适当的x和y坐标为标签创建data.frame,并使用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))

enter image description here

对于TextID,您可以执行以下操作:

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

enter image description here

相关问题