2017-05-25 92 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

謝謝你,它工作得很好確實如此。我不完全確定要理解爲什麼多邊形可行,但行不行。 –