Search code examples
rggplot2tidyverse

Create new axis at the center of the graph: ggplot


I want to create a new axis for an existing plot. I can use the geom_line layer for this but how do I add labels for X and Y axis (similar to the default axis).

New Axis for a plot


Solution

  • One option would be ggh4x::coord_axes_inside which offers an out-of-box option to add inside axes:

    library(ggplot2)
    library(ggh4x)
    
    iris |>
      ggplot(aes(Sepal.Length, Sepal.Width, color = Species)) +
      geom_point() +
      theme(axis.line = element_line()) +
      coord_axes_inside(
        labels_inside = TRUE,
        xintercept = 6,
        yintercept = 3
      )