简体   繁体   English

R将图例和直接标记添加到ggplot2等高线图中

[英]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. 我有一个栅格地图,我想使用连续比例使用ggplot2绘制,并在其上标记为等值线。

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 情节1 在此输入图像描述

Plot 2 情节2 在此输入图像描述

Plot 3 情节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. 一种可能性是angled.boxes ,但fill颜色可能不太好。

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

在此输入图像描述

To change the fill colour to transparent (using code from here . 要将fill颜色更改为透明(使用此处的代码。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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