本文介绍了R将图例和直接标签添加到ggplot2等高线图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一张栅格地图,我想用ggplot2使用连续比例绘制并在上面标注等值线。 为此,我使用了directlabels包,并且已经接近获取我想要的内容,但是我无法在同一个地图上同时获取图例和标注的isolines 以下代码重现了我的问题: install.packages(c ('ggplot2','directlabels')) library('ggplot2') library('directlabels') df< - expand.grid(x = 1:100,y = 1:100) df $ z #图1:这个图很好,但没有轮廓p geom_raster(data = df,aes(fill = z))+ scale_fill_gradient(limits = range(df $ z ),高='白色',低='红色')p #情节2:这个情节增加了等值线但没有标签,它还增加了第二个图例, 't p 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 p 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 mapThe 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 contoursp <- 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 wantp <- 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 showdirect.label(p, list("bottom.pieces", colour='black'))Plot 1Plot 2Plot 3I 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 advancePablo 解决方案 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$yp <- 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'))p1There 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"))p2To 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"))p3And 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等高线图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 23:41
查看更多