stackinator
stackinator

Reputation: 5819

I want ggplot gridline thickness to be different for major/minor gridlines

My example is taken from here (link). R version is 3.4.3, ggplot2 version is 2.2.1

library(tidyverse)
df <- data.frame(dose=c("D0.5", "D1", "D2"),
                    len=c(4.2, 10, 29.5))

    ggplot(data=df, aes(x=dose, y=len)) +
      geom_bar(stat="identity")

Almost every ggplot2 tutorial shows some simple graph. I always notice the major gridlines are about twice the thickness of the minor gridlines, which is what I'd expect. This is shown in the first image below. It looks great.

Whenever I plot something on ggplot the minor and major gridlines share the same weight, the same thickness, by default. Why? I didn't change any gridline settings? Is there some R Studio global setting I accidentaly enabled/disabled? I want the minor gridlines to be a lighter weight than the major gridlines, like the first image shown. The second image is what I get when I ggplot the graph.

correct_grid.png

incorrect_grid.png

Upvotes: 8

Views: 9318

Answers (1)

Relasta
Relasta

Reputation: 1106

You can change the weight of the grid lines by using the theme function

df <- data.frame(dose=c("D0.5", "D1", "D2"),
            len=c(4.2, 10, 29.5))

ggplot(data=df, aes(x=dose, y=len)) +
       geom_bar(stat="identity") + 
       theme(panel.grid.minor = element_line(size = 0.5), panel.grid.major = element_line(size = 1))

Upvotes: 15

Related Questions