【问题标题】:ggplot2: How to add closed (and filled) arrowheads to a polar plotggplot2:如何将封闭(和填充)箭头添加到极坐标图
【发布时间】:2015-10-03 13:56:23
【问题描述】:

点击此链接:

ggplot2 polar plot arrows

我一直想知道如何在 ggplot2 的极坐标图上获得闭合箭头。

这是我的代码:

# make some data     
    polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, 
    -60L, 0L), value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), .Names = c("degree", 
    "value"), class = "data.frame", row.names = c(NA, -8L))

    require(ggplot2)
    require(grid)

    base <- ggplot(polar, aes(x=degree, y=value, axis.x = NULL, axis.y = NULL))
    p <- base 
    p <- p + geom_segment(aes(y=0, xend=degree, yend=value)) # add the lines


    awid <- 1
    p <- p + geom_segment(aes(y=0, xend=degree, yend=value)) + geom_segment(aes(y=value-0.05,yend=value,x=degree-awid/value,xend=degree)) +  geom_segment(aes(y=value-0.05,yend=value,x=degree+awid/value,xend=degree))
    p

但是,我得到的箭头是开放的箭头,而不是封闭的。我想知道如何用(基本上)填充的三角形替换箭头并且仍然处于极坐标中。有什么建议么?

提前致谢!

PS;更新以添加箭头问题。这是我运行的代码:

 require(ggplot2)
 require(grid)

 # Data     
 polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, -60L, 0L), 
                         value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), 
                    .Names = c("degree", "value"), 
                    class = "data.frame", row.names = c(NA, -8L))

 ggplot(polar, aes(x=degree, y=value)) +
   geom_segment(aes(y=0, xend=degree, yend=value), arrow=arrow(type="closed")) +
   coord_polar()

这是我得到的情节:

【问题讨论】:

    标签: r ggplot2 polar-coordinates


    【解决方案1】:

    使用grid 中的arrow() 函数,您只需调用一次geom_segment。

     require(ggplot2)
     require(grid)
    
     # Data     
     polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, -60L, 0L), 
                             value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), 
                        .Names = c("degree", "value"), 
                        class = "data.frame", row.names = c(NA, -8L))
    
     ggplot(polar, aes(x=degree, y=value)) +
       geom_segment(aes(y=0, xend=degree, yend=value), arrow=arrow(type="closed")) +
       coord_polar()
    

    【讨论】:

    • 谢谢!你能给我完整的代码吗?我早些时候(现在)试过这个,箭头都指向西。
    • 我已经添加了我运行的所有代码。我真正做的只是导入您的polar 数据,然后在我的原始答案中运行ggplot 代码。
    • 我也无法重现这个。
    • 嗯。我在一个干净的会话中运行代码并得到了相同的结果。我在 OSX 10.10.4 上运行 R 3.2.2、RStudio 0.99.484 和 ggplot2 版本 1.0.1.9003。
    • 我也在 R.3.2.2 上。我的 ggplot2 也是 1.0.1。 \n packageVersion("grid") [1] ‘3.2.2’ > packageVersion("ggplot2") [1] ‘1.0.1’
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多