连接极线ggplot图中的间隙 [英] Join gap in polar line ggplot plot
本文介绍了连接极线ggplot图中的间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当ggplot用极坐标绘制线图时,它在最高x值和最低x值(下面的Dec
和Jan
)之间留有间隙,而不是缠绕成螺旋状.我怎样才能继续做下去并缩小差距?
When ggplot makes a line plot with polar coordinates, it leaves a gap between the highest and lowest x-values (Dec
and Jan
below) instead of wrapping around into a spiral. How can I continue the line and close that gap?
特别是,我想使用几个月作为我的x轴,但是在一条循环线中绘制多年的数据.
In particular, I want to use months as my x-axis, but plot multiple years of data in one looping line.
代表:
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()
推荐答案
这是一个有点棘手的选项:
Here's a somewhat-hacky option:
# 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()
显然,添加额外的数据点并不理想,因此可能存在一个更优雅的答案.
Obviously adding extra data points is not ideal, though, so maybe a more elegant answer exists.
这篇关于连接极线ggplot图中的间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文