curious
curious

Reputation: 710

How to add empty value on x axis for line plot in ggplot

df <- data.frame(value = c(1, 2, 1.5), bin = c('B', 'C', 'D'))
p <- ggplot(data = df, aes(x = df$bin, y = df$value, group = 1)) + geom_line() + geom_point()

I want an x tick for 'A' that has no value or line it it. I have other plots that do have a value for 'A' and I want to make all axes consistent.

I was hoping this would work, but it does not:

p + scale_x_discrete(breaks = c('A', 'B', 'C', 'D'))

enter image description here

Upvotes: 0

Views: 1057

Answers (1)

Edo
Edo

Reputation: 7818

use this instead:

p + scale_x_discrete(limits = c('A', 'B', 'C', 'D'))

enter image description here


Also, it's better if you don't use df$ inside aes.

Better this way:

p <- ggplot(data = df, aes(x = bin, y = value, group = 1)) +
  geom_line() + 
  geom_point()

Upvotes: 1

Related Questions