在for循环中使用ggplot2的多个图 [英] Multiple plots using ggplot2 in for loop

查看:154
本文介绍了在for循环中使用ggplot2的多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了这些问题(1)(2) ,但是,它们似乎与我的问题在这里没有关系。

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

我试图首先创建Ribbon(for循环外),并在for循环中添加一堆线和点图。但是,最终的图只包含最后一个for循环的行/点,而不是全部。这整个代码都在函数中,我想从函数中返回ggplot(在这种情况下)对象,以便稍后使用它。

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 ...`

我使用min,max点来克里特功能区,然后想要添加p_1D,p_2D点和线。我不确定ggplot2图形的有益方法是什么意思?我正在使用for循环覆盖不同的情节。

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?

推荐答案

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

解决了这个问题。

这篇关于在for循环中使用ggplot2的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆