【问题标题】:R - ggplot2: coord_radar not working with geom_rect or annotate('rect')R - ggplot2:coord_radar 不适用于 geom_rect 或 annotate('rect')
【发布时间】:2017-05-25 10:04:43
【问题描述】:

我正在处理此处讨论过的类似问题: ggplot - connecting points in polar coordinates with a straight lineggplot2: connecting points in polar coordinates with a straight line 2

我想要与coord_polarggiraphExtra 包中的函数coord_radar() 成直线,这几乎让我到达了我想要的位置,即(来自之前的回复,请参见上面的链接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()

但是,我想使用 annotate('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

任何见解都将不胜感激。抱歉,如果这在其他地方得到解决,我很确定我已经查看了有关该主题的所有问题。 谢谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我不知道错误来自哪里,我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)
    

    【讨论】:

    • 谢谢,它确实很好用。我不完全确定为什么多边形可以工作,但线不行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多