我有一个数据集,其中包含所有奥运会的田径运动成绩。 我需要制作一个具有不同类别的facet ggplot,例如100m和Marathon所以我的子集:
ath.sub <- subset(ath, Event_STD%in%c('100m','Marathon'))
我得到了这个数据框
> head(ath.sub)
Event Event_STD Athlete Country Result Medal YEAR unit Sex time
1261 100m Men 100m Usain Bolt JAM 9.69 GOLD 2008 time Men 2011-01-01
1262 100m Men 100m Donovan Bailey CAN 9.84 GOLD 1996 time Men 2011-01-01
1263 100m Men 100m Justin Gatlin USA 9.85 GOLD 2004 time Men 2011-01-01
1264 100m Men 100m Francis Obikwelu POR 9.86 SILVER 2004 time Men 2011-01-01
1265 100m Men 100m Maurice Greene USA 9.87 GOLD 2000 time Men 2011-01-01
1266 100m Men 100m Maurice Greene USA 9.87 BRONZE 2004 time Men 2011-01-01
> tail(ath.sub)
Event Event_STD Athlete Country Result Medal YEAR unit Sex time
3370 Marathon Women Marathon Valentina Yegorova RUS 2:28.05 SILVER 1996 time Women 2011-01-01 02:28:00
3371 Marathon Women Marathon Yuko Arimori JPN 2:28.39 BRONZE 1996 time Women 2011-01-01 02:28:00
3372 Marathon Women Marathon Valentina Yegorova URS 2:32:41 GOLD 1992 time Women 2011-01-01 02:32:00
3373 Marathon Women Marathon Yuko Arimori JPN 2:32:49 SILVER 1992 time Women 2011-01-01 02:32:00
3374 Marathon Women Marathon Lorraine Moller NZL 2:33.59 BRONZE 1992 time Women 2011-01-01 02:33:00
3375 Marathon Women Marathon Catherine Ndereba KEN <NA> SILVER 2008 time Women <NA>
> str(ath.sub)
'data.frame': 236 obs. of 10 variables:
$ Event : chr "100m Men" "100m Men" "100m Men" "100m Men" ...
$ Event_STD: chr "100m" "100m" "100m" "100m" ...
$ Athlete : chr "Usain Bolt" "Donovan Bailey" "Justin Gatlin" "Francis Obikwelu" ...
$ Country : chr "JAM" "CAN" "USA" "POR" ...
$ Result : chr "9.69" "9.84" "9.85" "9.86" ...
$ Medal : chr "GOLD" "GOLD" "GOLD" "SILVER" ...
$ YEAR : int 2008 1996 2004 2004 2000 2004 1996 2008 1996 2008 ...
$ unit : chr "time" "time" "time" "time" ...
$ Sex : chr "Men" "Men" "Men" "Men" ...
$ time : chr "2011-01-01 00:00:09.69" "2011-01-01 00:00:09.84" "2011-01-01 00:00:09.85" "2011-01-01 00:00:09.86" ...
然后我转换posixct中的时间字段
> ath.sub$time<-as.POSIXct(ath.sub$time,tz = 'GMT')
> str(ath.sub$time)
POSIXct[1:236], format: "2011-01-01 00:00:00" "2011-01-01 00:00:00" "2011-01-01 00:00:00" "2011-01-01 00:00:00" ...
正如我之前所写,我需要制作一个ggplot facet折线图。 如果我选择类似的学科(如100米或400米),我没有问题。 但是对于像100米和马拉松这样不同的时间训练我得到了这个错误
Error in prettyDate(x = x, n = n, min.n = min.n, sep = sep, ...) :
范围太小'min.n'
这是ggplot代码
gg.ath<- ggplot(ath.sub, aes( YEAR, time, colour=Sex))+
facet_wrap(~Event_STD, scales = 'free')+
scale_y_datetime()+
scale_x_continuous(breaks = ath.sub$YEAR)+
geom_line()+
geom_smooth()
答案 0 :(得分:1)
我的同事修好了,她在转换时间字段时使用了lubridate包
ath.sub$time <- lubridate::ymd_hms(ath.sub$time)