简体   繁体   English

在ggplot2中缩放x轴

[英]Scale x-axis in ggplot2

I have issues with the x-axis when plotting a line graph. 绘制折线图时,x轴存在问题。 As the graph below indicates, I have too many labels. 如下图所示,标签太多。

线形图

I try to remedy the solution by including only some labels using seq() , but I would come across this error: 我尝试通过使用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

Any suggestions? 有什么建议么? If it helps, df$date is considered a character. 如果有帮助,则将df$date视为字符。 Perhaps, I should convert it to numeric or as a datetime? 也许我应该将其转换为数字或日期时间?

Remove scale_x_continuous. 删除scale_x_continuous。 Make sure your date is a date. 确保您的日期是一个日期。 You can do that with as.Date(date). 您可以使用as.Date(date)来实现。 Add below: 在下面添加:

+ scale_x_date(date_breaks = "1 month")

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM