本文介绍了标记在ggplot2中对转换后的数据重新标定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 问题 在我测试heatmaps时,这里是一个循环问题,可能有一个令人沮丧的明显答案... 我使用字段绘制具有不同数据的热图回答了一个问题和 ggplot2 包。它基本上允许使用准备绘图的 akima 程序包插入大小不同的缩放x和y轴。 不幸的是,我无法找到一种重新标记轴的方法,以便它们回溯到原始值。我知道它会涉及在 ggplot2 breaks 和标签参数$ c>但我一直无法产生任何错误。对于绘图软件包的解决方案将非常感激...... 更新了解决方案 为了方便起见,是我的代码使用 ggplot2 : library(akima) library(ggplot2) x.orig y.orig x y z t。 < - interp(x,y,z) t.df< - data.frame(t。) gt< - data.frame(expand.grid(x = t $ x $,y = t。$ y),z = c(t。$ z), value = cut(c(t。$ z), break = aq(min(z),max(z),0.25))) p geom_contour(aes(x = x,y = y,z = z),color =black) #----------------- --------------------------------------------- #以下解决方案由X提示。他的答案是: get.labels< - function(break.points,orig.data,scaled.data,digits){ labels< - as。字符(lapply(break.points,函数(i)round(i * min(orig.data) / min(scaled.data),数字)))标签} x.break.points< - seq(min(x),max(x),0.5) x.labels< - get.labels(x.break.points,x.orig,x,digits = 2)p $ b y.break.points< - seq(min(y),max(y),0.5) y.labels< - get .labels(y.break.points,y.orig,y,digits = 8)p labels = y.labels) 结果 还有待解决了 仍然存在一个问题:当代码第一次运行时,它会反向生成标签,在第二次和后续运行时,标签被正确标记。也许是另一个问题?... 解决方案我做了这样的事情: library(akima) library(ggplot2) x.orig y.orig x y z t。 < - interp(x,y,z) t.df< - data.frame(t。) gt< - data.frame(expand.grid(x = t $ x $,y = t。$ y),z = c(t。$ z), value = cut(c(t。$ z), break = aq(min(z),max(z),0.25))) p geom_contour(aes(x = x,y = y,z = z),color =black) get.labels< - function(break.points,orig.data,scaled。数据,数字){标签 function(i)round(i * min(orig.data) / min(scaled。数据),数字)))标签} x.break.points< - seq(min(x ),max(x),0.5) x.labels< - get.labels(x.break.points,x.orig,x,digits = 2)p labels = x.labels) y.break.points< --seq(min(y),max(y),0.5) y.labels< get.labels(y.break.points,y.orig,y,digits = 8)p< - p + scale_y_continuous(breaks = y.break.points, labels = y.labels) p The ProblemAs I experiment with heatmaps, here's a circular question, with a probably frustratingly obvious answer...I answered a question on plotting a heatmap with dissimilar data using the fields and ggplot2 packages. It basically allows very differently scaled x and y axes to be interpolated with the akima package ready for plotting.Unfortunately, I can't work out a way to relabel the axes so they refer back to the original values. I know it will involve the use of breaks and labels parameters in ggplot2 but I have been unable to produce anything but mistakes. Solutions for both the plotting packages would be much appreciated...Updated with solutionFor convenience, here is my code using ggplot2:library("akima")library("ggplot2")x.orig <- rnorm(20, 4, 3)y.orig <- rnorm(20, 5e-5, 1e-5)x <- scale(x.orig)y <- scale(y.orig)z <- rnorm(20)t. <- interp(x,y,z)t.df <- data.frame(t.)gt <- data.frame( expand.grid(x=t.$x, y=t.$y), z=c(t.$z), value=cut(c(t.$z), breaks=seq(min(z),max(z),0.25)))p <- ggplot(gt) + geom_tile(aes(x,y,fill=value)) + geom_contour(aes(x=x,y=y,z=z), colour="black")# --------------------------------------------------------------# Solution below prompted by X. He's answer:get.labels <- function(break.points, orig.data, scaled.data, digits) { labels <- as.character(lapply(break.points, function(i) round(i * min(orig.data) / min(scaled.data), digits) ) ) labels}x.break.points <- seq(min(x), max(x), 0.5)x.labels <- get.labels(x.break.points, x.orig, x, digits=2)p <- p + scale_x_continuous(breaks=x.break.points, labels=x.labels)y.break.points <- seq(min(y), max(y), 0.5)y.labels <- get.labels(y.break.points, y.orig, y, digits=8)p <- p + scale_y_continuous(breaks=y.break.points, labels=y.labels)pThe ResultStill to be solvedThere remains one issue: when the code is first run, it generates the labels in reverse, on the second and subsequent runs, the labels are correctly labeled. Maybe another question?... 解决方案 I did something like this:library("akima")library("ggplot2")x.orig <- rnorm(20, 4, 3)y.orig <- rnorm(20, 5e-5, 1e-5)x <- scale(x.orig)y <- scale(y.orig)z <- rnorm(20)t. <- interp(x,y,z)t.df <- data.frame(t.)gt <- data.frame( expand.grid(x=t.$x, y=t.$y), z=c(t.$z), value=cut(c(t.$z), breaks=seq(min(z),max(z),0.25)))p <- ggplot(gt) + geom_tile(aes(x,y,fill=value)) + geom_contour(aes(x=x,y=y,z=z), colour="black")get.labels <- function(break.points, orig.data, scaled.data, digits) { labels <- as.character(lapply(break.points, function(i) round(i * min(orig.data) / min(scaled.data), digits) ) ) labels}x.break.points <- seq(min(x), max(x), 0.5)x.labels <- get.labels(x.break.points, x.orig, x, digits=2)p <- p + scale_x_continuous(breaks=x.break.points, labels=x.labels)y.break.points <- seq(min(y), max(y), 0.5)y.labels <- get.labels(y.break.points, y.orig, y, digits=8)p <- p + scale_y_continuous(breaks=y.break.points, labels=y.labels)p 这篇关于标记在ggplot2中对转换后的数据重新标定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 05:04