Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change grid line behavior in ggplot2

Tags:

r

ggplot2

Say I have the following data:

require(ggplot2)
set.seed(123)
data <- data.frame(x = sample(1:20, 100, replace = TRUE))

I want to create a dot plot of data$x, so this is what I do:

ggplot(data, aes(x)) +
  geom_dotplot(binwidth = 1) +
  scale_x_continuous(breaks = seq(1, 20, 1)) +
  scale_y_continuous(breaks = NULL)

Which gives me this:

dotplot1

I would like to get rid of those vertical grid lines, so I add theme(line = element_blank()) to my ggplot statement. The problem is that command also eliminates the tick marks, which I would like to keep. How can I hide the grid lines whilst keeping their respective ticks?

I would also like to know how I can change the grid lines so they'll be drawn every 1:20, not at every 0.5 mark.

I've looked for those answers in ?title() and ?geom_dotplot, tried a couple of things, to no avail. Appreciate the help!

like image 574
Waldir Leoncio Avatar asked Jul 24 '13 20:07

Waldir Leoncio


2 Answers

Use panel.grid

theme(panel.grid = element_blank())

enter image description here

If you want the grid lines to be drawn every 1:20, not at every 0.5 mark.

 theme(panel.grid.minor =   element_blank(),
        panel.grid.major =   element_line(colour = "white",size=0.75))

enter image description here

like image 143
agstudy Avatar answered Sep 21 '22 15:09

agstudy


you might find answers here

opts has been changed to theme. but the thing about grid.major.x, grid.minor.y should set on the track

To get rid of the major grid line, for example

ggplot(data, aes(x)) +
    geom_dotplot(binwidth = 1) +
    scale_x_continuous(breaks = seq(1, 20, 1)) +
    scale_y_continuous(breaks = NULL) + theme(panel.grid.major = element_blank())
like image 27
Anto Avatar answered Sep 23 '22 15:09

Anto