1

I am making a density plot with ggplot of a dataframe (datf) composed of several time series.the command I am using is:

ggplot (melt (datf), mapping = aes (fill = variable, x = value)) + geom_density (alpha = .5)+ xlim(0,45)

and I am getting this graph:

enter image description here There are 2 things wrong, 1: The lengend is obviously big and showing the info I don't want to show (I would like to use names stored in a vector "v") 2: The plot is actually wrong, a correct one should show a shape like that:

enter image description here I checked other similar questions but I got confused of how to give the right command. how the right command should look?

lmo
  • 37,904
  • 9
  • 56
  • 69
Davido
  • 121
  • 4
  • 12
  • 1
    please add your data so we can recreate your specific problem. See [this post regarding reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – emilliman5 Feb 22 '17 at 14:50
  • I could add the whole code but it wont help. I tested it, and everything is correct before the plotting command, the mistake is only there. my data frame (datf) is a data frame created binding elements (time series) of a list created in a loop. "h=do.call(cbind, lista)" "datf=as.data.frame(h)" – Davido Feb 22 '17 at 14:56
  • 3
    The call to `ggplot` _looks_ correct, but it is __very difficult__ to accurately understand where the problem is without having a snippet of your data. Consider `dput(head(melt(datf), 10))`. – bouncyball Feb 22 '17 at 15:11
  • In my loop 10 time series area created (ts1,ts2,ts3,ts4,ts5,ts6,ts7,ts8,ts9,ts10), if after the loop I bind them as k=cbind(ts1,ts2,ts3,ts4,ts5,ts6,ts7,ts8,ts9,ts10) ## datf=as.data.frame(k) and I give the command above I get the second graph (the correct one), but if I add these time series in every loop as elements of a list and after the loop a bind the elements of the list: h=do.call(cbind, lista) ## datf=as.data.frame(h) and give the ploting command I get the first graph (wrong one). – Davido Feb 22 '17 at 16:17
  • I cannot do it as the first option because in the real case I have much more than 10 time series so I need to work with a list. any idea? :) – Davido Feb 22 '17 at 16:18

1 Answers1

0

There is a critical difference in the two figures. It looks to me like you need to omit the NA values in your data frame. But, it is hard to discern the discrepancies without your actual data frame.

Try

na.omit(Data)

satan
  • 1
  • 2
  • In my loop 10 time series area created (ts1,ts2,ts3,ts4,ts5,ts6,ts7,ts8,ts9,ts10), if after the loop I bind them as k=cbind(ts1,ts2,ts3,ts4,ts5,ts6,ts7,ts8,ts9,ts10) ## datf=as.data.frame(k) and I give the command above I get the second graph (the correct one), but if I add these time series in every loop as elements of a list and after the loop a bind the elements of the list: h=do.call(cbind, lista) ## datf=as.data.frame(h) and give the ploting command I get the first graph (wrong one). – Davido Feb 22 '17 at 16:17
  • I cannot do it as the first option because in the real case I have much more than 10 time series so I need to work with a list. any idea? :) – Davido Feb 22 '17 at 16:17