R adding legend and directlabels to ggplot2 contou

2020-02-28 04:10发布

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 enter image description here

Plot 2 enter image description here

Plot 3 enter image description here

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

1条回答
女痞
2楼-- · 2020-02-28 04:54

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

查看更多
登录 后发表回答