AF7
AF7

Reputation: 3242

Plotting margin plots for geom_ raster() in ggplot2

I am trying to plot a raster map with some data, and margin plots of this data. Kind of like the levelplot() rasterVis function, or even better with colors (reflecting the colors inside the plot) instead than with a line. It will be clear below, looking at what I'm trying to do. rasterVis example (with density line):

https://i.sstatic.net/QiaUu.png

However, I want to use ggplot2. Download the data used in this example (8KB).

I tried to use grid.arrange():

library(ggplot2)
library(raster)
library(maps)
library(grid)
library(scales) #for muted()
library(gridExtra)

load("example.Rsave") #load data

#prepare data for the margin plot
var=tp_rlim; min=0; max=1; mid=0.5
varname <- colnames(var)[3]
zonal <- aggregate(var[,3], list(var$Lat), mean)
colnames(zonal) <- c("Lat", varname)

#plot the margin plot
ggzonal <- ggplot(zonal, aes(x="", y=Lat, fill=tp))+ 
             geom_raster() +
             theme(legend.position="none",
                   axis.title.x = element_blank(),
                   axis.title.y = element_blank(),
                   axis.ticks = element_blank(),
                   axis.text.x = element_blank(),
                   axis.text.y = element_blank()
                   ) +
             scale_fill_gradient2(limits=c(min, max), space="Lab", midpoint=mid)

#plot main plot
  gg <- ggplot(var, aes_string(x="Lon", y="Lat", fill=varname)) +
          geom_raster() +
          coord_quickmap(xlim = range(var$Lon), ylim = range(var$Lat)) + 
          scale_fill_gradient2(limits=c(min, max), space="Lab", midpoint=mid) +
            theme(legend.position="bottom",
                  legend.key.width = unit(1.5, "cm"),
                  legend.key.height = unit(0.3, "cm"),
                  legend.title=element_blank()
                  ) + 
            borders(region=unique(na.omit(map.where("world", var$Lon, var$Lat))), colour="black")

  grid.arrange(gg, ggzonal, ncol=2, widths=c(5, 1))

But the outcome is unsatisfactory: enter image description here

I have also tried to use ggExtra::ggMarginal, but:

ggExtra::ggMarginal(gg)

enter image description here

Or:

ggExtra::ggMarginal(gg, type="histogram")

enter image description here

How can I do this?

Upvotes: 2

Views: 1305

Answers (2)

baptiste
baptiste

Reputation: 77096

this might work,

# fix the ylim of annotation plot
gg2 = ggzonal + coord_cartesian(ylim = range(var$Lat)) 
# devtools::install_github("baptiste/egg")
library(egg)
ggarrange(gg, gg2, widths = c(10,1))

enter image description here

Upvotes: 3

Axeman
Axeman

Reputation: 35242

Using the wonderful cowplot package and the instruction found here, we can do the following:

library(cowplot)

grobs <- ggplotGrob(gg + theme(legend.position="bottom"))$grobs
legend_b <- grobs[[which(sapply(grobs, function(x) x$name) == "guide-box")]]

prow <- plot_grid(gg + theme(legend.position="none"), ggzonal, align = 'h', rel_widths = c(5, 1))

plot_grid(prow, legend_b, ncol = 1, rel_heights = c(1, .2))
ggsave('stack.png', w = 5, h = 5)

Result:

enter image description here

Upvotes: 2

Related Questions