0

I have using following function and code to plot a graph:

myfn2 = function(ddf, i, j, k){
    p = ggplot()+
        geom_point(aes(x=ddf[,i],y=ddf[,j], shape=ddf[,k], color=ddf[,k]))+
        labs(x=names(ddf)[i], y=names(ddf)[j])
    print(p)
}

myfn2(iris, 1,2,5)

However, the graph is not correct:

enter image description here

The correct graph should be:

enter image description here

How can this be corrected? Also how can title of legend be corrected? Thanks for your help.

rnso
  • 23,686
  • 25
  • 112
  • 234

1 Answers1

1

You could try

    # ...
    geom_point(data = ddf, aes_string(x = names(ddf)[i], 
                                      y=names(ddf)[j], 
                                      shape=names(ddf)[k], 
                                      color=names(ddf)[k])) +
    # ...
lukeA
  • 53,097
  • 5
  • 97
  • 100