Remove geom(s) from an existing ggplot chart?

前端 未结 4 450
情深已故
情深已故 2020-12-15 21:28

I am trying to understand how I can make changes to the internals of a ggplot2 chart. I started reading the few ressources I could find about ggplot_built and <

4条回答
  •  没有蜡笔的小新
    2020-12-15 21:56

    You can try to include some alpha. Instead of removing the layer, it will not show up.

    count(mpg, class) %>%
      mutate(pct=n/sum(n)) %>%
      ggplot(aes(class, pct)) +
      geom_col(fill="blue", alpha=0) +
      geom_line(group=1) +
      geom_point(size=4)
    

    You can of course add the alpha afterwards like

    p <- ggplot_build(p)
    p$data[[1]]$alpha <- 0
    plot(ggplot_gtable(p))
    

提交回复
热议问题