假设我在R中有以下数据:
mydata <- data.frame(Group=1:5, Profit=seq(60, 100, by=10),
Count=seq(50000, 10000, by=-10000))
我可以绘制一个条形图,按组显示利润:
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")
我还可以绘制一个饼图,按组显示曝光(计数)的比例:
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")
我想要做的是结合上面的内容,这样饼的角度与每个组级别的曝光比例有关,就像在r2中一样,但也可以根据r1中每个组级别的利润来增加/减少每个“切片”的大小(即半径)。
感谢您给予的任何帮助。
谢谢
发布于 2019-06-19 14:22:20
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")
https://stackoverflow.com/questions/56664559
复制相似问题