简体   繁体   中英

How can I make the angles of my polar coordinates relate to exposure proportions in ggplot2?

Suppose I have the following data in 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.

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") 

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM