Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 当因子为数值时,GG对错误的颜色映射_R_Ggplot2_Correlation_Ggally - Fatal编程技术网

R 当因子为数值时,GG对错误的颜色映射

R 当因子为数值时,GG对错误的颜色映射,r,ggplot2,correlation,ggally,R,Ggplot2,Correlation,Ggally,我在颜色映射方面有问题。当用于设置颜色的变量为字符(转换为因子)时,一切正常: library(GGally) data(state) df <- data.frame(state.x77, State = state.name, Abbrev = state.abb, Region = state.region, Division = state.division ) col.i

我在颜色映射方面有问题。当用于设置颜色的变量为字符(转换为因子)时,一切正常:

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)
库(GGally)
数据(状态)

感谢您添加图片@hrbrmstr!这是我的第一个问题,所以我没有足够的分数来包含它们。这很奇怪!有这种定义颜色的技巧,但也不再有效了。将不得不查看ggpairs的代码并进行调查我认为这是ggally_cor函数的问题,因为它似乎不会影响散点图……如果有人能建议一种替代方法,我也将不胜感激。我有一些截止日期要赶。谢谢你需要它是基于ggplot的吗?你看过成对的例子了吗?它们有一个很好的例子,可以在上面的三角形上建立相关性,还有一个很好的ol'格
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)