如何在ggplot中找到geom_line控制点

3

我能够通过这个问题的答案在ggplot中找到geom_curve控制点:

如何在ggplot中找到geom_curve控制点

现在我想知道如何将这个函数应用于geom_line,以获得我在ggplot上创建的所有沿geom_line的控制点。

b$data [[1]] 给出起点和终点,

p$layers [[1]]$geom_params 给出曲线信息(角度、曲率等)。

但是,我该如何找到沿着geom_line的所有坐标/点,以便我可以使用这些点重新生成该行?

  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)
1个回答

1

ggplot_build 可能会对您有所帮助。例如,数据 a 如下所示:

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

然后将b作为geom_line项目。
b <- a %>%
  ggplot(aes(xmax, ymax)) +
  geom_line() 

最终,
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

通过这个结果,你可能能够重现线条。这种方式对许多其他像geom_density这样的geom_...事物也可用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接