ggplot2:如何在同一个因子的不同地块使用相同的颜色 [英] ggplot2: How to use same colors in different plots for same factor
本文介绍了ggplot2:如何在同一个因子的不同地块使用相同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在不同的地块上将相同的颜色固定在一个值上?
假设我有两个data.frames df1和df2:
库(ggplot2)
库(gridExtra)
set.seed(1)
df1< - data.frame(c = c('a','b', 'c','d','e'),x = 1:5,y = runif(5))
df2 < - data.frame(c = c('a','c',' 'e','g','h'),x = 1:5,y = runif(5))
使用c作为颜色指示器绘制它们时,我会得到相同的五种颜色。
g1 < - ggplot(df1,aes(x = x,y = y,fill = c))+ geom_bar统计=身份)
g2 < - ggplot(df2,aes(x = x,y = y,fill = c))+ geom_bar(stat =identity)
grid.arrange g1,g2,ncol = 2)
但我希望c的相同值可以获得相同的颜色。
解决方案
我现在写了一个函数,它可以生成计算颜色的另一个函数。我不确定这是否是一种好方法。
库(ggplot2)
库(gridExtra)
库(RColorBrewer)
makeColors< - function(){
maxColors< - 10
usedColors< - c()
possibleColors< - colorRampPalette(brewer.pal(9, Set1))(maxColors)
函数(值){
newKeys< - setdiff(values,names(usedColors))
newColors< - possibleColors [1:length (newKeys)]
usedColors.new< - c(usedColors,newColors)
names(usedColors.new)< - c(names(usedColors),newKeys)
usedColors<<< ; - usedColors.new
possibleColors<< - possibleColors [length(newKeys)+1:maxColors]
usedColors
}
}
mkColor< - makeColors()
set.seed(1)
df1< - data.frame(c = c('a','b ','c','d','e'),x = 1:5,y = runif(5))
df2 < - data.frame(c = c('a','c ','e','g','h'),x = 1:5,y = runif(5))
g1 < - ggplot(df1,aes(x = x,y = y,fill = c))+ geom_bar(stat =identity)+ scale_fill_manual(values = mkColor(df1 $ c))
g2 < - ggplot(df2,aes(x = x,y = y,fill = c))+ geom_bar(stat =identity)+ scale_fill_manual(values = mkColor(df2 $ c))
grid.arrange (g1,g2,ncol = 2)
How can I pin the same color to a value in diffent plots?
Say I have two data.frames df1 and df2:
library(ggplot2)
library(gridExtra)
set.seed(1)
df1 <- data.frame(c=c('a', 'b', 'c', 'd', 'e'), x=1:5, y=runif(5))
df2 <- data.frame(c=c('a', 'c', 'e', 'g', 'h'), x=1:5, y=runif(5))
When plotting them using c as color-indicator I get the same five colors.
g1 <- ggplot(df1, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity")
g2 <- ggplot(df2, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity")
grid.arrange(g1, g2, ncol=2)
But I want that same values of c get the same color.
解决方案
I now wrote a function which generates another function which computes the colors. I'm not sure if it's a good way. Comments appreciated.
library(ggplot2)
library(gridExtra)
library(RColorBrewer)
makeColors <- function(){
maxColors <- 10
usedColors <- c()
possibleColors <- colorRampPalette( brewer.pal( 9 , "Set1" ) )(maxColors)
function(values){
newKeys <- setdiff(values, names(usedColors))
newColors <- possibleColors[1:length(newKeys)]
usedColors.new <- c(usedColors, newColors)
names(usedColors.new) <- c(names(usedColors), newKeys)
usedColors <<- usedColors.new
possibleColors <<- possibleColors[length(newKeys)+1:maxColors]
usedColors
}
}
mkColor <- makeColors()
set.seed(1)
df1 <- data.frame(c=c('a', 'b', 'c', 'd', 'e'), x=1:5, y=runif(5))
df2 <- data.frame(c=c('a', 'c', 'e', 'g', 'h'), x=1:5, y=runif(5))
g1 <- ggplot(df1, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity") + scale_fill_manual(values = mkColor(df1$c))
g2 <- ggplot(df2, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity") + scale_fill_manual(values = mkColor(df2$c))
grid.arrange(g1, g2, ncol=2)
这篇关于ggplot2:如何在同一个因子的不同地块使用相同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文