2017-05-25 44 views
2

我与已经在这里讨论了类似的问题,工作geom_rect或注释(“矩形”)工作: ggplot - connecting points in polar coordinates with a straight lineggplot2: connecting points in polar coordinates with a straight line 2的R - GGPLOT2:coord_radar不

我想直线与coord_polar和功能coord_radar()ggiraphExtra包差点被我在哪里,我想是的,即(从以前的响应见上面的链接2)

iris %>% gather(dim, val, -Species) %>% 
    group_by(dim, Species) %>% summarise(val = mean(val)) %>% 
    ggplot(aes(dim, val, group=Species, col=Species)) + 
    geom_line(size=2) + ggiraphExtra:::coord_radar() 

不过,我想背景颜色添加到该PL使用注释('rect')(或geom_rectangle);但是当我做:

iris %>% gather(dim, val, -Species) %>% 
    group_by(dim, Species) %>% summarise(val = mean(val)) %>% 
    ggplot(aes(dim, val, group=Species, col=Species)) + 
    geom_line(size=2) + 
    annotate("rect", xmin=0, xmax =2.5 , ymin = -Inf, ymax = Inf, alpha=0.3, fill="#C77CFF")+ 
    annotate("rect", xmin=2.5, xmax =4.5 , ymin = -Inf, ymax = Inf, alpha=0.3, fill="#619CFF")+ 
    ggiraphExtra:::coord_radar() 

我收到以下错误信息: Error in ``$<-.data.frame*tmp*``, "r", value = numeric(0)) : replacement has 0 rows, data has 1

此错误是有点晦涩给我,我不知道怎么用它做注意。相同的代码与coord_polar作品就好在这里看到: polar coord with coloured background

任何见解将不胜感激道歉,如果这解决了别的地方,我敢肯定,我查看了有关此主题的所有问题 比。 k你

回答

0

我不知道从哪里来的错误,我guess它不容易调试。但是,为什么不使用多边形:

p <- iris %>% gather(dim, val, -Species) %>% 
    group_by(dim, Species) %>% summarise(val = mean(val)) %>% 
    ggplot(aes(dim, val, group=Species, col=Species)) 
annotPolar <- list(
    annotate("rect",xmin=0, xmax =2.5 , ymin = -Inf, ymax = Inf, alpha=0.3, fill="#C77CFF"), 
    annotate("rect", xmin=2.5, xmax =4.5 , ymin = -Inf, ymax = Inf, alpha=0.3, fill="#619CFF"), 
    coord_polar() 
) 

lst <- list(
    p + 
    geom_polygon(size=2, fill=NA, show.legend = F) + 
    geom_line(size = NA) + 
    guides(color = guide_legend(override.aes = list(size = 2))) + 
    annotPolar + 
    ggtitle("polygon"), 
    p + 
    geom_line(size=2) + 
    annotPolar + 
    ggtitle("line"), 
    p + 
    geom_line(size=2) + 
    ggiraphExtra:::coord_radar() + 
    ggtitle("radar") 
) 
gridExtra::grid.arrange(grobs=lst, ncol = 2) 

enter image description here

+0

谢谢你,它工作得很好确实如此。我不完全确定要理解为什么多边形可行,但行不行。 –