When using ggplot in R, how do I remove margins surrounding the plot area?

后端 未结 4 1070
青春惊慌失措
青春惊慌失措 2020-12-10 12:23

I\'m trying to generate some fractals and have a question regarding the margins with ggplot in R. I\'m using the following code to generate the fractals.

lib         


        
4条回答
  •  生来不讨喜
    2020-12-10 13:08

    After using your code, I see more clearly what you're looking for. This:

    gg <- ggplot(data=df, aes(V1, V2, color=cl[V3]))
    gg + 
      geom_point() +
      labs(x = NULL, y = NULL, title = NULL) +
      scale_x_continuous(expand = c(0, 0), limits = range(df$V1)) +
      scale_y_continuous(expand = c(0, 0), limits = range(df$V2)) +
      scale_colour_manual(values = sort(c("#00000000", rainbow(35)), decreasing = FALSE)) +
      theme(
        panel.background = element_rect(fill = "transparent", colour = NA),
        plot.background = element_rect(fill = "transparent", colour = NA),
        panel.grid = element_blank(),
        panel.border = element_blank(),
        plot.margin = unit(c(0, 0, 0, 0), "null"),
        panel.margin = unit(c(0, 0, 0, 0), "null"),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.line = element_blank(),
        legend.position = "none",
        axis.ticks.length = unit(0, "null"),
        axis.ticks.margin = unit(0, "null"),
        legend.margin = unit(0, "null")
      )
    

    you have to remove the labels, not-expand the x & y axis and set hard limits. The nulls are also important.'

    This can also be done by doing gb <- ggplotGrob(gg) and manually editing the grobs & parameters, but I think this probably gets you what you need.

提交回复
热议问题