简体   繁体   English

如何使我的极坐标角度与ggplot2中的曝光比例相关?

[英]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.

相关问题 在ggplot2中,如何制作跨因子比例的条形图(并添加误差条)? - In ggplot2, how can I make a bar chart of proportions across factors (and add error bars)? 如何使用 ggplot2 在极坐标中绘制二维热图? - How to draw a 2d heatmap in polar coordinates with ggplot2? R和ggplot2:如何连接折线图和极坐标的点? - R and ggplot2: how do I connect the dots for a line chart and polar coordinates? 如何使用geom_rect环绕ggplot2中的极坐标? - How to wrap around the polar coordinates in ggplot2 with geom_rect? 如何在ggplot2中使用填充美学绘制两组相对比例? - How can I plot the relative proportions of two groups using a fill aesthetic in ggplot2? 如何在ggplot2中制作以不同纵横比保留角度的段? - How to make segments that preserve angles in different aspect ratios in ggplot2? 如何确定ggplot2中角点的x和y坐标? - How can I determine the x and y coordinates of the corners in ggplot2? 如何使用带有“y”的极坐标线重新排列图形作为简单的计数 ggplot2? - How can I reorder the graph using cord polar with 'y' as a simple count ggplot2? 我如何在ggplot2中制作红色的前10行,其余的蓝色基于示例(R,ggplot2) - How I can make in ggplot2 my first 10 lines in red and the rest lines in blue based on example (R, ggplot2) 如何使用ggplot2在R中的极坐标中绘制一年的小时(三月至二月)数据 - How to graph a year's worth of hourly (March - Feb) data in polar coordinates in R with ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM