bic ton
bic ton

Reputation: 1408

How to show on the x-axis the only corresponding years?

Given this simple data:

 df <- tibble(word = c("apple", "apple","banana","pear","pear","A","A","A","A","A","A","A","A","A"),i =seq_along(word),year=c(2000,2001,2000,2000,2001,2000,2001,2002,2000,2002,2003,2004,2008,2009))

I can plot it like this:

ggplot(df, aes(year, word)) +
geom_point(size=4)+labs(x="Year")

However, I need on the x-axis tics to show only the years that correspond to the points

Upvotes: 0

Views: 25

Answers (1)

teunbrand
teunbrand

Reputation: 37933

You can manually set the breaks.

library(ggplot2)

df <- tibble::tibble(
  word = c("apple", "apple","banana","pear","pear","A","A","A","A","A","A","A","A","A"),
  i =seq_along(word), 
  year=c(2000,2001,2000,2000,2001,2000,2001,2002,2000,2002,2003,2004,2008,2009))

ggplot(df, aes(year, word)) +
  geom_point(size = 4) +
  scale_x_continuous(
    breaks = unique(df$year),
    name = "Year"
  )

Created on 2022-01-31 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions