簡體   English   中英

在同一ggplot中為scale_fill_gradient設置不同的限制

[英]Set different limits for scale_fill_gradient in the same ggplot

我有要ggplot的代碼:

df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),roi=rep(1:10,10),area=runif(100, 5.0, 7.5))
df$time <- factor(df$time, levels=rev(levels(df$time)))
  p1 <- ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) +
    theme_minimal()  + coord_fixed(ratio=1) +
    geom_tile(colour = "white", width = 1.0, height = 1) + 
    scale_fill_gradient(low="blue",high="red")

在上面的代碼中,“藍色”和“紅色”分別表示“區域”列的最低和最高值。 但是我想從每個“時間”值中使用自己的梯度范圍繪制“面積”(例如,“時間”為0.5,梯度范圍由“面積”值的最小值和最大值設置為0.5)。 你是否認為這是可能做到這一點與ggplot。 先感謝您!

您可以按Time縮放area條目,因此最終得到標准化的area Z分數,該分數表征每個Time的標准差單位為零均值的差異。

set.seed(2017);
df <- data.frame(
    time = as.factor(rep(0.5:9.5, each = 10)),
    roi = rep(1:10, 10),
    area = runif(100, 5.0, 7.5))

 library(tidyverse);
 df %>%
     mutate(time = factor(time, levels = rev(levels(time)))) %>%
     group_by(time) %>%
     mutate(area = scale(area)) %>%
     ggplot(aes(x=factor(roi), y=time, fill = area)) +
         theme_minimal() +
         coord_fixed(ratio=1) +
         geom_tile(colour = "white", width = 1.0, height = 1) +
         scale_fill_gradient(low="blue",high="red")

在此處輸入圖片說明

請注意,該area現在是Z分數; 例如,值-2表示此特定條目比該特定Time的平均area值低2個標准差。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM