gpt4 book ai didi

r - ggplot饼图标签

转载 作者:行者123 更新时间:2023-12-01 18:03:50 25 4
gpt4 key购买 nike

我正在努力使饼图标签正确。环顾四周,认为我可以轻松实现 mathematicalCoffee做过。到目前为止我有这段代码:

ltr = LETTERS[seq( from = 1, to = 26)]

wght = runif(length(ltr))
wght = wght/sum(wght)
wght = round(wght, digits = 2)

alloc = as.data.frame(cbind(ltr, wght))
alloc$wght = as.numeric(as.character(alloc$wght))

ggpie <- function (dat, by, totals) {
ggplot(dat, aes_string(x=factor(1), y=totals, fill=by)) +
geom_bar(stat='identity', color='black') +
guides(fill=guide_legend(override.aes=list(colour=NA))) +
coord_polar(theta='y') +
theme(axis.ticks=element_blank(),
axis.text.y=element_blank(),
axis.text.x=element_text(colour='black'),
axis.title=element_blank()) +
## scale_fill_brewer(palette = "GnBu") +
scale_y_continuous(breaks=cumsum(dat[[totals]]) - dat[[totals]] / 2, labels=paste(dat[[by]], ":", dat[[totals]]))
}

AA = ggpie(alloc, by = "ltr", totals = "wght") +
ggtitle("Letter weights")

AA

生成的饼图:Pie

有没有办法生成这样的东西,例如:

CoolPie

更新建议的重复 - 我认为该线程更多的是关于饼图的替代方案以及为什么饼图不好。我想坚持使用饼图,并希望找到一种正确/用户友好处理标签的解决方案。

最佳答案

对于饼图,plotly 比 ggplot 更容易工作。也许是这样的:

library(plotly)

p <- plot_ly(alloc, labels = ~ltr, values = ~wght, type = 'pie',textposition = 'outside',textinfo = 'label+percent') %>%
layout(title = 'Letters',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

enter image description here

关于r - ggplot饼图标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44436214/

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