user3022875 user3022875 - 1 month ago 12
R Question

ggpairs only plotting 1 of 5 plots then error

I am getting the below error when trying to plot the dat data frame

library(GGally)
library(ggplot2)
dat = data.frame(a=rnorm(5) , b= rnorm(5) ,c =rnorm(5) , d=rnorm(5) , e= c(1,2,3,4,5))
dat

a b c d e
1 0.21444531 1.9972134 2.1988103 -0.47624689 1
2 -0.32468591 0.6007088 1.3124130 -0.78860284 2
3 0.09458353 -1.2512714 -0.2651451 -0.59461727 3
4 -0.89536336 -0.6111659 0.5431941 1.65090747 4
5 -1.31080153 -1.1854801 -0.4143399 -0.05402813 5

ggpairs(dat ,mapping=aes(color =e),upper=list(continuous=wrap("cor",size=2)), columns = c("a","b","c","d"))


Error:


Error in $<-.data.frame(tmp, "label", value = ": ") :
replacement has 1 row, data has 0


I would like to color the data points using column "e"

Any ideas?

Answer

If you factorize e then it runs:

dat$e <- factor(dat$e)
ggpairs(dat,mapping=aes(color=e),upper=list(continuous=wrap("cor",size=2)), columns = c("a","b","c","d"))

But that is a pretty ugly figure not to mention a useless comparison.

If you eliminate the mapping then the code also runs fine:

ggpairs(dat,upper=list(continuous=wrap("cor",size=2)), columns = c("a","b","c","d"))