当因子是数字时,ggpairs颜色映射错误

时间:2014-04-24 18:55:16

标签: r ggplot2 correlation ggally

我遇到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)

plot1

请注意,相关图中颜色的顺序为:绿色,蓝色,红色,紫色。

但是,当用于设置颜色的变量是数字(转换为因子)时:

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)

plot2

我遇到了问题......尽管事实上我确保了关卡的顺序是正确的(1,3,10,13)。

由于某种原因,相关图中的颜色已经改变了顺序 - 它们现在是绿色,紫色,红色,蓝色。但是,请注意散点图看起来相同...这意味着信息不再对应于图表。

我将使用自定义颜色列表,每个颜色必须对应一个特定的数字组(以匹配我正在生成的其他图表)。有谁知道如何解决这个问题?

0 个答案:

没有答案