modifying ggplot objects after creation

前端 未结 1 1510
攒了一身酷
攒了一身酷 2020-12-03 14:31

Is there a preferred way to modify ggplot objects after creation?

For example I recommend my students to save the r object together with the pdf file fo

相关标签:
1条回答
  • 2020-12-03 14:58

    You could use ggplot_build() to alter the plot without the code or data:

    Example plot:

    data("iris")
    
    p <- ggplot(iris) + 
      aes(x = Sepal.Length, y = Sepal.Width, colour = Species) + 
      geom_point()
    

    Colours are respective to Species.

    Disassemble the plot using ggplot_build():

    q <- ggplot_build(p)
    

    Take a look at the object q to see what is happening here. To change the colour of the point, you can alter the respective table in q:

    q$data[[1]]$colour <- "black"
    

    Reassemble the plot using ggplot_gtable():

    q <- ggplot_gtable(q)
    

    And plot it:

    plot(q)
    

    Now, the points are black.

    0 讨论(0)
提交回复
热议问题