简体   繁体   中英

R adding legend and directlabels to ggplot2 contour plot

I have a raster map that I want to plot using ggplot2 using a continuous scale and labeled isolines on top of that.

For that I'm using the directlabels package and am close to getting what I want but I can't get both the legend and the labeled isolines on the same map

The following code reproduces my problem:

install.packages(c('ggplot2', 'directlabels'))
library('ggplot2')
library('directlabels')
df <- expand.grid(x=1:100, y=1:100)
df$z <- df$x * df$y

# Plot 1: this plot is fine but without contours    
p <- ggplot(aes(x=x, y=y, z=z), data = df) + 
     geom_raster(data=df, aes(fill=z)) +
     scale_fill_gradient(limits=range(df$z), high = 'white', low = 'red')
p

# Plot 2: This plot adds the isolines but no labels and it also adds a second legend for level which I don't want
p <- p + geom_contour(aes(colour = ..level..), color='gray30', na.rm=T,     show.legend=T)
p

# Plot 3: This plot has the labeled isolines but it removes the z legend that I want to show
direct.label(p, list("bottom.pieces", colour='black'))

Plot 1 在此输入图像描述

Plot 2 在此输入图像描述

Plot 3 在此输入图像描述

I would like to have the coloured raster in the background, with it's color legend on the side and the labeled isolines on top. Is there a way to do this?

Also is there a way to get the labels placed in the middle of the isolines instead of the bottom or top?

Thanks in advance

Pablo

First, fixing the issue to do with the legends.

library(ggplot2)
library(directlabels)

df <- expand.grid(x=1:100, y=1:100)
df$z <- df$x * df$y

p <- ggplot(aes(x=x, y=y, z=z), data = df) + 
     geom_raster(data=df, aes(fill=z), show.legend = TRUE) +
     scale_fill_gradient(limits=range(df$z), high = 'white', low = 'red') + 
     geom_contour(aes(colour = ..level..)) +
     scale_colour_gradient(guide = 'none') 

p1 = direct.label(p, list("bottom.pieces", colour='black'))
p1

在此输入图像描述

There aren't too many options for positioning the labels. One possibility is angled.boxes , but the fill colour might not be too nice.

p2 = direct.label(p, list("angled.boxes"))
p2

在此输入图像描述

To change the fill colour to transparent (using code from here .

p3 = direct.label(p, list("far.from.others.borders", "calc.boxes", "enlarge.box", 
      box.color = NA, fill = "transparent", "draw.rects"))
p3

在此输入图像描述

And to move the labels off the contour lines:

p4 = direct.label(p, list("far.from.others.borders", "calc.boxes", "enlarge.box", 
      hjust = 1, vjust = 1, box.color = NA, fill = "transparent", "draw.rects"))
p4

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM