Theme manipulation in ggplot2: altering x and y grid lines
Asked Answered
I

2

4

Is it possible to manipulate the spacing and size of dashed and dotted grid lines in ggplot using the themes? The following plot:

p + l + opts(panel.grid.major = theme_line(colour = 'black', linetype = 'dashed'), 
             panel.grid.minor = theme_line(colour = NA), 
             panel.background = theme_rect(colour = 'white'))

I'd like to change the spacing (such as in Illustrator) between dashed and dotted grid lines.

Additionally, does anyone know if the x-axis and y-axis grid lines can be manipulated separately? For instance, I want to turn off the x-axis grid lines in this example.

I have seen manipulation using vline and hline (Add a dotted vertical line on certain x-axis values using ggplot), but don't want to have to hard code that each time if possible.

dashed line

Inch answered 18/9, 2011 at 23:48 Comment(2)
I removed my answer, since I believe I misunderstood you. Are you saying you want to have the dashed lines consist of dashes that are spaced further apart?Jew
@Jew Yes, precisely. In Illustrator for example, you can set the spacing of dashed and dotted lines. I want to know if this is also possible to do in a systemic way in ggplot. Additionally, I want to remove the x-axis major grid lines entirely--i.e. there should only be horizontal grid lines.Inch
I
6

You can use the power of lty. see Line Type Specification in ?par.

example:

qplot(1:5, 1:5) + opts(panel.grid.major = theme_line(linetype = "2925"))

As for the second question, at this time you cannot specify the v and h lines separately.
Here is a quick and dirty hack:

qplot(1:5, 1:5)
grid.edit("panel.grid.major.x.polyline", grep = TRUE, gp = gpar(lty = "5195"))
grid.edit("panel.grid.major.y.polyline", grep = TRUE, gp = gpar(lty = "33"))
Isologous answered 19/9, 2011 at 1:1 Comment(0)
J
3

Now that I understand what you're asking, I think maybe this demonstrates what you're looking for:

dat <- data.frame(x = 1:10, y = 1:10)

ggplot(dat,aes(x = x, y = y)) + 
    geom_point() + 
    scale_y_continuous(breaks = NA) +
    opts(panel.grid.major = theme_line(linetype = c("28")))

enter image description here

The linetype specification is a little complicated. See ?par, the section on specifying line types for an explanation.

Jew answered 19/9, 2011 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.