问题描述
我真的很喜欢这个,没有任何喜悦。如果网站存在,我们很乐意为您提供参考。我努力去理解,我知道pie /甜甜圈图表被认为天生就是邪恶的。
也就是说,我想要做的是
- 创建一个甜甜圈/圆环图(这是一个空的中间派),像
- 在上面添加第二个图层圆圈(with
alpha = 0.5
左右)显示第二个(可比较的)变量。
- 在上面添加第二个图层圆圈(with
为什么呢?我正在寻找显示财务信息。第一环是成本(细分),第二环是总收入。然后,想法是在每个审查周期内添加 + facet = period
,以显示收入和支出的趋势以及两者的增长。
任何想法都将得到最多赞赏
注意:如果需要使用MWE,完全任意使用
donut_data = iris [,2:4]
revenue_data = iris [,1]
facet = iris $种类
这与我试图做的类似。谢谢
我没有完整的回答你的问题,但我可以提供一些代码,可以帮助你开始使用 ggplot2
。
library(ggplot2)
#创建测试数据。
dat = data.frame(count = c(10,60,30),category = c(A,B,C))
#添加列,需要使用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, ()()+
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(color =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戒指)
library(gridExtra)
png(ring_plots_1.png,height = 4,width = 8,units =in,res = 120)
grid.arrange(p1,p2,nrow = 1)
dev.off()
想法: > 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. That said, what I'm trying to do is 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 Any thoughts would be most appreciated Note: Completely arbitrarily if an MWE is needed if this was tried with That would be similar to what I'm trying to do.. Thanks 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 Thoughts: 这篇关于ggplot甜甜圈图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
iris
数据集中的一些列(一个好的开始),但我无法看到如何使用这些数据来创建一个环形图。例如,您链接的环图显示了几个类别的比例,但是既不是>虹膜[,2:4]
也不是虹膜[,1]
geom_rect(data = dat2,xmax = 3,xmin = 2,aes(ymax = ymax,ymin = ymin))
li>
period
的列,您可以使用 facet_wrap(〜period)$
ggplot2
,您需要以'long-form'形式存储数据。 来自
reshape2
包的 melt()
可能对转换数据非常有用。
ggplot(dat,aes(x = category,y = count,fill = category))+
geom_bar(stat =identity)
alpha=0.5
or so) that shows a second (comparable) variable.+ facet=period
for each review period to show the trend in both revenues and expenses and the growth in both.donut_data=iris[,2:4]
revenue_data=iris[,1]
facet=iris$Species
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()
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.geom_rect(data=dat2, xmax=3, xmin=2, aes(ymax=ymax, ymin=ymin))
period
, you can use facet_wrap(~ period)
for facetting.ggplot2
most easily, you will want your data in 'long-form'; melt()
from the reshape2
package may be useful for converting the data.ggplot(dat, aes(x=category, y=count, fill=category)) + geom_bar(stat="identity")