ggplot 甜甜圈图 [英] ggplot Donut chart

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

问题描述

我真的在谷歌上搜索了很多,但没有任何乐趣.如果网站存在,很乐意获得对网站的引用.我正在努力理解 Hadley 极坐标文档,我知道馅饼/甜甜圈图表本质上被认为是邪恶的.

Hi I really have googled this a lot without any joy. Would be happy to get a reference to a website if it exists. I'm struggling to understand the Hadley documentation on polar coordinates and I know that pie/donut charts are considered inherently evil.

也就是说,我想做的是

  1. tikz 环图 此处显示
  2. 在顶部添加第二层圆圈(alpha=0.5 左右),显示第二个(可比较的)变量.
  1. Create a donut/ring chart (so a pie with an empty middle) like the tikz ring chart shown here
  2. Add a second layer circle on top (with alpha=0.5 or so) that shows a second (comparable) variable.

为什么?我正在寻找显示财务信息.第一个环是成本(分解),第二环是总收入.然后我们的想法是为每个审查期添加 + facet=period 以显示收入和支出的趋势以及两者的增长.

Why? I'm looking to show financial information. The first ring is costs (broken down) and the second is total income. The idea is then to add + facet=period for each review period to show the trend in both revenues and expenses and the growth in both.

任何想法将不胜感激

注意:如果尝试使用 MWE,则完全任意

Note: Completely arbitrarily if an MWE is needed if this was tried with

donut_data=iris[,2:4]
revenue_data=iris[,1]
facet=iris$Species

这与我正在尝试做的类似..谢谢

That would be similar to what I'm trying to do.. Thanks

推荐答案

我没有对您的问题的完整答案,但我可以提供一些代码,可以帮助您开始使用 ggplot2.

I don't have a full answer to your question, but I can offer some code that may help get you started making ring plots using ggplot2.

library(ggplot2)

# Create test data.
dat = data.frame(count=c(10, 60, 30), category=c("A", "B", "C"))

# Add addition columns, needed for drawing with geom_rect.
dat$fraction = dat$count / sum(dat$count)
dat = dat[order(dat$fraction), ]
dat$ymax = cumsum(dat$fraction)
dat$ymin = c(0, head(dat$ymax, n=-1))

p1 = ggplot(dat, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
     geom_rect() +
     coord_polar(theta="y") +
     xlim(c(0, 4)) +
     labs(title="Basic ring plot")

p2 = ggplot(dat, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
     geom_rect(colour="grey30") +
     coord_polar(theta="y") +
     xlim(c(0, 4)) +
     theme_bw() +
     theme(panel.grid=element_blank()) +
     theme(axis.text=element_blank()) +
     theme(axis.ticks=element_blank()) +
     labs(title="Customized ring plot")


library(gridExtra)
png("ring_plots_1.png", height=4, width=8, units="in", res=120)
grid.arrange(p1, p2, nrow=1)
dev.off()

想法:

  1. 如果您发布一些结构良好的示例数据,您可能会得到更多有用的答案.您提到使用 iris 数据集中的一些列(一个好的开始),但我无法看到如何使用这些数据来制作环形图.例如,您链接的环形图显示了多个类别的比例,但 iris[, 2:4]iris[, 1] 都不是分类的.莉>
  2. 您想在顶部添加第二层圆":您的意思是将第二个圆环直接叠加在第一个圆环之上?或者您希望第二个戒指在第一个戒指的内部还是外部?您可以添加第二个内部环,例如 geom_rect(data=dat2, xmax=3, xmin=2, aes(ymax=ymax, ymin=ymin))
  3. 如果您的 data.frame 有一个名为 period 的列,您可以使用 facet_wrap(~ period) 进行分面.
  4. 要最轻松地使用 ggplot2,您需要长格式"的数据;reshape2 包中的 melt() 可能对转换数据很有用.
  5. 制作一些条形图进行比较,即使您决定不使用它们.例如,尝试:ggplot(dat, aes(x=category, y=count, fill=category)) +geom_bar(stat="identity")
  1. You may get more useful answers if you post some well-structured sample data. You have mentioned using some columns from the iris dataset (a good start), but I am unable to see how to use that data to make a ring plot. For example, the ring plot you have linked to shows proportions of several categories, but neither iris[, 2:4] nor iris[, 1] are categorical.
  2. You want to "Add a second layer circle on top": Do you mean to superimpose the second ring directly on top of the first? Or do you want the second ring to be inside or outside of the first? You could add a second internal ring with something like geom_rect(data=dat2, xmax=3, xmin=2, aes(ymax=ymax, ymin=ymin))
  3. If your data.frame has a column named period, you can use facet_wrap(~ period) for facetting.
  4. To use ggplot2 most easily, you will want your data in 'long-form'; melt() from the reshape2 package may be useful for converting the data.
  5. Make some barplots for comparison, even if you decide not to use them. For example, try: ggplot(dat, aes(x=category, y=count, fill=category)) + geom_bar(stat="identity")

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

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