4
votes

I looked at these questions(1) and (2), but, they don't seem to be relevant to my question here.

I am trying to create the ribbons first(outside for loop) and add a bunch of line and point plots inside the for loop. But, the final plot only contains the lines/points from the last for loop and not all of them. This whole code is in a function and I want to return the ggplot(p, in this case) object from the function so that it can be consumed later.

hist_points_cols_val<-hist_points_cols(hist_points)
hist_points_labels_val<-hist_points_labels(hist_points)
p <- ggplot()+
geom_ribbon(aes(x = x_labels , ymin = Min , ymax = Max),data=t_out_summary,colour = '#ece9d8',fill = '#ff9999',alpha = 0.4)+
geom_ribbon(aes(x = x_labels , ymin = Qtr1 , ymax = Qtr3),data=t_out_summary,colour = '#ece9d8',fill = '#33ff99',alpha = 0.2)+
scale_x_discrete(name=scale_x, labels=plot_labels)+
scale_y_continuous(name=scale_y, labels = percent_format())+
geom_line(aes(x = x_labels,y = Median,colour="Median"),data=t_out_summary)+
geom_point(aes(x = x_labels,y = Median,colour="Median"),data=t_out_summary,shape=4)

print(hist_points_cols_val)
print(hist_points_labels_val)
for( i in 1: length(hist_points)){
p   <- p + geom_line(aes(x = x_labels,y = t_out_summary[[hist_points_cols_val[i]]],colour=hist_points_labels_val[i]),data=t_out_summary,linetype='dashed')+
    geom_point(aes(x = x_labels,y = t_out_summary[[hist_points_cols_val[i]]],colour=hist_points_labels_val[i]),data=t_out_summary,shape=4)      
}

p<-p+scale_colour_hue(name="Legend")

The data looks like this:

`> str(t_out_summary)
'data.frame':   12 obs. of  11 variables:
 $ Min     : num  0.121 0.132 0.145 0.164 0.172 ...
 $ Qtr1    : num  0.154 0.165 0.174 0.191 0.2 ...
 $ Median  : num  0.184 0.195 0.203 0.218 0.225 ...
 $ Qtr3    : num  0.22 0.231 0.238 0.249 0.253 ...
 $ Max     : num  0.398 0.381 0.37 0.351 0.339 ...
 $ p_1D    : num  0.16 0.169 0.176 0.197 0.207 ...
 $ p_2D    : num  0.153 0.163 0.171 0.193 0.204 ...
 $ p_1W    : num  0.168 0.179 0.187 0.204 0.215 ...
 $ p_2W    : num  0.141 0.154 0.163 0.184 0.196 ...
 $ p_1M    : num  0.146 0.157 0.165 0.186 0.201 ...
 $ x_labels: int  1 2 3 4 5 6 7 8 9 10 ...`

I am using the min,max points to crete the ribbon and then would like to add p_1D, p_2D points and lines. I am not sure what do you mean by the conducive way to ggplot2 graphics? I am using for loop for overlaying different plots. How do I do it without for loop?

2
As mentioned below, I can't really help without a reproducible example. However, this code looks to me to be written in the spirit of base graphics, not ggplot2 graphics. The fact that you feel you need a for loop, and even worse, the subsetting of the mapped y variable inside the loop, suggest to me that you simply need to organize your data in a way that's conducive to ggplot.joran
You are right. I was able to do it by melting the data.Neerav

2 Answers

3
votes

You have not supplied a reproducable example, so i'll try to explain what I would try:

Save your plots to a list, with the ribbons the same in every list element. And then loop through the list and add your different lines and points. Afterwards add to every list-element the legend-name.

Then you can access the plots from the list.

0
votes
hist_summ<-melt(as.matrix(hist_summary))
geom_line(aes(x=Var2,y=value,colour = Var1,group=Var1),data=hist_summ)

This solves the problem.