Ellen
Ellen

Reputation: 142

Reduce margins in GGPlot, having problems with plot.margin

I want my plot to fill more of the screen-much of it is dominated by white space and it is difficult to see the actual plot. I've tried plot.margin=margin and plot.margin=unit with no success. I must be doing something slightly wrong. I appreciate any advice on decreasing white space and increasing the plot itself.

Image I'm trying to alter

Here is a subset of my code

Date          Location       Temperature
1/1/2013      Effluent       47
1/2/2013      Effluent       47
1/3/2013      Effluent       47
1/4/2013      Effluent       47
1/1/2013      BDT            56
1/2/2013      BDT            57
1/3/2013      BDT            60
1/4/2013      BDT            56

temp<-read.csv("temperature.csv", header=TRUE)
library(lubridate)
library(ggplot2)
library(scales)
library(grid)
ggplot(temp, aes(x = mdy(Date), y = Temperature, color = Location)) +
  geom_point(size = .5) +
  guides(color = guide_legend(override.aes = list(size=5))) +
  coord_fixed(12) +
  ylab("Temperature (\u00B0F)") +
  scale_x_date(name = "", breaks = date_breaks("1 year"), labels = date_format("%b %Y")) + 
  scale_color_discrete(labels = c("Effluent", "RSW-002", "RSW-002A", "RRI", "SJG", "BDT"),
                       breaks = c("Effluent", "RSW-002", "RSW-002A", "RRI", "SJG", "BDT")) + scale_color_brewer(palette = "RdYlBu") +
  theme(legend.position="bottom", legend.text=element_text(size=16, face="bold"),
        axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0, size=16), axis.text.y = element_text(size=16), axis.title.y = element_text(vjust=3, size=16, face="bold"),
        legend.title=element_blank(),
        plot.title = element_text(hjust = 0.5), plot.margin=margin(c(0,0,0,0,"cm"))) 
ggsave("TEMP.png", width=15, height = 8, dpi = 300)

Upvotes: 1

Views: 2496

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

I think the main thing to point out is that this has nothing to do with whitespace as such. You're asking for coord_fixed, so ggplot will take up the maximum space it can on the x-axis, while obeying your command to keep the ratio of y axis:x axis constant. With the ratio you have specified, it simply has nothing to draw near the top or bottom of the plot, and will only do so if you specify a higher ratio.

One other thing I've changed: the margin() function takes its arguments directly without the c().

I've had to make up some data to get my plot into approximately the same shape as yours.

library(lubridate)
library(ggplot2)
library(scales)
library(grid)

temp <- data.frame(
          Date = as.Date(rep(as.POSIXct("2013-01-01") + days(1:2590), 6)),
          Temperature = (sin(2*pi* 1:15540/365.25) + 1) * 20 + 40 + rnorm(15540, 0, 3),
          Location = rep(c("Effluent","RSW-002","RSW-002A","RRI","SJG","BDT"), each = 2590))

ggplot(temp, aes(x = Date, y = Temperature, color = Location)) +
geom_point(size = .5) + 
  guides(color = guide_legend(override.aes = list(size = 5))) + 
  coord_fixed(20) +
  ylab("Temperature (\u00B0F)") +
  scale_x_date(name = "", 
               breaks = date_breaks("1 year"), 
               labels = date_format("%b %Y")) + 
  scale_color_discrete(labels = c("Effluent", "RSW-002", "RSW-002A", "RRI", "SJG", "BDT"),
                       breaks = c("Effluent", "RSW-002", "RSW-002A", "RRI", "SJG", "BDT")) + 
  scale_color_brewer(palette = "RdYlBu") +
  theme(legend.position = "bottom", 
        legend.text = element_text(size = 16, face = "bold"),
        axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0, size = 16), 
        axis.text.y = element_text(size=16), 
        axis.title.y = element_text(vjust=3, size=16, face="bold"),
        legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5),
        plot.margin = margin(1, 1, 1, 1, "cm"))

ggsave("TEMP.png", width = 15, height = 8, dpi = 300)

And TEMP.png looks like this:

enter image description here

Upvotes: 3

Related Questions