我在使用 ggpairs 颜色映射时遇到问题。当用于设置颜色的变量是一个字符(转换为一个因子)时,事情会按预期工作:
library(GGally)
data(state)
df <- data.frame(state.x77,
State = state.name,
Abbrev = state.abb,
Region = state.region,
Division = state.division
)
col.index <- c(3,5,6,7)
p <- ggpairs(df,
# columns to include in the matrix
columns = col.index,
# what to include above the diagonal
upper = list(continuous = "cor"),
# what to include below the diagonal
lower = list(continuous = "points"),
# what to include in the diagonal
diag = "blank",
# how to label plots
axisLabels = "show",
# other aes() parameters
legends=F,
colour = "Region",
title = "Plot Title"
)
print(p)
请注意,相关图中的颜色顺序为:绿色、蓝色、红色、紫色。
但是,当用于设置颜色的变量是数字时(转换为因子):
df.numeric <- df
df.numeric$Region <- as.character(df.numeric$Region)
df.numeric$Region[which(df.numeric$Region == "Northeast")] <- 1
df.numeric$Region[which(df.numeric$Region == "South")] <- 3
df.numeric$Region[which(df.numeric$Region == "North Central")] <- 10
df.numeric$Region[which(df.numeric$Region == "West")] <- 13
df.numeric$Region <- factor(df.numeric$Region, levels = c(1,3,10,13))
p <- ggpairs(df.numeric,
# columns to include in the matrix
columns = col.index,
# what to include above the diagonal
upper = list(continuous = "cor"),
# what to include below the diagonal
lower = list(continuous = "points"),
# what to include in the diagonal
diag = "blank",
# how to label plots
axisLabels = "show",
# other aes() parameters
legends=F,
colour = "Region",
title = "Plot Title"
)
print(p)
我遇到了一个问题……尽管我确保级别的顺序是正确的(1、3、10、13)。
由于某种原因,相关图中的颜色顺序发生了变化——它们现在是绿色、紫色、红色、蓝色。但是,请注意散点图看起来相同......这意味着信息不再对应于各个图。
我将使用自定义颜色列表,每种颜色都必须对应一个特定的数字组(为了匹配我生成的其他图)。有谁知道如何解决这一问题?