cmirian
cmirian

Reputation: 2253

How can I add specific value to x-axis in ggplot2?

I am trying to make a graph in ggplot2. I want the x-axis to show 2.84 along with the sequence typed below. Is there any other way beside typing all the exact values in breaks()? I tried google but it doesn't solve my problem.

scale_x_continuous(limits = c(1, 7), seq(1,7,by=0.5), name = "Number of 
treatments")

Upvotes: 4

Views: 8383

Answers (1)

Claus Wilke
Claus Wilke

Reputation: 17820

You can programmatically generate specific breaks, like this:

# make up some data
d <- data.frame(x = 6*runif(10) + 1,
                y = runif(10))

# generate break positions
breaks = c(seq(1, 7, by=0.5), 2.84)
# and labels
labels = as.character(breaks)

# plot
ggplot(d, aes(x, y)) + geom_point() + theme_minimal() +
  scale_x_continuous(limits = c(1, 7), breaks = breaks, labels = labels,
                     name = "Number of treatments")

enter image description here

Upvotes: 6

Related Questions