【问题标题】:Bubble chart without axis with labels in RR 中没有带有标签的轴的气泡图
【发布时间】:2019-04-11 05:56:02
【问题描述】:

我有以下数据框:

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

【问题讨论】:

    标签: r ggplot2 visualization bubble-chart


    【解决方案1】:

    您使用适当的 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))
    

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

    【讨论】:

    • 对不起,我的代码生成圆圈有错误,我必须得到 13 个圆圈,这是级别数(df$text),你知道我应该如何纠正这个问题吗?@Jrakru56
    • 我不确定我是否理解您的问题。你是指同名的圈子不止1个吗?
    • 没错,图表中应该有13个圆圈@Jrakru56
    • 查看df,有22行重复text名称。您可能错过了一个聚合步骤,例如计算每个text 的出现次数。 circleProgressiveLayout 没有为圆圈大小提供任何参数。这有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多