watchtower
watchtower

Reputation: 4298

Labels in ggplot2 point labels and Breaks

I have recently started using ggplot2, and I am depending on help manual to understand how ggplot2 works.

Objective: Customize tick marks in graph.

a) x-axis

Here's what I did:

df <- data.frame(x = c(1, 3, 5) * 1000, y = 1)
 axs <- ggplot(df, aes(x, y)) +
   geom_point() +
   labs(x = NULL, y = NULL)
 axs
 axs + scale_x_continuous(breaks = c(1000,3000, 5000), labels = c("1k","3k","5k")) #works  

This works well, but I wanted to print zero as well. So, here's what I did:

axs + scale_x_continuous(breaks = c(0,1000,3000, 5000), labels = c("0","1k","3k","5k")) 
#works but doesn't show zero. 

I wasn't sure, so I thought of printing even numbers.

axs + scale_x_continuous(breaks = c(0,2000,4000, 6000), labels = c("0","2k","4k","6k")) 
#doesn't work at all.

This shows 2k and 4k, but not 0 and 6k. I am not sure why.

b) y-axis I did similar thing for y-axis without any success.

windows()
 t1<-c(0,1,2,3)
 axs + scale_y_continuous(breaks = t1, labels = c("0","1","2","3")) #doesn't work.

This only prints "1" and I don't see other points at all. I am not sure why.

I looked at another thread on SO: Trim first and last labels in ggplot2 However, this thread seems to be focused on printing date format.

Can someone please help me? I am a beginner, so this question might sound too naive for some of you. I am sorry for this.

Upvotes: 3

Views: 4867

Answers (1)

conor
conor

Reputation: 1314

You need to change the limits of the plot, so try

axs + scale_x_continuous(limit = c(0,5000), 
                         breaks = c(0,1000,3000, 5000), 
                         labels = c("0","1k","3k","5k")) 

Same thing with the y-axis

axs + scale_y_continuous(limit = c(0,3),
                         breaks = t1,
                         labels = c("0","1","2","3"))

Upvotes: 1

Related Questions