创建半甜甜圈或议会席位图 [英] Creating a half-donut, or parliamentary seating, chart

查看:99
本文介绍了创建半甜甜圈或议会席位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个图表,在ggplot2中显示议会席位的大小,例如下面的图表.本质上,我的主要问题是如何将甜甜圈图转换为半甜甜圈图(半圆弧)?

I'd like to create a chart displaying the size of the seats to a parliament, such as the one below, in ggplot2. My main problem is, essentially, how do I turn a donut chart to a half-donut chart (half-circle arc)?

以上面的图片为例,我不知道从这里去哪里:

Using the picture above as an example, I don't know where to go from here:

df <- data.frame(Party = c("GUE/NGL", "S&D", "Greens/EFA", "ALDE", "EPP", "ECR", "EFD", "NA"),
                             Number = c(35, 184, 55, 84, 265, 54, 32, 27))
df$Party <- factor(df$Party)
df$Share <- df$Number / sum(df$Number)
df$ymax <- cumsum(df$Share)
df$ymin <- c(0, head(df$ymax, n= -1))

ggplot(df, aes(fill = Party, ymax = ymax, ymin = ymin, xmax = 2, xmin = 1)) + geom_rect() + 
coord_polar(theta = "y") + xlim(c(0, 2))

推荐答案

这对您有用吗?

ggplot(df, aes(fill = Party, ymax = ymax, ymin = ymin, xmax = 2, xmin = 1)) + geom_rect() + 
  coord_polar(theta = "y",start=-pi/2) + xlim(c(0, 2)) + ylim(c(0,2))

基本上,您只是将ylim设置为最大值的2倍,因此只能将其绘制为一半.在这种情况下,我们将y限制设置为0到2.然后您可以将coord_polar(start=)中的起始位置偏移以将其放置在适当的位置.

Basically you just set the ylim to be 2x your max so it only plots it on half. In this case we set the y limits to be from 0 to 2. Then you can offset the start in coord_polar(start=) to get it in proper place.

这篇关于创建半甜甜圈或议会席位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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