ggplot2 构面图表中 min.n 的 prettyDate 范围错误太小
Error in prettyDate range too small for min.n in ggplot2 facet chart
我有一个包含所有奥运会田径成绩的数据集。
我需要制作一个具有不同类别的 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 折线图。
如果我选择类似的项目(比如 100m 或 400m),我没有问题。
但是对于 100m 和马拉松这样的不同时间训练者,我得到了这个错误
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()
我的同事修复了它,她在转换时间字段时使用了 lubridate 包
ath.sub$time <- lubridate::ymd_hms(ath.sub$time)
我有一个包含所有奥运会田径成绩的数据集。 我需要制作一个具有不同类别的 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 折线图。 如果我选择类似的项目(比如 100m 或 400m),我没有问题。 但是对于 100m 和马拉松这样的不同时间训练者,我得到了这个错误
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()
我的同事修复了它,她在转换时间字段时使用了 lubridate 包
ath.sub$time <- lubridate::ymd_hms(ath.sub$time)