Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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

like image 624
stackinator Avatar asked Apr 04 '18 15:04

stackinator


People also ask

How do you add a minor gridline in R?

You need to use scale_x_continuous() and scale_y_continuous() with breaks and minor_breaks parameters. Parameter breaks will add major gridlines whereas minor_breaks will add minor gridlines.

What is the difference between major and minor gridlines?

On value axes, major grid lines are drawn for every major axis division. Minor grid lines separate the units delineated by major grid lines. Minor grid lines, which can only appear on value axes, appear for every minor axis division. By default, major grid lines appear for value axes.

Which function can I use to modify the elements of a theme in Ggplot?

To modify individual elements, you need to use theme() to override the default setting for an element with an element function.

What is the function of minor gridlines?

Minor gridlines are horizontal and vertical lines that run through your chart layout to represent axis divisions. They are very useful for quickly calculating the height or breadth of visual components used in our chart.


1 Answers

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))
like image 63
Relasta Avatar answered Oct 21 '22 13:10

Relasta