Ewa Michalska
Ewa Michalska

Reputation: 95

How to only show selected x-axis tick-labels using ggplot

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.

Upvotes: 0

Views: 2876

Answers (1)

Tim Assal
Tim Assal

Reputation: 687

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))+  

Upvotes: 1

Related Questions