0

I have two data sets, d1 and d2 in csv files. Each data has 6 columns. I managed to combine them by melt command and graph them together in ggplot. After I have added one extra column which is another variable the graph would depend on, I couldn't get the required graph. Provided sample of the dataset and my code.

The dataset after using melt and reshape package:

initi A   B       C     D    E   L1
0.005 1 23.7      1.0  1.0  24.7 d2
0.005 2 31.2      2.0  2.1  31.2 d2
0.005 3 35.8      3.1  3.2  35.6 d2
 1    1 6.2       1.0  1.0  6.2  d1
 1    2 10.1      2.0  2.1  7.0  d1
 1    3 11.2      3.0  3.5  7.0  d1
 2    1 14.2      8.0 14.3  5.2  d1
 2    2 15.9      7.0 13.0  5.5  d1
 2    3 16.0      6.2 12.4  5.8  d1

I need to graph A in the X-axis and B in the Y-axis. The initi value will represent each graph. In other words, it will be in the legend. For d1, I need to plot the results between A and B. For d2 I want to plot two graphs where the first graph when the initi = 1 and the second for initi = 2. All of the graphs for d1 and d2 are between A and B and combined in graph. The total in this case 3 lines combined in one graph. I managed to graph d1 and d2 before I have added the initi column. Now I struggling. Below is my code:

 dlist <- list(d1 =data1 ,d2 = data2) 
 reshaped_data <- melt(dlist, id.vars = c('initi','A','B','C','D','E')) 
 graph_AB<-ggplot(reshaped_data,aes(x = A, y = B, colour = initi)) +  
     geom_point(size = 5)+
     geom_line() +
     ggtitle("DATA1 vs DATA2") 

The above code is close to what I want ...Except that the lines are connected in strange way. I should get each pattern for "initi" as one line. Plus the legend is not showing each value of 'initi'

13
  • I'm not quite sure if this is what you want. So, I'll here write here in the comment to see if it is before I write as answer: Does this work? ggplot(data = df, aes(x = A, y = B, colour=factor(initi))) + geom_point() + geom_line() + facet_grid( . ~ L1)
    – Arun
    Feb 19, 2013 at 19:40
  • Yes it did sometheing close to what I want.It produced two graphs next each other. For d1 it produced one graph with d1 results only (A VS B). In graph 2, d2 results!!!?. Both graphs under one title and one legend ??? I want to combine them ?? Feb 19, 2013 at 19:50
  • Sorry I don't quite follow... Could you please explain again (by editing your post what you want to plot)?
    – Arun
    Feb 19, 2013 at 19:52
  • For d2 there is no initi=1 at all in the data you've shown.
    – Arun
    Feb 19, 2013 at 19:54
  • 1
    Do you want to facet based on L1 or initi? It is not quite clear.. And what do you mean initi is changing?
    – Arun
    Feb 19, 2013 at 20:04

2 Answers 2

0

You say you want two graphs, one for when initi is 1 and another for when it is 2, but it also takes the value 0.005. You need to subset your data first if you want to omit the 0.005 level.

In ggplot2, multiple graphs are called "facets", and since your facets depend on one variable, the command you want is facet_wrap(). (You'd use facet_grid if your facets depended on 2 variables.) Facets should be on factors, so we'll make sure initi is a factor

reshaped_data$initi <- factor(reshaped_data$initi)

then just add + facet_wrap(~ initi) to your ggplot.

EDIT: If you want just one graph, try

graph_AB<-ggplot(reshaped_data,aes(x = A, y = B, colour = initi, group = L1)) +  
    geom_point(size = 5)+
    geom_line() +
    ggtitle("DATA1 vs DATA2")

graph_AB

In the future, post data using dput. For example, if you do dput(reshaped_data) and paste it into your question, the output will be this:

    structure(list(initi = c(0.005, 0.005, 0.005, 1, 1, 1, 2, 2, 
2), A = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), B = c(23.7, 31.2, 
35.8, 6.2, 10.1, 11.2, 14.2, 15.9, 16), C = c(1, 2, 3.1, 1, 2, 
3, 8, 7, 6.2), D = c(1, 2.1, 3.2, 1, 2.1, 3.5, 14.3, 13, 12.4
), E = c(24.7, 31.2, 35.6, 6.2, 7, 7, 5.2, 5.5, 5.8), L1 = structure(c(2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("d1", "d2"), class = "factor")), .Names = c("initi", 
"A", "B", "C", "D", "E", "L1"), class = "data.frame", row.names = c(NA, 
-9L))

which anyone can paste into R and use easily.

6
  • I thought initi, since it has to be in legend, should be colour and the facetting should be on L1...
    – Arun
    Feb 19, 2013 at 19:44
  • @Arun Hmm, maybe. Based on "The initi value will represent each graph" I assumed the goal was to facet on that, but it's pretty confusing. Maybe "will represent" should be "will be present in", in which case I'm all wrong. Feb 19, 2013 at 20:00
  • What I want exactly to plot is one graph to compare d1 and d2 results (A and B) with respect to the change of 'initi'. I tried something like :'ggplot(df,aes(x = A, y = B, colour = initi,shape=L1))' it worked, but it doesn't show me the legend correctly. Feb 19, 2013 at 20:15
  • @shujaa I think I need facet_grid (initi and L1 both changing) Feb 19, 2013 at 20:24
  • Could you provide the full ggplot command for your suggestion. Feb 19, 2013 at 20:32
0
dlist <- list(d1 =data1 ,d2 = data2) 
reshaped_data <- melt(dlist, id.vars = c('initi','A','B','C','D','E')) 
graph_AB<-ggplot(reshaped_data,aes(x = A, y = B, colour = initi)) +  
geom_point(size = 5)+
ggtitle("DATA1 vs DATA2") 
print(graph_AB)

I just removed geom_line from original question !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.