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

我对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)。

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

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

链接地址: http://www.djcxy.com/p/30859.html

上一篇: ggpairs wrong color mapping when factor is numeric

下一篇: GGally::ggpairs plot without gridlines when plotting correlation coefficient