ggplot2中的缩放x轴

问题描述:

绘制折线图时,我对x轴有问题。如下图所示,我的标签太多了。ggplot2中的缩放x轴

Line graph

我尝试只包括使用seq()一些标签来补救方案,但我会遇到这样的错误:

ggplot(df, aes(x = date, y = measure, group = 1)) + 
    geom_line() + 
    theme_bw() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) + 
    xlab("X-Axis") + 
    ylab("Y-Axis") + 
    scale_x_continuous(breaks = round(seq(min(df$date), max(df$date), by = 221.2), 1)) 

#> Error in seq.default(min(df$date), max(df$date), : 
#> 'from' must be a finite number 
#> In addition: Warning message: 
#> In seq.default(min(df$date), max(df$date), : 
#> NAs introduced by coercion 

有什么建议?如果有帮助,df$date被认为是一个角色。也许,我应该将其转换为数字或作为日期时间?

+1

请提供用于生产该地块您'df' – useR

删除scale_x_continuous。确保你的日期是一个日期。你可以用as.Date(日期)来做到这一点。以下地址:

+ scale_x_date(date_breaks = "1 month") 

来源: http://ggplot2.tidyverse.org/reference/scale_date.html