Isabelle
Isabelle

Reputation: 31

How to find the geom_line control points in ggplot

I was able to find the geom_curve control points in ggplot using the answer on this question:

How to find the geom_curve control points in ggplot

I am now wondering how to apply this same function to geom_line to obtain all the control points along the geom_line I created on my ggplot.

b$data[[1]] gives the starting and ending points and

p$layers[[1]]$geom_params gives the curve information (angle, curvature, ...).

But how do I find all the coordinates/points along the geom_line so I can reproduce the line with those points?

  C <- ggplot() +
    geom_sf(data = world, fill = "grey69", size = 0.1, colour = "transparent") +
    scale_x_continuous(limits = c(-180, 180), breaks = seq(-180, 180, by = 40)) +
    scale_y_continuous(limits = c(-90, 90), breaks = seq(-90, 90, by = 30)) +
    coord_sf(expand = FALSE) + 
    labs(x = "Longitude", y = "Latitude") +
    geom_sf(data = world, color = "grey87", fill = "grey87", size = 0.1) + 
    geom_line(aes(c(19, 6),c(-32,-40)), 
            lineend = "round", size = 0.4, col = "gray20", linetype = "solid", alpha = 0.6)

Upvotes: 3

Views: 114

Answers (1)

Kra.P
Kra.P

Reputation: 15123

ggplot_build may help you. For example, data a looks like

  weathersit     xmax         ymax
1        Bad 4715.689 0.0002230112
2   Very_Bad 4063.629 0.0002209857
3  God_Awful 2015.291 0.0002969503

Then let b the geom_line item as

b <- a %>%
  ggplot(aes(xmax, ymax)) +
  geom_line() 

Finally,

ggplot_build(b) %>% purrr::pluck("data",1)

         x            y PANEL group flipped_aes colour size linetype alpha
3 2015.291 0.0002969503     1    -1       FALSE  black  0.5        1    NA
2 4063.629 0.0002209857     1    -1       FALSE  black  0.5        1    NA
1 4715.689 0.0002230112     1    -1       FALSE  black  0.5        1    NA

with this result, you may be able to reproduce line. This way are available for many other geom_... things like geom_density.

Upvotes: 1

Related Questions