我使用c(-3,3)
scale_fill_gradient(low= "black", high = "red")
绘制了ggplot2
范围内的一系列值,并且该图由另一个程序进行分析。其中一个测量值以像素为单位输出为强度,我想将其转换回以mm为单位的值。有可能吗?
答案 0 :(得分:1)
这是我的第一篇文章!无论如何这是我提出的hackey解决方案。我已经从黑色切换到红色再到黑色切换到黑色,因为我们只需要处理1个像素值。我所做的是创建一个绘图,将其转换为图像并查看绘图的1行的像素值。重新缩放范围后,像素值1到0(黑色到白色)映射到初始mm值-3到3。只是我的尝试,希望它有所帮助。
library(tidyverse)
library(imager)
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
dat <- data.frame(mm = seq(-3,3,.1))
####plot them out like you want
plot <- ggplot(data = dat, aes(y=1, x=mm, fill = mm))+
geom_tile()+
scale_fill_gradient(low= "black", high = "white")
#There's probably a better way to save the plot and retrieve it as an image
ggsave("plot.jpg", plot)
img <- imager::load.image("plot.jpg") %>%
grayscale() %>% #convert to grayscale
imsub(img, x>300, x <1900) #trim on the left and right
dims <- dim(img2) #get dimensions
values <- img2[,dims[2]/2,,] #Take a single pixel slice out of the image. 0
is black 1 is white
dat$scaled <- range01(dat$mm) # dat$scaled should correspond to values vector from the plot and you should be about to get mm by matching across.