如何连接端点&极坐标图中的geom_line起点(coord_polar)?

时间:2014-11-11 21:03:39

标签: r ggplot2 geometry line-plot

我有点担心在极坐标图中获取一条线的端点以连接起始点。

我的数据:

df <- structure(list(ri = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360),
                 n = c(329L, 315L, 399L, 700L, 919L, 757L, 656L, 918L, 1117L, 976L, 878L, 803L, 811L, 1072L, 1455L, 1642L, 1891L, 1688L, 1553L, 1841L, 2061L, 2321L, 2498L, 2080L, 1595L, 1080L, 1002L, 953L, 729L, 604L, 538L, 489L, 535L, 455L, 328L, 351L, 329L),
                 d = c(0.008581340149717, 0.00821617673909074, 0.0104071572028483, 0.0182581705313128, 0.0239703695975378, 0.0197449072745768, 0.017110514097916, 0.0239442864967787, 0.0291348235478234, 0.0254571063408018, 0.022900962466418, 0.0209447299094916, 0.0211533947155638, 0.0279610840136675, 0.0379509116043715, 0.0428284514463079, 0.0493231435353035, 0.0440282740812228, 0.0405070554787553, 0.0480189884973526, 0.0537572706643366, 0.0605388768616813, 0.0651555856960275, 0.0542528495787579, 0.0416025457106341, 0.0281697488197397, 0.0261352669605363, 0.0248571950233444, 0.0190145804533243, 0.015754192858447, 0.0140327082083518, 0.0127546362711599, 0.0139544589060748, 0.0118678108453533, 0.00855525704895798, 0.0091551683664154, 0.008581340149717)),
            .Names = c("ri", "n", "d"), row.names = c(NA, 37L), class = c("tbl_df", "tbl", "data.frame"))

我首次尝试使用此代码创建极坐标图:

ggplot(df, aes(x=ri, y=d)) +
  geom_line() +
  scale_x_continuous(breaks=seq(0,360,10)) +
  coord_polar() +
  theme_bw() +
  theme(panel.grid.minor=element_blank())

enter image description here

这或多或少会产生我想要的东西。但是,我喜欢从0开始的y轴。 因此,我使用了以下代码:

ggplot(df, aes(x=ri, y=d)) +
  geom_line() +
  scale_x_continuous(breaks=seq(0,360,10)) +
  scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +
  coord_polar() +
  theme_bw() +
  theme(panel.grid.minor=element_blank())

很遗憾,现在该行以350结尾,但未与0/360相关联: enter image description here

接下来我尝试了:

ggplot(df, aes(x=ri, y=d)) +
  geom_polygon(fill=NA, color="black") +
  scale_x_continuous(breaks=seq(0,360,10)) +
  scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +
  coord_polar() +
  theme_bw() +
  theme(panel.grid.minor=element_blank())

此代码确实将端点与起点连接,但也创建了一个圆: enter image description here

我也尝试了geom_path,但结果与geom_polygon相同。进一步分析问题,我试图用:

制作一个正常的情节
ggplot(df, aes(x=ri, y=d)) +
  geom_line() +
  scale_x_continuous(expand=c(0,0), breaks=seq(0,360,10)) +
  scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +
  theme_bw() +
  theme(panel.grid.minor=element_blank())

给出: enter image description here

如您所见,350360之间有一条线。对geom_plygon执行相同的操作:

ggplot(df, aes(x=ri, y=d)) +
  geom_polygon(fill=NA, color="black") +
  scale_x_continuous(expand=c(0,0), breaks=seq(0,360,10)) +
  scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +
  theme_bw() +
  theme(panel.grid.minor=element_blank())

导致: enter image description here

同样,使用geom_path代替geom_polygon会得到相同的结果。因此,问题似乎是通过将y轴的限制与coord_polar组合设置而产生的。

我的问题:

  1. 当使用0geom_line结合使用时,如何将极坐标图中的端点和起点连接到y轴,从coord_polar开始?
  2. 在将geom_polygon / geom_pathcoord_polar结合使用时,或者在middel中没有圆圈?
  3. 注意:
    原始数据集没有ri=0的行。我自己添加了这一行。它是ri=360行的重复。

2 个答案:

答案 0 :(得分:2)

经过一些追踪和错误后,我通过以下步骤得到了我想要的东西:

  1. 使用ri=360

  2. 删除ri=0的行(与df <- df[df$ri!=360,]重复)
  3. ri转换为因子&amp;将group=1添加到aes

  4. start=-pi*1/36添加到coord_polar以便将0置于极坐标图的顶部

  5. 这会产生以下ggplot代码:

    ggplot(df, aes(x=factor(ri), y=d, group=1)) +
      geom_polygon(fill=NA, color="black") +
      scale_y_continuous(limits=c(0,0.07), breaks=seq(0,0.06,0.01)) +
      coord_polar(start=-pi*1/36) +
      theme_bw() +
      theme(panel.grid.minor=element_blank())
    

    结果图:

    enter image description here

答案 1 :(得分:0)

如何设置y比例的限制是一个副作用。使用:

ggplot(dat, aes(x=ri, y=d)) +
  geom_line() +
  scale_x_continuous(expand=c(0,0), breaks=seq(0,360,10)) +
  scale_y_continuous(breaks=seq(0,0.06,0.01)) +
  coord_polar() +
  theme_bw() +
  theme(panel.grid.minor=element_blank())

enter image description here

这很奇怪,因为值&gt; 0不应该被ylim(0, 0.07)截断,但它是......