So, I'm trying to write a function that creates scatterplots.
Depending on the data, I may want the value of V1 or V2 to determine the shape / color of the point. The data in V1 takes only certain values, like V2, but both look continuous until R / ggplot, so I consider them as.factor
. I could also regress to buy V1 or V2.
This works great:
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, I first need to switch to aes_string
so that I can read the colum headers (also changing the x-axis value), but now I can not convince ggplot that V1 / V2 use only a finite number of values ββto assign different points. color works, but assigns a continuous hue, not different colors, so it also treats 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 am sure that there are many kludges, but it seems to me (perhaps in my naivety), as if this could be a real solution. Any help is appreciated.
Ps here is a sample sigma 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