How to remove space between axis & area-plot in ggplot2?

前端 未结 3 1392
醉话见心
醉话见心 2020-11-22 06:49

I have the following dataframe:

uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L,         


        
3条回答
  •  伪装坚强ぢ
    2020-11-22 07:13

    Update: See @divibisan's answer for further possibilities in the latest versions of ggplot2.


    From ?scale_x_continuous about the expand-argument:

    Vector of range expansion constants used to add some padding around the data, to ensure that they are placed some distance away from the axes. The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables.

    The problem is thus solved by adding expand = c(0,0) to scale_x_continuous and scale_y_continuous. This also removes the need for adding the panel.margin parameter.

    The code:

    ggplot(data = uniq) + 
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
      scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
      scale_y_continuous(limits = c(0,101), expand = c(0, 0)) +
      theme_bw() +
      theme(panel.grid = element_blank(),
            panel.border = element_blank())
    

    The result: plot area chart with no gaps

提交回复
热议问题