当ggplot用极坐标绘制线形图时,它在最高x值和最低x值(以下为DecJan)之间留有间隙,而不是缠绕成螺旋形。我如何才能继续前进并缩小差距?

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

代表:

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

r - 连接极线ggplot图中的间隙-LMLPHP

最佳答案

这是一个有点骇人听闻的选择:

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

r - 连接极线ggplot图中的间隙-LMLPHP

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

关于r - 连接极线ggplot图中的间隙,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41842249/

10-13 00:51