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

我有点卡住在极坐标图中获取一条线的端点以连接起始点.

我的数据:

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())

这或多或少会产生我想要的东西.但是,我喜欢从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连接:

接下来我尝试了:

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())

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

我也尝试了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())

这使:

如您所见,有一条介于350和360之间的线.对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())

这导致:

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

我的问题:

>当将geom_line与coord_polar结合使用时,如何在极坐标图中连接端点和起始点,y轴从0开始?
>或者在将geom_polygon / geom_path与coord_polar结合使用时,没有在middel中获得圆圈?

注意:
原始数据集没有ri = 0的行.我自己添加了这一行.它是ri = 360行的重复.

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

>使用df< -df [df $ri!= 360,]删除行ri = 360(ri = 0的副本)
>将ri转换为因子&将group = 1添加到aes中.
>将start = -pi * 1/36添加到coord_polar以获得极坐标图顶部的0

这导致以下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())

由此产生的情节:

点赞