简体   繁体   English

R气泡图:标签重叠

[英]R Bubble chart: labels overlapping

Attempting to code a function that returns a bubble chart from aggregated data. 尝试编写从聚合数据返回气泡图的函数。

I'm passing it a column of a data.frame in "agg". 我在“agg”中传递了一个data.frame列。

aggs2 <- function(agg, deporur=0, all=TRUE){

  ##create aggregate from library data

  agg1 <- aggregate(agg, by=list(NoNA$IMD_NATIONAL_QUINTILE, NoNA$UR), 
                    FUN=function(x) c(mn=mean(x), n=length(x)))

  ##bind into a dataframe

  agg1 <- cbind(agg1[,1:2], agg1[,3])

  ##add column holding values of Deprivation Quantile and Urban/Rural status

  agg1$NewCol <- do.call(paste, c("Deprivation Quantile", agg1[c("Group.1", "Group.2")], 
                                  sep = " "))

  ##set column names

  colnames(agg1) <- c("Deprivation", "Urban and Rural", "Mean", "Count", "DepUR")

  ##remove categories with low counts

  if(all==FALSE){

    agg1 <- subset(agg1, agg1$Count > 9)

  }

  ##order data.frame by mean

  agg1 <- agg1[order(agg1$Mean, decreasing=TRUE),]

  ##create bubble chart
  if(deporur==1){

    radius3 <- sqrt(agg1$Count/pi)

    symbols(factor(agg1$DepUR), agg1$Mean, circles=radius3, inches=0.35, 
            xlim=c(0,10.0), ylim=c(min(agg1$Mean-0.25),10.0), fg="white", bg="purple",
            xlab="Deprivation Quantile and Urban/Rural Status", ylab="Mean Response")

    text(factor(agg1$DepUR), agg1$Mean-.1, agg1$DepUR, cex=0.7)
  }

  #return ordered dataframe
  agg1

}

This returns a sorted data.frame by mean, and the following chart: 这将按平均值返回已排序的data.frame,并显示以下图表:

剥夺与城市/农村

Because this function will need to create graphs from a variety of different documents and columns, I would like to code it so that the labels do not overlap the bubbles, or other labels. 因为此函数需要从各种不同的文档和列创建图形,所以我想对其进行编码,使标签不与气泡或其他标签重叠。

I have looked at the directlabels library, but I have been unable to work out how to code it properly. 我看过直接标签库,但我一直无法弄清楚如何正确编码。

Would greatly appreciate any assistance. 非常感谢任何帮助。

I'm not aware of any solution for non-overlapping labels with regards to other labels AND other circles. 我不知道有关其他标签和其他圈子的非重叠标签的任何解决方案。 Nevertheless, wordcloud::textplot might be a starting point: 尽管如此, wordcloud::textplot可能是一个起点:

library(wordcloud)
set.seed(8)
df <- data.frame(x = runif(10), y = runif(10), size = sample(10:20, 10), lab = paste0("label", 1:10))
par(mfrow = c(1,2))
with(df, {
  plot(x, y, cex = size, pch = 19, col = adjustcolor("violet", alpha.f = .4), main = "non-overlapping")
  textplot(x, y, lab, new = FALSE, show.lines = FALSE, cex = 2)
  plot(x, y, cex = size, pch = 19, col = adjustcolor("violet", alpha.f = .4), main = "overlapping")
  text(x, y, lab, cex = 2)
})

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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