R语言 将geom_tile的图例标签更改为log10格式

m3eecexj  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(158)

如何将热图中的标签更改为log10格式?例如c(10^-4.5, 10^-4, 10^-3.5, 10^-3)
我已经尝试过,但没有工作,说NAs生产

myGGplotObject + scale_fill_gradient(name = "log10(meaurement of interest)",low="white", high="red",
breaks = scales::trans_format("log10", scales::math_format(expr = 10^.x)),
labels = scales::trans_format("log10", scales::math_format(expr = 10^.x))
)

下面是我代码的完整示例

# load Library
library(tidyverse)

# create example dataSet
x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
length_measurement <- 400
data$Z <- rnorm(400, 0.000025, 0.0005)

#add NA's 
addNA <- sample(seq_len(length_measurement), length_measurement/3)
data$Z[addNA] <- NA


#plot Heat map
ggplot(data, aes(Y,X, fill= log10(Z))) + 
  geom_tile() +   scale_fill_gradient(name = "log10(meaurement of interest)",low="white", high="red"#,
                                        #breaks = scales::trans_format("log10", scales::math_format(expr = 10^.x)),
                                        #labels = scales::trans_format("log10", scales::math_format(expr = 10^.x))
)
jdgnovmf

jdgnovmf1#

获取标签的一个选项是使用scales::label_math

library(ggplot2)

set.seed(123)

ggplot(data, aes(Y, X, fill = log10(Z))) +
  geom_tile() +
  scale_fill_gradient(
    name = "log10(meaurement of interest)", low = "white", high = "red",
    labels = scales::label_math()
  )

相关问题