0

I'm working with an R data-frame and attempting to plot each column independently. I want the "fixed" variable to form the x-axis (strings) and the other 2, 3, or 4 (the function will need to handle all of these) to be separate lines. The way I am currently handling this: I have a vector containing the names of the attributes of the data-frame. However, obviously simply using the vector will not index it properly. So here's what the data frame looks like:

> retset
  fixed       st1       st2       st3       st4
1  str1 0.9335938 0.9445313 0.9170455 0.9090909
2  str2 0.9670732 0.9768750 0.9637500 0.9532895
3  str3 0.9037500 0.9325758 0.8890625 0.8546875
4  str4 0.9540541 0.9717949 0.9397436 0.9354167
5  str5 0.9154412 0.9382812 0.9090909 0.9117188

and then a vector that would have:

uniq <- c("st1", "st2","st3","st4")

and I want to plot it something like:

z<-ggplot(data=retset)
for (x in length(uniq)) {
  z <- z + geom_line(data=retset, aes(x=fixed,y=uniq[x], color=uniq[x]))
}

Anybody have any suggestions?

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
Eric
  • 946
  • 2
  • 12
  • 25
  • will that not prevent me from using the dataframe itself? – Eric Oct 17 '15 at 00:07
  • 1
    See `aes_string`. Example [here](http://stackoverflow.com/questions/19826352/pass-character-strings-to-ggplot2-within-a-function), plus many others if search `aes_string` and *ggplot2 loops*. – aosmith Oct 17 '15 at 01:48
  • Or - as @BG1850 mentioned - melt your data first. – Heroka Oct 17 '15 at 11:21

1 Answers1

0

I am not sure whether I understand it correctly , but from my understanding melting the data based on the Fixed column and then making the plot would work

Bg1850
  • 3,032
  • 2
  • 16
  • 30
  • You are correct. It would work, and legends are a lot easier in long data. However, this is not really an answer, but more of a comment (as it doesn't provide any code to anser OP's problem. – Heroka Oct 17 '15 at 11:20
  • yes I agree I did not have enough reputation to comment – Bg1850 Oct 17 '15 at 21:02