0

Plot showing jumps between simulations

enter image description here

I have this issue with geom_path(). I want to plot a lot of simulations together, but for some reason when the plot is produced ggplot draws lines jumping between different simulations instead of following the correct simulation.

colnames(HIV.prev.HF.Rand.95) <- outMatRand[[1]]$time
HIV.prev.HF.Rand.95_m <- melt(HIV.prev.HF.Rand.95)

colnames(HIV.prev.HF.Rand.sat) <- outMatRand[[1]]$time
HIV.prev.HF.Rand.sat_m <- melt(HIV.prev.HF.Rand.sat)

And that melted data looks like this:

     Var1 Var2       value
        .    .           .
        .    .           .
197    97 1986 0.001215456
198    98 1986 0.001277602
199    99 1986 0.001200965
200   100 1986 0.001247436
201     1 1987 0.001173847
202     2 1987 0.001301196
203     3 1987 0.001312864
204     4 1987 0.001544837
205     5 1987 0.001467538
206     6 1987 0.001509051
207     7 1987 0.001297585
208     8 1987 0.001538991
        .    .           .
        .    .           .

I plotted using geom_path()

HF.95.sim.plt <- ggplot() + 
                 geom_point(data=as.data.frame(Fdata.prev1),aes(year,prev*100)) + 
                 geom_errorbar(data=as.data.frame(Fdata.prev1), aes(x=year,y=prev*100,ymin=lw*100,ymax=up*100)) + 
                 geom_path(data = HIV.prev.HF.Rand.95_m, aes(x = Var2, y = value*100, group = Var1),alpha = 0.2) + 
                 geom_path(data = HIV.prev.HF.Rand.sat_m, aes(x = Var2, y = value*100, group = Var1),color='red') + 
                 ylim(0,2)

HF.95.sim.plt

Is this just because I'm plotting a hundred simulations together?

Tung
  • 26,371
  • 7
  • 91
  • 115
  • 3
    Hi, and welcome to SO! Take a look at [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), because this makes it much easier to figure out what is going wrong and you are more likely to get a useful response. Figure out the smallest amount of code that starts to look wrong, and once you have that post just that. – Calum You Jul 18 '18 at 21:59
  • Looking at the melted data and plot (spike at ~1989) are two points going from `Var1=99` and `Var1=100`, there is about ~96% jump in `value`. This is assuming that in `ggplot()` code, the year correspond to two columns `Var1` and `Var2` where, x-axis goes from 1 to 100 for `Var1` for `year`. My Fuess would be the spikes (increase or decrease) say at `year=2003` could be due to corresponding high/low y-axis values in the dataframe. If it is possible to post data (all columns used in `ggplot()` code) for any one line(simulation) for all the years, it could be possible to look further into it. – Nilesh Ingle Jul 18 '18 at 22:21
  • Why `geom_path()` instead of `geom_line()`? If you use `geom_line` you could use the simulation number to group your data, which might help. – phalteman Jul 18 '18 at 22:44
  • Also, if you make a subset of your data and then use `dput()` it'll make it easier for others to recreate what you're trying to do. The data format you provided above makes it very difficult to use. – phalteman Jul 18 '18 at 22:47
  • Thank you for your responses, I will try to make a reproducible example now. – user10101980 Jul 18 '18 at 22:57
  • Thank you for your responses, I will try to make a reproducible example now. I tried using geom_line() but the same think happened, a friend of mine though geom_path() was more likely to work so I used it here. It might be a problem in my data, although I did consider it beforehand and there didn't seem to be any (obvious) problems with it. I will double check it now. Thank you all for your help, sorry I'm not responding individually to your messages, I haven't gotten the hang of this site yet. – user10101980 Jul 18 '18 at 23:04
  • I found the problem, It was a bug in my code producing the data. Basically I had a boundary to select the middle 95% of simulation values. But I had done it by timestep, so if one simulation left this boundary and was replaced by another the values assigned were shifted resulting in the cris-crossing seen in my plot. Thank you all for your help. I really appreciate it. – user10101980 Jul 19 '18 at 00:02

0 Answers0