简体   繁体   English

R ggplot2:向coord_polar添加另一个几何

[英]R ggplot2: Adding another geom to coord_polar

I have a plot i wish to add another layer to Th plot is below. 我有一个图,我想在下面的图上添加另一层。 I want to overlay another polar plot on it to see that the numbers "match up" 我想在上面叠加另一个极坐标图,以查看数字“匹配”

In the example below I have create the plot for one species of the iris dataset. 在下面的示例中,我为虹膜数据集的一种创建了绘图。 I would like to overlay another plot of a different species 我想覆盖另一个不同物种的地块

Thank you for your time 感谢您的时间

library(ggplot2)
library(dplyr)

mydf <- iris
plot.data <- tidyr::gather(mydf,key = attribute ,value = avg_score, Sepal.Length:Petal.Width)


plot.data <- plot.data %>% 
   filter(Species == 'setosa') %>% 
   group_by(attribute) %>% 
   summarise(attr_mean = mean(avg_score))

ggplot(plot.data, aes(x=attribute, y = attr_mean, col = attribute)) +
   geom_bar(stat = "identity", fill = 'white') +
  coord_polar(theta = "x") +
  theme_bw() 

This is quite the pedestrian way of doing things. 这是行人的行事方式。

plot.setosa <- plot.data %>% 
  filter(Species == 'setosa') %>% 
  group_by(attribute) %>% 
  summarise(attr_mean = mean(avg_score))

plot.virginica <- plot.data %>% 
  filter(Species == 'virginica') %>% 
  group_by(attribute) %>% 
  summarise(attr_mean = mean(avg_score))

ggplot(plot.setosa, aes(x=attribute, y = attr_mean, col = attribute)) +
  geom_bar(stat = "identity", fill = 'blue', alpha = 0.25) +
  geom_bar(data = plot.virginica, stat = "identity", fill= "green", alpha = 0.25,
           aes(x = attribute, y = attr_mean, col = attribute)) + 
  coord_polar(theta = "x") +
  theme_bw()

在此处输入图片说明

And a slightly less pedestrian. 和行人少一点。

xy <- plot.data %>%
  group_by(Species, attribute) %>%
  summarise(attr_mean = mean(avg_score))

ggplot(xy, aes(x = attribute, y = attr_mean, color = attribute, fill = Species)) +
  theme_bw() +
  geom_bar(stat = "identity", alpha = 0.25) +
  coord_polar(theta = "x")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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