[英]How can I make the angles of my polar coordinates relate to exposure proportions in ggplot2?
Suppose I have the following data in R: 假设我在R中有以下数据:
mydata <- data.frame(Group=1:5, Profit=seq(60, 100, by=10),
Count=seq(50000, 10000, by=-10000))
I can plot a bar chart showing the Profit by Group: 我可以绘制一个显示按组分类的利润的条形图:
r1 <- ggplot(data=mydata, aes(x=Group, y=Profit, fill=Group))
r1 +
geom_bar(stat="identity") +
scale_fill_gradient(low="khaki", high="turquoise4") +
labs(title="Group 5 is the most profitable segment")
I can also plot a pie chart showing the proportion of exposure (Count) by Group: 我还可以绘制一个饼图,显示按组分列的曝光比例(计数):
r2 <- ggplot(data=mydata, aes(x="", y=Count, fill=Group))
r2 +
geom_bar(width=1, stat="identity") +
scale_fill_gradient(low="khaki", high="turquoise4") +
coord_polar(theta="y", start=0) +
labs(title="We have significant exposure in lower Groups")
What I'd like to do is combine the above so that the angle of the pie relates to the exposure proportions for each Group level as it is in r2, but also to increase/decrease the size of each "slice" (ie the radius) according to the profit for each Group level in r1. 我想要做的是将上面的内容结合起来,使得饼的角度与每个组级别的曝光比例相关,因为它在r2中,但也增加/减少每个“切片”的大小(即半径) )根据r1中每个组级别的利润。
Any help gratefully received. 任何帮助感激不尽。
Thanks 谢谢
library(dplyr)
mydata %>%
mutate(end_count = cumsum(Count), # or add "/sum(Count)" to make it "out of 100%"
start_count = lag(end_count, default = 0)) %>%
ggplot() +
geom_rect(aes(xmin = start_count, xmax = end_count,
ymin = 0, ymax = Profit, fill=Group)) +
scale_fill_gradient(low="khaki", high="turquoise4") +
coord_polar(theta="x", start=0) +
labs(title="We have significant exposure in lower Groups")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.