2017-02-18 53 views
0

我有一个图形,我希望将另一图层添加到 Th图形在下面。我想叠加另一个极坐标图,看看数字“匹配”R ggplot2:向coord_polar添加另一个几何图形

在下面的示例中,我创建了一个虹膜数据集的物种的图。我想覆盖不同种类

感谢您的另一地块为您的时间

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

回答

2

这是做事相当的行人路。

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

enter image description here

和稍微少行人。

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

enter image description here

+0

太谢谢你了。这非常聪明 –