How to remove ggplot margin in R

Viewed 285

I want to remove margin from ggplot but again I am getting white border as in the image.

I have tried various options for plot margin but still margin is there.

    data <- structure(list(timestamp = structure(c(1598950800, 1598950800, 
                                               1598950800, 1598951100, 1598951100, 1598951100, 1598951400, 1598951400, 
                                               1598951400, 1598951700, 1598951700, 1598951700, 1598952000, 1598952000, 
                                               1598952000, 1598952300, 1598952300, 1598952300, 1598952600, 1598952600, 
                                               1598952600, 1598952900, 1598952900, 1598952900, 1598953200, 1598953200, 
                                               1598953200, 1598953500, 1598953500, 1598953500, 1598953800, 1598953800, 
                                               1598953800, 1598954100, 1598954100, 1598954100, 1598954400, 1598954400, 
                                               1598954400), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
                       Road_segment = c("L1", "L2", "L3", "L1", 
                                        "L2", "L3", "L1", "L3", "L2", 
                                        "L2", "L3", "L1", "L1", "L3", 
                                        "L2", "L2", "L3", "L1", "L1", 
                                        "L3", "L2", "L1", "L2", "L3", 
                                        "L3", "L2", "L1", "L2", "L3", 
                                        "L1", "L1", "L2", "L3", "L1", 
                                        "L2", "L3", "L3", "L2", "L1"
                       ), length_mi = c(2, 1.5, 1, 
                                        2, 1.5, 1, 
                                        2, 1, 1.5, 
                                        1.5, 1, 2, 
                                        2, 1, 1.5, 
                                        1.5, 1, 2, 
                                        2, 1, 1.5, 
                                        2, 1.5, 1, 
                                        1, 1.5, 2, 
                                        1.5, 1, 2, 
                                        2, 1.5, 1, 
                                        2, 1.5, 1, 
                                        1, 1.5, 2), 
                       avg_tdensity = c(59.9809611866698, 56.3123856677006, 57.6719458363159, 
                                        59.3508907977412, 56.4167760279965, 57.3786586335799, 56.8455221506403, 
                                        53.8045315358307, 54.8844746679392, 54.9068440308598, 52.5369343036666, 
                                        54.0965759961823, 53.7734629762189, 53.8517557464408, 53.941233198123, 
                                        57.5837111270182, 54.4867971049073, 59.4453392189613, 61.6561779209417, 
                                        55.8401435616003, 60.4730871709218, 63.5737294201861, 66.6594587608367, 
                                        56.9064165274795, 58.3753380259286, 65.83552055993, 63.5973415254911, 
                                        55.3480175773483, 56.538564781675, 54.6707229778096, 58.3641533444683, 
                                        52.1665970730931, 56.8069971367215, 60.7017517696652, 53.6516742225404, 
                                        56.3745227869244, 53.4913604549431, 52.4971665473634, 56.1781694901774
                       )), row.names = c(NA, -39L), class = c("tbl_df", "tbl", "data.frame"
                       ))

library(ggplot2)
ggplot(data, aes(x = timestamp, y = length_mi,
                    group = Road_segment, fill = avg_tdensity)) +
  geom_bar(stat = 'identity', position = position_stack(), width = 5*60) +
  coord_flip() +
  scale_fill_gradient(low = "yellow", high = "red", na.value = NA)+
  labs(x=NULL, y=NULL)+
  
  theme(axis.line=element_blank(),axis.text.x=element_blank(),
        axis.text.y=element_blank(),axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),legend.position="none",
        panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),plot.background=element_blank(), 
        panel.grid = element_blank(),
        plot.margin = unit(c(0, 0, 0, 0), "points")
        )

After plot I checked margin using theme_get() and found all margins to be 5.5 points.

enter image description here

1 Answers

The white border is mainly due to the default expansion of the x and y scale. To get rid of this use scale_x/y_continuous(expands = c(0, 0)). Additionally I added axis.ticks.length=unit(0, "pt") to the themeto remove the space where the ticks will be plotted. Otherwise you will still get a small white border.

library(ggplot2)
ggplot(data, aes(x = timestamp, y = length_mi,
                 group = Road_segment, fill = avg_tdensity)) +
  geom_bar(stat = 'identity', position = position_stack(), width = 5*60) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  coord_flip() +
  scale_fill_gradient(low = "yellow", high = "red")+
  labs(x=NULL, y=NULL) +
  theme(axis.line=element_blank(),axis.text.x=element_blank(),
        axis.text.y=element_blank(),axis.ticks=element_blank(),
        axis.ticks.length = unit(0, "pt"),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),legend.position="none",
        panel.background=element_blank(),panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),plot.background=element_blank(), 
        panel.grid = element_blank(),
        plot.margin = unit(c(0, 0, 0, 0), "points")
  )

Related