以R显示ggplot x轴上的所有时间间隔
我试图在R中的绘图的x轴上创建年度时间间隔,但我不断得到错误: as.Date.numeric(值)中出错:必须提供“origin” 我的数据是这样的以R显示ggplot x轴上的所有时间间隔,r,ggplot2,R,Ggplot2,我试图在R中的绘图的x轴上创建年度时间间隔,但我不断得到错误: as.Date.numeric(值)中出错:必须提供“origin” 我的数据是这样的 category start_date end_date (chr) (date) (date) 1 A 2000-03-16 2011-12-31 2 B 2001-04-04 2011-12-29 3 C 2006-07-18 2010-12-08 4
category start_date end_date
(chr) (date) (date)
1 A 2000-03-16 2011-12-31
2 B 2001-04-04 2011-12-29
3 C 2006-07-18 2010-12-08
4 D 2000-01-01 2006-12-31
5 E 2006-03-13 2008-11-04
我可以用以下代码绘制一个漂亮的图:
ggplotly(
ggplot(
data = mydata,
aes(
x = start_date,
xend = end_date,
y = category,
yend = category, color = category
)
)+ geom_segment(size = 3) + xlab("Year") + ylab("Category") )
但我的x轴并没有显示所有年份。以下是由上述代码生成的绘图:
当我尝试再添加一行代码时(删除前面的一个括号):
我得到了上面的错误
我也试过:
+
scale_x_continuous(breaks = cutoffs ))
截止日期为:
cutoffs = c('01-01-2000', '01-01-2001', '01-01-2002', '01-01-2003', '01-01-2004', '01-01-2005',
'01-01-2006', '01-01-2007','01-01-2008','01-01-2009','01-01-2010','01-01-2011')
cutoffs = as.Date(cutoffs, format = '%d-%m-%Y')
我得到了同样的错误
如有任何反馈,将不胜感激
Yara如果您切换到
刻度盘x\u日期(date\u breaks=“years”)
您可以在刻度盘中添加date\u标签=“%Y”
,只获得刻度盘上的年份。此功能非常好,谢谢!我添加了:+scale\u x\u date(date\u breaks=“years”,date\u labels=“%Y”)
cutoffs = c('01-01-2000', '01-01-2001', '01-01-2002', '01-01-2003', '01-01-2004', '01-01-2005',
'01-01-2006', '01-01-2007','01-01-2008','01-01-2009','01-01-2010','01-01-2011')
cutoffs = as.Date(cutoffs, format = '%d-%m-%Y')