【问题标题】:Join gap in polar line ggplot plot在极线ggplot图中加入间隙
【发布时间】:2017-06-10 01:57:58
【问题描述】:

当 ggplot 使用极坐标绘制线图时,它会在最高和最低 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 plot ggplot2 polar-coordinates


【解决方案1】:

这是一个有点老套的选项:

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

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

【讨论】:

  • 如果您以连续比例绘制,那么在概念上为什么需要额外的数据点应该会变得更加明显:ggplot(df, aes(as.integer(month), value, group = year)) + geom_line() + coord_polar() + scale_x_continuous(limits = c(0, 12))geom_line 无法推断。在我看来,ggplot2 甚至用离散的比例绘制它是一个错误。请注意它与笛卡尔坐标不同。
  • 请参阅my answer 的变体 2。它将几个月转换为 POSIXct 并使用 scale_x_date()
猜你喜欢
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 2016-05-20
相关资源
最近更新 更多