0

To get a list which have 2 ggplot objects, my function gscatFn2 as below. Instead of using column name "Week" of the data frame, I want to use column index 7 for aesthetic mapping as below. But error message:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'Have you called 'sort' on a list?

There must be somthing wrong with my color aesthetic mapping which I couldn't figure out.

df1 <- data.frame(list(Avg=c(0.282,0.282,0.282,0.733),CV=c(0.7,1.06,0.7,0.9),Lot=c("a","b","c","d"),Model=c("m1","m2","m3","m4"),Size=c("01","02","03","04"),Month=c("11","11","22","22"),Week=c("23","23","24","24")))
df2 <- data.frame(list(Avg=c(0.010,0.02,0.029,0.083),CV=c(0.04,0.9,0.4,0.1),Lot=c("a","b","c","d"),Model=c("m1","m2","m3","m4"),Size=c("01","02","03","04"),Month=c("11","11","22","22"),Week=c("23","23","24","24")))

grp2 <- list(df1, df2) 

gscatFn2 <- function(grp, ccc, ccv,  cca, cccol) {
#grp: 2 data frame, ccc: Size, ccv : CV, cca : Avg, cccol : Week 
#ggpplot object for each data frame shall return
library(ggplot2)
p <- list()
for (i in ( 1:length(grp) ) )  {
#for each data frame repeat
    #x and y
    p1 <- ggplot(grp[[i]], aes_string( x=colnames(grp[[i]])[cca],y=colnames(grp[[i]])[ccv] )) 
    #color
    p2 <- p1 +  geom_point(aes_string( color= colnames(factor(grp[[i]])[cccol])  ) ) + scale_color_manual(values= c("deepskyblue3","firebrick"))
    #accumulate
    p[[i]] <- p2
}
return(p)
}

# for testing   
pp <- gscatFn2(grp2,5,2,1,7)  # <<<- I got error "Error in sort.list(y) : 'x' must be atomic..."
print(pp[[1]])
zx8754
  • 52,746
  • 12
  • 114
  • 209
Soon
  • 491
  • 3
  • 16

1 Answers1

0

Aside from the weird way to build the graph, about which I won't speak -you may have all the right reasons to do that-, the only problem I see is the use of

colnames(factor(grp[[i]])[cccol])))

Changing it to

 colnames(grp[[i]])[cccol]))

solves the issue:

enter image description here

GGamba
  • 13,140
  • 3
  • 38
  • 47
  • It works. Thank you very much. The mysterious thing is that without **factorizing** the colmun, the descrete color mapping can be possible? – Soon Jun 28 '17 at 08:44
  • 1
    well, yes, as demonstrated by the image. If you are really concerned that a continuous variable might slip in, you can use `color= paste0('factor(', colnames(grp[[i]])[cccol],')')` – GGamba Jun 28 '17 at 08:49