Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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")
like image 988
cmirian Avatar asked Sep 04 '25 04:09

cmirian


1 Answers

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

like image 109
Claus Wilke Avatar answered Sep 06 '25 02:09

Claus Wilke