Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control y-axis ticks and x-axis ticks independently in ggplot2?

Tags:

r

ggplot2

I want to remove axis ticks from the x-axis without removing them from the y-axis.

Right now, I can get both to be removed using:

axis.ticks=theme_blank()

For instance:

# Generate data
c <- ggplot(mtcars, aes(factor(cyl)))

c + geom_bar()+opts(axis.ticks=theme_blank())
#c + geom_bar(width=.5)
#c + geom_bar() + coord_flip()
#c + geom_bar(fill="white", colour="darkgreen")

But I don't know how to control them independently.

like image 993
Atticus29 Avatar asked Mar 25 '13 17:03

Atticus29


People also ask

How do I specify tick marks in ggplot2?

The default value of Y-axis tick marks using ggplot2 are taken by R using the provided data but we can set it by using scale_y_continuous function of ggplot2 package. For example, if we want to have values starting from 1 to 10 with a gap of 1 then we can use scale_y_continuous(breaks=seq(1,10,by=1)).

How do you change the x and y axis in ggplot2?

Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.

How do you set Y axis ticks?

Specify y-Axis Tick Values for Specific Axes Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Set the y-axis ticks for the lower plot by passing ax2 as the first input argument to the yticks function.

How do you change the X axis ticks in R?

Option 1. Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks.


1 Answers

To remove just x axis ticks use axis.ticks.x=

 c <- ggplot(mtcars, aes(factor(cyl))) 
 c + geom_bar()+opts(axis.ticks.x=theme_blank())

For the latest ggplot2 version (0.9.3) instead of opts() use theme() and element_blank().

 c <- ggplot(mtcars, aes(factor(cyl))) 
 c + geom_bar()+theme(axis.ticks.x=element_blank())
like image 112
Didzis Elferts Avatar answered Sep 28 '22 17:09

Didzis Elferts