R将图例和直接标签添加到ggplot2等高线图中 [英] R adding legend and directlabels to ggplot2 contour plot

查看:1156
本文介绍了R将图例和直接标签添加到ggplot2等高线图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张栅格地图,我想用ggplot2使用连续比例绘制并在上面标注等值线。

为此,我使用了directlabels包,并且已经接近获取我想要的内容,但是我无法在同一个地图上同时获取图例和标注的isolines



以下代码重现了我的问题:

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

#图1:这个图很好,但没有轮廓
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 ),高='白色',低='红色')
p

#情节2:这个情节增加了等值线但没有标签,它还增加了第二个图例, 't
p < - p + geom_contour(aes(color = ..level ..),color ='gray30',na.rm = T,show.legend = T)
p

#情节3:这个情节有标记的等值线但它删除了我想要显示
的z传奇direct.label(p,list(bottom.pieces,color ='black'))

地块1



剧情2

.imgur.com / 5sbFR.pngrel =nofollow noreferrer>



我想在背景中使用彩色光栅,边上的颜色图例和顶部标记的等值线。有没有办法做到这一点?



还有一种方法可以将标签放置在等值线中间而不是底部或顶部?



预先致谢

Pablo

解决方案首先,解决与传说有关的问题。

  library(ggplot2)
library(直接标签)

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 =真实)+
scale_fill_gradient(限制=范围(df $ z),高='白色',低='红色')+
geom_contour(aes(color = ..level ..))+
scale_colour_gradient(guide ='none')

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



定位标签的选项并不多。一种可能性是 angled.boxes ,但 fill 颜色可能不太好。

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



要更改 fill color to transparent(使用



并移动标签关闭等高线:

  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


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

这篇关于R将图例和直接标签添加到ggplot2等高线图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆