在散点图中每个饼图中添加饼比例的百分比 [英] Adding percents for pie proportions in each piechart in scatterpie plot

查看:99
本文介绍了在散点图中每个饼图中添加饼比例的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个散布图,我想为图中的每个饼图添加与饼图比例相对应的百分比.

此问题是我在



如何为每个饼图添加饼图比例,例如以下演示?

  • (与演示不同,我希望将百分比添加到所有饼中,而不仅仅是前5个).

解决此问题的一个提示是, scatterpie 基本上是一个循环的箱线图,如

针对x轴上的每个刻度不同地对齐标签(例如在您的演示中)将需要更多的修补工作.

I have a scatterpie plot, and I want to add the percents that correspond to pie proportions, for every single pie in the plot.

This question is a follow-up to a previous question I posted here.

My Data

library(tidyverse)
library(scatterpie)

my_df <- structure(list(day_in_july = 13:20, yes_and_yes = c(0.611814345991561, 
0.574750830564784, 0.593323216995448, 0.610539845758355, 0.650602409638554, 
0.57429718875502, 0.575971731448763, 0.545454545454545), yes_but_no = c(0.388185654008439, 
0.425249169435216, 0.406676783004552, 0.389460154241645, 0.349397590361446, 
0.42570281124498, 0.424028268551237, 0.454545454545455), y = c(0.388185654008439, 
0.425249169435216, 0.406676783004552, 0.389460154241645, 0.349397590361446, 
0.42570281124498, 0.424028268551237, 0.454545454545455)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))


> my_df

## # A tibble: 8 x 4
##   day_in_july yes_and_yes yes_but_no     y
##         <int>       <dbl>      <dbl> <dbl>
## 1          13       0.612      0.388 0.388
## 2          14       0.575      0.425 0.425
## 3          15       0.593      0.407 0.407
## 4          16       0.611      0.389 0.389
## 5          17       0.651      0.349 0.349
## 6          18       0.574      0.426 0.426
## 7          19       0.576      0.424 0.424
## 8          20       0.545      0.455 0.455

Plotting the scatterpie

xvals <- my_df$day_in_july

ggplot(data = my_df) +
  geom_scatterpie(aes(x = day_in_july, y = y*20, r = 0.25), 
                  data = my_df, 
                  cols = colnames(my_df)[2:3],
                  color = "red") + 
  scale_fill_manual(values = c("pink", "seagreen3")) +
  scale_x_continuous(labels = xvals, breaks = xvals) +
  scale_y_continuous(name = "yes but no",
                     labels = function(x) x/20) + 
  coord_fixed()

which gives this plot:



How can I add the pie proportions for each pie, such as the following demo?

  • (Unlike the demo, I'm looking to add percents to all pies, not just the first 5).

One hint for solving this is that scatterpie is basically a circularized boxplot, as commented by the developer. However, when I tried to implement a barplot annotation strategy I realized I had no idea how to apply it to scatterpie.

解决方案

You could add labels with geom_text like this.

ggplot(data = my_df) +
  geom_scatterpie(aes(x = day_in_july, y = y*20, r = 0.25), 
                  data = my_df, 
                  cols = colnames(my_df)[2:3],
                  color = "red") + 
  geom_text(aes(y = y*20, x = day_in_july, 
                label = paste0(formatC(y*100, digits = 3), "%")),
                nudge_y = 0.07, nudge_x = -0.25, size = 3) +
  geom_text(aes(y = y*20, x = day_in_july, 
                label = paste0(formatC((1-y)*100, digits = 3), "%")),
            nudge_y = 0.07, nudge_x = 0.25, size = 3) +
  scale_fill_manual(values = c("pink", "seagreen3")) +
  scale_x_continuous(labels = xvals, breaks = xvals) +
  scale_y_continuous(name = "yes but no",
                     labels = function(x) x/20) + 
  coord_fixed()

Aligning the labels differently for each tick on the x-axis (like in your demo) would require more tinkering.

这篇关于在散点图中每个饼图中添加饼比例的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆