简体   繁体   中英

ggplot2: passing a continuous variable to “shape” within a wrapper

So I'm trying to write a function which creates scatter plots.

Depending on the data, I may want the value of V1 or V2 to determine the shape/colour of the point. The data in V1 only takes particular values, as does V2, but both look continuous to R/ggplot so I treat them as.factor . I might also regress buy V1 or V2.

This works fine:

p1<-ggplot(sigmas.table,
           aes(x=V4,y=V5, color=as.factor(V2),shape=as.factor(V1)))+  
    geom_point(size=4)+
    stat_smooth(aes(group=as.factor(V2)),method=lm,formula=y~poly(x,1),size=1)

But to wrap this in a function, first I have to switch to aes_string so that I can read in the colum titles (I also vary the x-axis value), but now I cant convince ggplot that V1/V2 only take a finite number of values to assign different points to. colour sort of works, but assigns a continuous shade rather than different colours, so it too is treating the data as continuous:

scatter.plot<- function(s.table,x.var, col.var, shape.var, line.var){
              ploti <- ggplot(s.table,
                              aes_string(x=x.var,y="V5", color=as.factor(col.var),shape=as.factor(shape.var)))+
                       geom_point(size=4)+
                       stat_smooth(aes_string(group=as.factor(line.var)),method=lm,formula=y~poly(x,1),size=1)+
               return(ploti)}

p1 <- scatter.plot(sigmas.table,"V4","V2","V1","V2")

returns

Error: A continuous variable can not be mapped to shape

I'm sure there are plenty of kludges available, but it seems to me (perhaps in my naivete) like there could be a real solution. Any and all help appreciated.

Ps here's a sample of the sigmas table:

     V1  V2         V3        V4         V5         V6
 1: 0.3  16 0.12791584 0.3454941 0.19463432 0.04231422
 2: 0.5  16 0.09908318 0.4460310 0.06286376 0.05462742
 3: 0.7  16 0.08374057 0.5277510 0.03820782 0.06463604
 4: 0.9  16 0.07385224 0.5984134 0.03026121 0.07329038
 5: 0.3  32 0.09045016 0.2443013 0.17003319 0.02992067
 6: 0.5  32 0.07006239 0.3153916 0.04250670 0.03862742
 7: 0.7  32 0.05921353 0.3731763 0.02563209 0.04570458
 8: 0.9  32 0.05222142 0.4231422 0.02037799 0.05182412
 9: 0.3  64 0.06395792 0.1727471 0.14072683 0.02115711
10: 0.5  64 0.04954159 0.2230155 0.03286223 0.02731371
11: 0.7  64 0.04187029 0.2638755 0.01946985 0.03231802
12: 0.9  64 0.03692612 0.2992067 0.01475039 0.03664519
13: 0.3 128 0.04522508 0.1221506 0.11012266 0.01496034
14: 0.5 128 0.03503120 0.1576958 0.02260296 0.01931371
15: 0.7 128 0.02960676 0.1865882 0.01317059 0.02285229
16: 0.9 128 0.02611071 0.2115711 0.01036970 0.02591206

As stated in my comment, you should transform your original data, if it actually contains factors. However, you can also do it in your function:

scatter.plot<- function(s.table,x.var, col.var, shape.var, line.var){
  s.table[,col.var] <- as.factor(s.table[,col.var])
  s.table[,shape.var] <- as.factor(s.table[,shape.var])
  s.table[,line.var] <- as.factor(s.table[,line.var])

  ggplot(s.table, aes_string(x=x.var, y="V5", color=col.var, shape=shape.var)) +
    geom_point(size=4) +
    stat_smooth(aes_string(group=line.var), 
                method=lm, formula=y~poly(x,1), size=1)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM