• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / 15 Tips to Customize lines in ggplot2 with element_line()

15 Tips to Customize lines in ggplot2 with element_line()

May 28, 2021 by cmdlinetips

As part of understanding and fine tuning ggplot2’s theme elements, we will focus on the anatomy of line elements and see 15 tips to control them with ggplot2’s theme system. With ggplot2’s theme system we can control the “non-data” elements of a plot made with ggplot2. ggplot2’s theme system comes with multiple element_ functions,
  1. element_text()
  2. element_line()
  3. element_rect()
  4. element_blank()

And we learned about element_text() in the previous post. In this tutorial we will focus on the theme element element_line(). We will learn 15 tips to have finer control over the “colors, line type” for many part of line elements in a plot.

Anatomy of ggplot2’s element_line()

With element_line(), we can customize all the lines that are not part of data. For example, we can customize the color of x and y axis lines, we can make the axis lines as arrows, and we can add second x-axis on top and so on.

To give a more specific example, to control the color of x axis line, we will use “axis.line.x” element as argument to theme() function and use element_line() to specify the color of the x-axis line.

Here is element_line() function with the available arguments and their default choices. In this tutorial, we will mainly focus on changing color, size, line type and arrow using theme().

element_line(
  colour = NULL,
  size = NULL,
  linetype = NULL,
  lineend = NULL,
  color = NULL,
  arrow = NULL,
  inherit.blank = FALSE
)

Broadly, with element_line() we can customize three groups of lines in a plot. First, and X and Y axis lines. Second is the lines associated with tick on X and Y axis. And the third is the major and minor grid lines along both X and Y axis. The figure below shows the anatomy of line elements and the key word in ggplot2 describing the element.

Tips to Customize line elements in ggplot2
Tips to Customize line elements in ggplot2

In total there are 15 ways (at least?) we can control the three broad aspects/elements of lines in a plot made with ggplot2. Here is the list of the elements that we can control using using element_lines().

Theme elements to control the X and Y axis lines.

  1. axis.line.x: to customize x-axis label/title
  2. axis.line.x.top: to customize x-axis label/title
  3. axis.line.x.bottom: to customize x-axis label/title
  4. axis.line.y: to customize y-axis line
  5. axis.line.y.left: to customize y-axis line on the left
  6. axis.line.y.right: to customize secondary y-axis line on the right

Theme elements to control the ticks on X and Y axis

  1. axis.ticks: to customize both X and Y Axis Ticks
  2. axis.ticks.x: to customize X Axis Ticks
  3. axis.ticks.y: to customize Y-Axis Ticks

Theme elements to control major and minor grid lines on X and Y axis

  1. panel.grid.major : to customize major grid lines on x and y axis
  2. panel.grid.minor : to customize minor grid lines on x and y axis
  3. panel.grid.major.x: to customize major grid lines on x-axis
  4. panel.grid.major.y: to customize major grid lines on y-axis
  5. panel.grid.minor.x: to customize minor grid lines on x-axis
  6. panel.grid.minor.y: to customize minor grid lines on y-axis

Let us get started by making a scatter plot using ggplot2 with common text annotations like title, subtitle, legend and tags.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

We use Palmer penguins data make the scatterplot. Check out the labs() function for variation addition to the plot. We use theme_set() option to make the plot in black and white theme with pre-specified font sizes.

p <- penguins %>%
  drop_na() %>%
  ggplot(aes(x = flipper_length_mm,
             y = bill_length_mm, 
             color = species,
             shape = sex))+
  geom_point()+
  theme(legend.position = "none")

p

And this is how the scatter plot looks like this. We have saved the code to make the plot as a variable and use it use it to customize 10 different text elements of ggplot2’s theme using element_text() function.

Simple Scatter Plot with ggplot2

Simple Scatter Plot with ggplot2

1. Customizing ggplot2 x-axis line with axis.line.x

p + theme(axis.line.x = element_line(linetype = "dashed", 
                                  color = "red",
                                  size = 2,
                                  arrow = grid::arrow()))
ggsave("customize_axis_line_x_element_line_ggplot2.png")

Customize X-axis Line with element_line()
Customize X-axis Line with element_line()

2. Customizing ggplot2 x-axis line with axis.line.x.bottom

p + theme(axis.line.x.bottom = element_line(linetype = "dashed", 
                                  color = "blue",
                                  size = 2,
                                  arrow = grid::arrow()))
ggsave("customize_axis_line_x_bottom_element_line_ggplot2.png")
Customize X-axis Line with axis.line.x.bottom
Customize X-axis Line with axis.line.x.bottom

3. Customizing ggplot2 top of x-axis line with axis.line.x.top

p + theme(axis.line.x.top = element_line(linetype = "dashed", 
                                  color = "blue",
                                  size = 2,
                                  arrow = grid::arrow()))+
  guides(x.sec="axis")
ggsave("customize_axis_line_x_top_element_line_ggplot2.png")
Customizing ggplot2 top of x-axis line with axis.line.x.top
Customizing ggplot2 top of x-axis line with axis.line.x.top

4. Customizing Y-axis Line with axis.line.y

theme: axis.line.y
p + theme(axis.line.y = element_line(color = "red",
                                  size = 0.5,
                                  arrow = grid::arrow()))
Customize y-axis line with axis.line.y
Customize y-axis line with axis.line.y

5. Customizing Y-axis Line on the Left with axis.line.y.left

# theme: axis.line.y.left

p + theme(axis.line.y.left = element_line(linetype = "dashed", 
                                  color = "blue",
                                  size = 2,
                                  arrow = grid::arrow()))
ggsave("customize_axis_line_y_left_element_line_ggplot2.png")
Customizng Y-axis Line on Left with element_line()
Customizng Y-axis Line on Left with element_line()

6. Customizing Secondary Y-axis Line on the Right with axis.line.y.right

p + theme(axis.line.y.right = element_line( color = "blue"))+
  guides(y.sec = "axis") 
ggsave("customize_axis_line_y_right_element_line_ggplot2.png")
Customizing Secondary Y-axis on Right
Customizing Secondary Y-axis on Right

7. Customizing Both X and Y-axis Ticks with axis.ticks

p + theme(axis.ticks = element_line(color = "red", size = 2))
ggsave("customize_axis_ticks_element_line_ggplot2.png")
Customizing Axis Ticks ggplot2
Customizing Axis Ticks ggplot2

8. Customizing X-axis Ticks with axis.ticks.x

p+ theme(axis.ticks.x = element_line(color = "red", size = 4))
ggsave("customize_axis_ticks_x_element_line_ggplot2.png")
Customizing X axis Ticks with axis.ticks.x
Customizing X axis Ticks with axis.ticks.x

9. Customizing Y-axis Ticks with axis.ticks.y

p+ theme(axis.ticks.y = element_line(color = "red", size = 4))
ggsave("customize_axis_ticks_y_element_line_ggplot2.png")
Customizing Y axis Ticks with axis.ticks.y
Customizing Y axis Ticks with axis.ticks.y

10. Customizing grid major with panel.grid.major

p + theme(panel.grid.major = element_line(color = "blue", 
                                       size = 1))
ggsave("customize_panel_grid_major_element_line_ggplot2.png")
Customize Major Grid Lines with panel.grid.major
Customize Major Grid Lines with panel.grid.major

11. Customizing grid minor lines with panel.grid.major

## panel.grid.minor
p+ theme(panel.grid.minor = element_line(color = "steelblue4",
                                       size = 1,
                                       linetype = "dotted"))
ggsave("customize_panel_grid_minor_element_line_ggplot2.png")

Customizing Minor Grid Lines
Customizing Minor Grid Lines

12. Customizing grid major on y-axis with panel.grid.major.y

p+ theme(panel.grid.major.y = element_line(color = "purple",
                                       size = 1,
                                       linetype = "dotted"))
ggsave("customize_panel_grid_major_y_element_line_ggplot2.png")
Customize Major Grid Lines on Y-axis
Customize Major Grid Lines on Y-axis

13. Customizing grid minor on y-axis with panel.grid.minor.y

p+ theme(panel.grid.minor.y = element_line(color = "purple",
                                       size = 1,
                                       linetype = "dotted"))
ggsave("customize_panel_grid_minor_y_element_line_ggplot2.png")
Customize Grid Minor Lines on Y-axis
Customize Grid Minor Lines on Y-axis

14. Customizing major grid lines on x-axis with panel.grid.major.x

p+ theme(panel.grid.major.x = element_line(color = "purple",
                                       size = 1,
                                       linetype = "dotted"))
ggsave("customize_panel_grid_major_x_element_line_ggplot2.png")
Customizing Major Line on X-axis
Customizing Major Line on X-axis

15. Customizing minor grid lines on x-axis with panel.grid.minor.x

#theme panel.grid.minor.x
p+ theme(panel.grid.minor.x = element_line(color = "purple",
                                       size = 1,
                                       linetype = "dotted"))
ggsave("customize_panel_grid_minor_x_element_line_ggplot2.png")
Customize panel grid minor x element_line()
Customize panel grid minor x element_line()


Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

remove ggplot2 elements with element_blank8 tips to use element_blank() in ggplot2 theme Anatomy of Rectangle Elements in ggplot27 Tips to customize rectangle elements in ggplot2 element_rect() Tips to Customize Text in ggplot2 plot10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text() Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R

Filed Under: ggplot2 element_line, R, R Tips, tidyverse 101 Tagged With: ggplot2, R

Reader Interactions

Trackbacks

  1. 10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text() - Python and R Tips says:
    May 28, 2021 at 2:10 pm

    […] element_line() […]

  2. 7 Tips to customize rectangle elements in ggplot2 element_rect() - Python and R Tips says:
    June 6, 2021 at 9:51 am

    […] the blog post series on how to control theme elements in ggplot2, in this post we will look at how to customize […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version