gpt4 book ai didi

R气泡图: labels overlapping

转载 作者:行者123 更新时间:2023-12-02 04:47:21 28 4
gpt4 key购买 nike

尝试编写一个从聚合数据返回气泡图的函数。

我将“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

}

这将返回一个按均值排序的 data.frame,以及以下图表:

Deprivation and Urban/Rural

因为此函数需要根据各种不同的文档和列创建图表,所以我想对其进行编码,以便标签不会与气泡或其他标签重叠。

我查看了 directlabels 库,但一直无法弄清楚如何正确地对其进行编码。

非常感谢任何帮助。

最佳答案

我不知道关于其他标签和其他圆圈的非重叠标签的任何解决方案。尽管如此,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)
})

enter image description here

关于R气泡图: labels overlapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31834212/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com