Matias Andina
Matias Andina

Reputation: 4230

Plot linked row data from data frame ggplot

I am having trouble with ggplot.

I pretend to plot this data frame using the column names as the x variable and the rows have the values that must be plotted for each replicate. Notice that there are two big groups identified as TR and UT that I want to plot colored in red and blue (or default colors). I know that ggplot asks for melting the data, so I did. But when I use melted data I lost the individual values that I want to connect over the x variable for each replicate.

> dput(pend2x45)
structure(list(grupo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("TR", 
"UT"), class = "factor"), tra1 = c(0.753387533875337, 1, 0.983904465213214, 
1, 0.2701754385965, 0.49181478016852, 1, 0.313598519888998, 1, 
NA, 0.397802197802199, 0, 0.0242857142857152, 0, 0, 0, 0.420454545454549, 
0, 0, 0.251256281407036), tra2 = c(1, 0.931318681318686, 0.882658359294182, 
0.723856209150329, 0.589473684210547, 0.95112254443432, 0.637602179836513, 
0.985568917668843, 0.502304147465442, NA, 0.112087912087914, 
0, 0, 0, 1, 0, 0.520454545454553, 0, 0, 1), tra3 = c(0.1318879855465, 
0.865384615384395, 1, 0.308823529411692, 0, 0.43872778297471, 
0.193460490463153, 0.0599444958371586, 0.0230414746543652, NA, 
0, 0, 0, 0, 0, 0, 0, 0.111553784860556, 0, 0), tra4 = c(0.079494128274612, 
0.909340659340445, 0.786085150571166, 0.666666666666513, 0, 1, 
0.114441416893713, 0.696392229417014, 0.0645161290322338, NA, 
0, 0.334196891191631, 0, 0, 0, 0, 0, 0.231075697211109, 0, 0), 
    test1 = c(0.304426377597101, 0.421828171828164, 0.48078920041552, 
    0.52450980392156, 1, 0.228484565014087, 0.53950953678472, 
    0.455134135060122, 0.0552995391705048, NA, 1, 0.81088082901553, 
    1, 0.610756123535665, 0.666666666666647, 0.687817258883237, 
    0.102272727272727, 1, 0.992609016999254, 0.130653266331654
    ), test2 = c(0.499548328816612, 0.159840159840158, 0.923156801661775, 
    0.0604575163398688, 0.961403508771952, 0.32787652011234, 
    0.610354223433229, 1, 0, NA, 0.47912087912088, 1, 0.395714285714293, 
    1, 0.190476190476187, 1, 1, 0.274900398406379, 1, 0.135678391959796
    )), .Names = c("grupo", "tra1", "tra2", "tra3", "tra4", "test1", 
"test2"), row.names = c("TR2x45", "TR2x45.1", "TR2x45.1.1", "TR2x45.1.2", 
"TR2x45.1.3", "TR2x45.2", "TR2x45.3", "TR2x45.4", "TR2x45.5", 
"TR2x45.6", "UT2x45", "UT2x45.1", "UT2x45.1.1", "UT2x45.1.2", 
"UT2x45.1.3", "UT2x45.2", "UT2x45.3", "UT2x45.4", "UT2x45.5", 
"UT2x45.6"), class = "data.frame")

The closest I got to the goal was using

mlt<-melt(pend2x45)
qplot(mlt,x=mlt$variable,y=mlt$value,col=mlt$grupo)

enter image description here

I think that the data is correctly plotted but I want to connect the individual dots to see where are they going as x increases. Later I would like to add some mean values for each group.

Upvotes: 0

Views: 116

Answers (1)

MrFlick
MrFlick

Reputation: 206566

If you want points and lines, then consider using ggplot() directly. Also, you have important grouping information in the rownames() which makes things harder to group. Here i move the rownames into the data itself, then melt; this allows for identifying which points should fall in the same line

ggplot(melt(cbind(pend2x45, id=rownames(pend2x45))), 
    aes(x=variable, y=value, color= grupo, group=id)) + 
    geom_point() + geom_line()

which produces

enter image description here

Upvotes: 0

Related Questions