【问题标题】:wrong labeling in ggplot pie chartggplot饼图中的错误标签
【发布时间】:2017-01-15 13:33:15
【问题描述】:

我正在尝试为百分比值创建一个饼图,当我尝试标记它们时,标记是错误的,

我的意思是这些值在图中指向错误的位置。

ggplot(Consumption_building_type, aes(x="", y=percentage, fill=Building_type))+ geom_bar(width = 0.5,stat ="identity")+coord_polar(theta = "y",direction = -1)+geom_text(aes(x=1.3,y = percentage/3 + c(0, cumsum(percentage)[- length(percentage)]),label = round(Consumption_building_type$percentage,0))) + theme_void()+ scale_fill_brewer(palette="GnBu")+ggtitle("Breakdown of building types")+theme_minimal()

这是我使用的代码,这是我得到的结果:


当我更改 direction=1 时,图形和标签都会移动

我使用的数据

structure(list(
    Building_type = c("Commercial", "Industrial", "Institutional", "Large residential", 
    "Large Residential", "Residential", "Small residential"), 
    Total_consumption_GJ = c(99665694, 5970695, 10801610, 63699633,
                             16616981, 24373766, 70488556), 
    average_consumption_GJ = c(281541.508474576, 72813.3536585366, 109107.171717172, 
    677655.670212766, 213038.217948718, 123099.828282828, 640805.054545455), 
    total = c(354L, 82L, 99L, 94L, 78L, 198L, 110L), 
    percentage = c(34.8768472906404, 8.07881773399015, 9.75369458128079, 
    9.26108374384236, 7.68472906403941, 19.5073891625616, 10.8374384236453)), 
    .Names = c("Building_type", "Total_consumption_GJ", "average_consumption_GJ", "total", "percentage"), 
     class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -7L)))

对于新用户不知道如何粘贴数据的数据真的很抱歉

【问题讨论】:

  • 您能否发布一个可重现的示例,即告诉我们从哪里获取您的数据或使用公共数据集?我尝试使用 mpg 数据集执行相同的步骤,但无法复制您的问题,改变方向不会改变颜色和标签。
  • 我使用来自data 的数据集根据建筑物类型计算频率,然后在频率上创建一个百分比,然后绘制图表
  • 欢迎来到 Stack Overflow!我们必须下载该数据并进行您所做的计算以便为您提供帮助,这似乎有点繁重。通常的方法是您应该使用dput(Consumption_building_type) 创建您的数据的可打印版本,以便我们可以剪切和粘贴它。
  • 结构(列表(建筑类型= c(“商业”,“工业”,“机构”,“大型住宅”,“大型住宅”,“住宅”,“小型住宅”),Total_consumption_GJ = C(99665694,5970695,10801610,63699633,16616981,24373766,70488556),average_consumption_GJ = C(281541.508474576,72813.3536585366,109107.171717172,677655.670212766,213038.217948718,123099.828282828,640805.054545455),总= C(354L,82L,99L,94L,78L, 198L, 110L), 百分比 = c(34.87, 8.07, 9.75, 9.26, 7.68, 19.50, 10.83))
  • @G5W:我不知道,对不起,从现在开始使用dput()

标签: r ggplot2 pie-chart


【解决方案1】:

ggplot 2.0+ 更新

ggplot 2.0+ 为position_stack() 提供了一些新参数,使解决这个问题变得更加简单。无需手动计算每个条的中心点(尽管在某些情况下仍可能首选该解决方案,因此在下面保留)。相反,我们可以简单地使用position_stack()的“vjust”参数:

g <- ggplot(Consumption_building_type, aes(x="", y=percentage, fill=Building_type))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = percentage, label = round(Consumption_building_type$percentage,0)), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Breakdown of building types")+theme_minimal() +
    labs(x = NULL)

一般解决方案:手动计算堆叠条的中点

我假设您的目标是在条的中心点为每个条放置一个标签。在这种情况下,首先我们可以计算中心点并将其添加到数据框中:

Consumption_building_type$zone.start <- with(Consumption_building_type, c(0, cumsum(percentage)[-length(percentage)]))  
Consumption_building_type$zone.end <- with(Consumption_building_type, cumsum(percentage))  
Consumption_building_type$label.point <- with(Consumption_building_type, (zone.start + zone.end) / 2) 

      Building_type Total_consumption_GJ average_consumption_GJ total percentage zone.start zone.end label.point
1        Commercial             99665694              281541.51   354      34.87       0.00    34.87      17.435
2        Industrial              5970695               72813.35    82       8.07      34.87    42.94      38.905
3     Institutional             10801610              109107.17    99       9.75      42.94    52.69      47.815
4 Large residential             63699633              677655.67    94       9.26      52.69    61.95      57.320
5 Large Residential             16616981              213038.22    78       7.68      61.95    69.63      65.790
6       Residential             24373766              123099.83   198      19.50      69.63    89.13      79.380
7 Small residential             70488556              640805.05   110      10.83      89.13    99.96      94.545

然后geom_label() 中的y 美学就是新创建的“label.point”列。

我还添加了labs(x = NULL),以便最终绘图的 y 轴上没有空引号。

new.plot <- ggplot(Consumption_building_type, aes(x="", y=percentage, fill=Building_type))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = label.point, label = round(Consumption_building_type$percentage,0))) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Breakdown of building types")+theme_minimal()

【讨论】:

  • 感谢完美
  • 太好了,有机会请把答案标记为正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多