简体   繁体   English

ggplot饼图标签

[英]ggplot pie chart labeling

I am struggling with getting the pie chart labels correct. 我正在努力让饼图标签正确。 Looked around and thought that I could easily implement what mathematicalCoffee did. 环顾四周,并认为我可以轻松实现mathCoffee的功能 So far I have this code: 到目前为止,我有这个代码:

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

The resulting pie chart: 生成的饼图: 馅饼

Is there any way to generate something like this, for example: 有没有办法生成这样的东西,例如:

CoolPie

Update for suggested dup - I think that thread is more about alternatives to pie charts and why pie charts are bad. 更新建议的重复 - 我认为该主题更多是关于饼图的替代品以及为什么饼图很糟糕。 I would like to stick to pie charts and want to find a solution to handling labels correctly/user-friendly. 我想坚持使用饼图,并希望找到正确处理标签/用户友好的解决方案。

We can make it work with ggplot2 and the ggrepel package. 我们可以使用ggplot2ggrepel包。

Unfortunately geom_text_repel() does not support a position = argument, so we have to calculate the starting position of the line by hand. 不幸的是, geom_text_repel()不支持position =参数,因此我们必须手动计算该行的起始位置。

With your data.frame : 使用您的data.frame

alloc$pos = (cumsum(c(0, alloc$wght)) + c(alloc$wght / 2, .01))[1:nrow(alloc)]

This calculates the mean point for each group (or obs, or whtvr you want to call it). 这会计算每个组(或者您想要调用它的名称或平均值)的平均点数。

Plugging it in in the geom_text_repel 's y aes gives a nice result: 将它插入geom_text_repely aes会得到一个很好的结果:

library(ggplot2)
library(ggrepel)
ggplot(alloc, aes(1, wght, fill = ltr)) +
    geom_col(color = 'black', 
             position = position_stack(reverse = TRUE), 
             show.legend = FALSE) +
    geom_text_repel(aes(x = 1.4, y = pos, label = ltr), 
                    nudge_x = .3, 
                    segment.size = .7, 
                    show.legend = FALSE) +
    coord_polar('y') +
    theme_void()

I made some choices, as the use of text instead of label , the removal of the legend and the axes. 我做了一些选择,因为使用text而不是label ,删除了图例和轴。 Feel free to change them 随意改变它们

For pie charts plotly works a lot easier than ggplot. 对于饼图,绘图比ggplot更容易。 Perhaps something like this: 也许是这样的:

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

在此输入图像描述

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

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