Labeling rescaled values on transformed data in gg

2019-06-25 04:44发布

The Problem

As 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 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'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)

p

The Result

Correctly labeled heat map

Still to be solved

There 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?...

1条回答
放我归山
2楼-- · 2019-06-25 05:08

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

查看更多
登录 后发表回答