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.
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.
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.
To modify individual elements, you need to use theme() to override the default setting for an element with an element function.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With