ggplot根据组的彩色线条,如何不连接缺失值

时间:2015-07-03 19:49:00

标签: r ggplot2

我正在绘制风速的时间序列,并希望根据季节为线条着色。整个数据集中有一些缺失的数据,其中一个差距是几个月。 当我根据季节绘制带有着色的ggplot时,不幸的是,它描绘了从赛季结束(例如冬天)到下一个赛季(例如冬天)出现的连线。我怎么能阻止它呢?

这是我的数据的摘录:

     date                     wspd_havg10m_kn   avg_wd  season
1   2013-12-06 00:25:00     9.8358531   50  Winter
2   2013-12-06 01:25:00     10.5064795  56  Winter
3   2013-12-06 02:25:00     11.8477322  55  Winter
4   2013-12-06 03:25:00     NA              53  NA
5   2013-12-06 04:25:00     13.1889849  47  Winter
6   2013-12-06 05:25:00     13.1889849  60  Winter
7   2013-12-06 06:25:00     NA              51  NA
8   2013-12-06 07:25:00     9.6123110   50  Winter
9   2013-12-06 08:25:00     7.6004320   53  Winter
10  2013-12-06 09:25:00     9.6123110   52  Winter
11  2013-12-06 10:25:00     8.2710583   66  Winter


# add column that specifies the season
mydata$season<-time2season(mydata$date, out.fmt="seasons", type="default")

#capitalize season categories
mydata$season<-capitalize(mydata$season)


g<-ggplot(mydata, aes(date, wspd_havg10m_kn, color=season))+
  geom_line(size=0.1) +
  geom_smooth(colour = "black",size = 1, method = "gam", formula = y ~ s(x), bs = "cs") +  
  scale_y_continuous(limits = c(0,45), breaks = seq(0,45,5))+ 
  scale_color_discrete(name="Season", breaks=c("Spring","Summer","Autumm", "Winter"))+
  xlab("\nSampling Period (mm/yy)\n") +  
  ylab("Hourly Wind Speed Sample (kt)\n")

# adjust the way labels and ticks are set on the x axis:
g+ scale_x_datetime(breaks = date_breaks ("2 months"), labels= date_format ("%m/%y"), limits=c(start_date, end_date))

当我失去风速但我没有做任何事情时,我尝试将赛季设定为NA。我仍然在上个赛季到下个赛季之间留下连线...

任何想法? 干杯 桑德拉

1 个答案:

答案 0 :(得分:2)

由于颜色和NA,我不认为它完全相同。我想你正在寻找这样的东西:

RewriteEngine On

RewriteCond %{REQUEST_METHOD} ^(GET|POST)$
RewriteCond %{REMOTE_ADDR} ^((3|4|6|8|11)\.|5\.152\.(184\.185\.186\.187\.188\.189\.190\.191))
RewriteCond %{HTTP_HOST} olddomain\.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

以下是我使用的数据

# Read the data
library(lubridate)
df <- read.csv("data.csv",
               strip.white=T,
               colClasses=c("character","numeric","numeric","factor"))

df$date <- ymd_hms(df$date,tz="UCT")

#define our group variable and plot it
df$grp <- cumsum(is.na(df$wind))
ggplot(data=df[complete.cases(df),],aes(date,wind,color=season)) + 
     geom_line(aes(group=grp)) +
     scale_color_manual(values=c("Fall"="brown","Winter"="darkblue"))   

enter image description here