Marking changed values ​​for converted data in ggplot2

Problem

As I experiment with heatmaps, here's a circular question, with a likely disgustingly obvious answer ...

I answered the question about building a heat map with heterogeneous data using the fields and ggplot2 . It basically allows you to interpolate differently scaled x and y axes with the akima package, ready for construction.

Unfortunately, I cannot find a way to move the axes so that they return to their original values. I know that this will be due to the use of the breaks and labels parameters in ggplot2 , but I could not do anything but errors. Solutions for both build packages would be much appreciated ...

Updated with solution

For 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 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) p 

Result

Correctly labeled heat map

It remains to be decided

One problem remains: the first time you run the code, it generates labels in the reverse order; in the second and subsequent runs, the labels are correctly marked. Maybe another question? ...

+4
source share
1 answer

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 

Heat map with properly labeled axes

+5
source

Source: https://habr.com/ru/post/1415705/


All Articles