15.1 C
London
Friday, July 5, 2024
HomeTidyverse in Rggplot2 in RHow to Remove Gridlines in ggplot2 (With Examples)

How to Remove Gridlines in ggplot2 (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The easiest way to remove gridlines in ggplot2 is to use theme_classic():

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  theme_classic()

Alternatively, you can use the following syntax to remove specific gridlines:

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  theme_bw() +
  theme(axis.line = element_line(color='black'),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank())

The following examples show how to use both of these methods in practice.

Example 1: Remove Gridlines with theme_classic()

The following code shows how to remove gridlines from a ggplot2 plot using theme_classic():

library(ggplot2)

#define data
df frame(x=c(1, 2, 3, 4, 5, 6),
                 y=c(6, 8, 14, 19, 29, 31))

#create ggplot with no gridlines
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  theme_classic()

Remove gridlines in ggplot2

Example 2: Remove Specific Gridlines

The following code shows how to remove gridlines from a ggplot2 plot using a bit more customization:

library(ggplot2)

#define data
df frame(x=c(1, 2, 3, 4, 5, 6),
                 y=c(6, 8, 14, 19, 29, 31))

#create ggplot with no gridlines
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  theme_bw() +
  theme(axis.line = element_line(color='black'),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank())

Remove gridlines in ggplot2

This code allows you to remove specific gridlines. For example, we could use the following code to keep the major gridlines in the plot:

library(ggplot2)

#define data
df frame(x=c(1, 2, 3, 4, 5, 6),
                 y=c(6, 8, 14, 19, 29, 31))

#create ggplot with no gridlines
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  theme_bw() +
  theme(axis.line = element_line(color='black'),
    plot.background = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank())

Alternatively, we could use the following code to remove all gridlines but keep the panel border in the plot:

library(ggplot2)

#define data
df frame(x=c(1, 2, 3, 4, 5, 6),
                 y=c(6, 8, 14, 19, 29, 31))

#create ggplot with no gridlines
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  theme_bw() +
  theme(axis.line = element_line(color='black'),
    plot.background = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank())

Additional Resources

A Complete Guide to the Best ggplot2 Themes
How to Change the Legend Title in ggplot2
How to Set Axis Limits in ggplot2
How to Adjust Line Thickness in ggplot2

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories