Rajesh
Rajesh

Reputation: 35

Reduce the Scale of Date on X-axis in r using ggplot

i have Dateframe 4 columns with 1600 rows and Date as one of the column. while ploting the grouped data in a single chart the X-axis has too many points. I need to reduce the scale with month-year format.

library(scales)

library(ggplot2)

    ggplot(site_sales_day, aes(x=Date, y=log(Sales), color=grouping,group=1,cex=0.2))+geom_line(size=2)+scale_x_date(format = "%m-%Y",breaks = '1 month')

sample data Sample Graph

Upvotes: 2

Views: 1480

Answers (1)

Nicolás Velasquez
Nicolás Velasquez

Reputation: 5908

If what you need is fewer breaks on the x-axis, simply augment the space between breaks by specifying longer time units to the scale_x_date command with the date_break parameter. For instance, draw breaks in quarters with scale_x_date(date_format = "%m-%Y", date_breaks = '3 months')

ggplot(site_sales_day, aes(x=Date, y=log(Sales), color=grouping,group=1,cex=0.2)) +
    geom_line(size=2) +
    scale_x_date(date_format = "%m-%Y", date_breaks = '3 months')

Upvotes: 2

Related Questions