![](/img/trans.png)
[英]In ggplot2, how can I make a bar chart of proportions across factors (and add error bars)?
[英]How can I make the angles of my polar coordinates relate to exposure proportions in ggplot2?
假設我在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中每個組級別的利潤。
任何幫助感激不盡。
謝謝
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.