ggpairs wrong color mapping when factor is numeric
I am having problems with the ggpairs colour mapping. When the variable used to set the colour is a character (converted to a factor), things work as expected:
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)
Note that the order of the colours in the correlation plots is: green, blue, red, purple.
However, when the variable used to set the colour is numeric (converted to a factor):
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)
I get a problem... despite the fact that I made sure the order of the levels is correct (1, 3, 10, 13).
For some reason the colors in the correlation plots have changed order - they are now green, purple, red, blue. However, notice the scatter plots look the same... this means the info no longer corresponds across plots.
I will be using a custom list of colours, each of which must correspond to a specific numerical group (in order to match other plots I am generating). Does anyone know how to fix this?
链接地址: http://www.djcxy.com/p/30860.html上一篇: 从ggpairs图中打印单行