【问题标题】:Adding geom_hline to density plot using coord_polar()使用 coord_polar() 将 geom_hline 添加到密度图
【发布时间】:2011-12-26 21:41:22
【问题描述】:

我需要使用 R 和 ggplot2 在我的密度图中添加一条参考线。

因为我使用的是极坐标,所以这应该在我的情节中间形成一个圆圈。我使用geom_hline(yintercept = .5),但是当我添加coord_polar() 时,我的绘图上没有显示任何线条。

这是我的代码。

ggplot(flights_sample2, aes(x = radians, fill = factor(nf, levels = c(8:0)))) +
    geom_bar(binwidth = pi/18, position = "fill") +
    scale_x_continuous(limits = c(0, 2*pi), breaks = c(0,pi/2, pi, 3*pi/2), 
                                            labels = c("N", "E", "S", "W")) +
    coord_polar() + 
    xlim(0,2*pi) + 
    geom_hline(yintercept = .5)

有什么建议吗?

【问题讨论】:

  • 您的示例不可重现。

标签: r ggplot2 polar-coordinates


【解决方案1】:

如果你先用 geom_line 设置参考,然后加上 coord_polar() 你可以得到一个参考圆:

xx=seq(0,2*pi,length=10)
yy=rep(2,10)
g <- ggplot(flights_sample2, aes(x = radians, fill = factor(nf, levels = c(8:0)))) +
    geom_bar(binwidth = pi/18, position = "fill") +
    scale_x_continuous(limits = c(0, 2*pi), breaks = c(0,pi/2, pi, 3*pi/2), 
                                            labels = c("N", "E", "S", "W")) +
        xlim(0,2*pi) + 
    geom_hline(aes(x=xx, y=yy))

g+coord_polar()

显然这没有使用您的数据和代码进行测试,但它适用于帮助(coord_polar)页面中的示例

【讨论】:

    猜你喜欢
    • 2019-08-21
    • 1970-01-01
    • 2016-12-21
    • 2022-06-11
    • 2015-11-08
    • 1970-01-01
    • 2018-12-22
    • 2014-01-21
    相关资源
    最近更新 更多