Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add axis ticks and labels in ggplot2 r

Tags:

r

label

ggplot2

Here is data.

X <- 1:10
Y <- rnorm (length(X), 5, 2)
ticks <- data.frame (t = c(5, 8, 9), l = c(1:3)) 

plot (X, Y, xaxt = "n")

axis(1, at = ticks$t, labels = ticks$l)

I want to do similar job in ggplot2. How can I do it

enter image description here

like image 592
SHRram Avatar asked Dec 13 '22 02:12

SHRram


1 Answers

library(ggplot2)

X <- 1:10
Y <- rnorm (length(X), 5, 2)
ticks <- data.frame (t = c(5, 8, 9), l = c(1:3))

df <- data.frame(X, Y)


p <- ggplot(data=df, aes(x = X,y = Y) )
p <- p + scale_x_continuous(breaks=c(ticks$t), 
                            labels=c(ticks$l))
p <- p + geom_point()
p <- p + theme_bw()
p

Hope this helps.

axis labels and breaks demo ggplot2

like image 168
daedalus Avatar answered Jan 10 '23 10:01

daedalus