簡體   English   中英

R - GGplot2 Barplot - 將調色板的 colors 固定為多個繪圖上的相同值

[英]R - GGplot2 Barplot - Fixing colors of color palette to same values over multiple plots

我即將制作顯示值出現次數的條形圖。 我的問題是我有 1 個“概述”-Plot 包含所有值,后面是作為此概述 plot 的子集的詳細圖。這意味着這些子圖不包含與概述圖相同數量的值。 在此處輸入圖像描述

在比較這些圖時,這是一個問題,因為相同的值在每個 plot 中具有不同的 colors。我想要每個值的固定顏色,這在所有圖上始終相同。

我知道您可以手動為每個值分配一種顏色,但這需要大量工作。 有沒有自動的方法來做到這一點? 我有比這更多不同值的地塊。

這是我的代碼:

numb = length(unique(subplot1$Diff))

mypalette<-rainbow(numb, alpha = 1)

ggplot(subplot1, aes(x=as.factor(Diff), fill=as.factor(Diff) )) + 
geom_bar( ) +
scale_fill_manual(values = mypalette) +
geom_text(stat='count', aes(label=..count..), vjust=-1) +
labs(x ="Difference", y = "Count") + 
theme(legend.position="none")

子圖

structure(list(ICD = c("F10", "F10", "F10", "F10"), xxA = c(-0.63, 
0.61, 0.57, 0.57), xxU = c(-0.52, 0.54, 0.5, 0.5), Diff = c(0.1, 
0.1, 0.1, 0.1)), row.names = c(NA, 4L), class = "data.frame")

structure(list(ICD = c("F10-F19", "F10-F19", "F10-F19", "F10-F19", 
"F10-F19", "F10-F19", "F10-F19", "F10-F19", "F10-F19"), xxA = c(0.5, 
-0.64, -0.56, 0.51, -0.69, 0.59, 0.56, -0.67, 0.54), xxU = c(-0.5, 
-0.52, -0.65, -0.53, 0.82, 0.55, 0.56, -0.51, 0.51), Diff = c(1, 
0.1, 0.1, 1, 1.5, 0, 0, 0.2, 0)), row.names = c(NA, 9L), class = "data.frame")

Vinícius Félix 的評論是正確的,但您不需要寫下每種顏色和對應的值。 下面是一種創建命名調色板(基於 subplot2 數據集的值)的方法,該調色板在不同地塊之間保持一致:

# Load libraries
library(tidyverse)

# Example datasets
subplot1 <- structure(list(ICD = c("F10", "F10", "F10", "F10"), xxA = c(-0.63, 
                                                                        0.61, 0.57, 0.57), xxU = c(-0.52, 0.54, 0.5, 0.5), Diff = c(0.1, 
                                                                                                                                    0.1, 0.1, 0.1)), row.names = c(NA, 4L), class = "data.frame")

subplot2 <- structure(list(ICD = c("F10-F19", "F10-F19", "F10-F19", "F10-F19", 
                                   "F10-F19", "F10-F19", "F10-F19", "F10-F19", "F10-F19"), xxA = c(0.5, 
                                                                                                   -0.64, -0.56, 0.51, -0.69, 0.59, 0.56, -0.67, 0.54), xxU = c(-0.5, 
                                                                                                                                                                -0.52, -0.65, -0.53, 0.82, 0.55, 0.56, -0.51, 0.51), Diff = c(1, 
                                                                                                                                                                                                                              0.1, 0.1, 1, 1.5, 0, 0, 0.2, 0)), row.names = c(NA, 9L), class = "data.frame")

# Create a 'named' colour palette
mypalette <- rainbow(5)
names(mypalette) <- sort(unique(subplot2$Diff))
mypalette
#>         0       0.1       0.2         1       1.5 
#> "#FF0000" "#CCFF00" "#00FF66" "#0066FF" "#CC00FF"
# Plot the data
ggplot(subplot1, aes(x=as.factor(Diff), fill=as.factor(Diff) )) + 
  geom_bar( ) +
  scale_fill_manual(values = mypalette) +
  geom_text(stat='count', aes(label=..count..), vjust=-1) +
  labs(x ="Difference", y = "Count") + 
  theme(legend.position="none")

ggplot(subplot2, aes(x=as.factor(Diff), fill=as.factor(Diff) )) + 
  geom_bar( ) +
  scale_fill_manual(values = mypalette) +
  geom_text(stat='count', aes(label=..count..), vjust=-1) +
  labs(x ="Difference", y = "Count") + 
  theme(legend.position="none")

reprex package (v2.0.1) 創建於 2021-09-27

暫無
暫無

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

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