加入极线ggplot图中的间隙

时间:2017-01-25 02:06:08

标签: r plot ggplot2 polar-coordinates

当ggplot使用极坐标绘制线图时,它会在最高和最低x值(下面的DecJan)之间留下间隙,而不是缠绕成螺旋线。我怎样才能继续这条线并缩小差距呢?

特别是,我想用月份作为我的x轴,但是在一个循环线中绘制多年的数据。

Reprex:

library(ggplot2)

# three years of monthly data
df <- expand.grid(month = month.abb, year = 2014:2016)
df$value <- seq_along(df$year)

head(df)
##   month year value
## 1   Jan 2014     1
## 2   Feb 2014     2
## 3   Mar 2014     3
## 4   Apr 2014     4
## 5   May 2014     5
## 6   Jun 2014     6

ggplot(df, aes(month, value, group = year)) + 
    geom_line() + 
    coord_polar()

spiral plot with gaps

1 个答案:

答案 0 :(得分:4)

这是一个有点蠢的选择:

# make a data.frame of start values end values should continue to
bridges <- df[df$month == 'Jan',]
bridges$year <- bridges$year - 1    # adjust index to align with previous group
bridges$month <- NA    # set x value to any new value

       # combine extra points with original
ggplot(rbind(df, bridges), aes(month, value, group = year)) + 
    geom_line() + 
    # close gap by removing expansion; redefine breaks to get rid of "NA/Jan" label
    scale_x_discrete(expand = c(0,0), breaks = month.abb) + 
    coord_polar()

spiral plot without gaps

显然,添加额外的数据点并不理想,因此可能存在更优雅的答案。