0

I am creating a graph using ggplot showing the evolution of poll-answers of the voting of Brexit, conducted over some months.

The code I have created so far shows every point in time as the specific date Graph:

                ggplot(data=cleandf, aes(x=startdate, y=leave)) + 
                geom_point() + 
                xlab("Date") + ylab("Percentage") + 
                ylim(0.0,0.6) + 
                theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

But as it looks very messy and unreadable, I would like to have only every 10th or 5th date shown, but as the values are characters, I do not know how to do this using other functions I know.

  • 3
    Actually, your data probably isn't a proper date value, it looks like you are using strings. It would be better to convert those string values to dates to get a better looking axis. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Maybe try `aes(x=lubridate::dmy(startdate), y=leave)` – MrFlick Apr 17 '21 at 21:39

1 Answers1

1

Building on the comment above; you can set the break value once it is converted to a date:

 scale_x_date(date_breaks = "2 year", date_labels = "%Y")

Alternatively, if you don't format as a date, you can use:

#Labels from 1985-2020 every 5 years
scale_x_continuous(breaks=seq(1985,2020,5))+  
Tim Assal
  • 677
  • 6
  • 15