带R中ggplot2的Boxplot - 按月返回 [英] Boxplot with ggplot2 in R - returns by month

查看:166
本文介绍了带R中ggplot2的Boxplot - 按月返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经计算了一个价格系列的每月回报。然后我建立一个数据框如下:

$ $ $ $ $ $ $ $ $ $ b 1 0.0001015229 0.0030780203 -0.0052233836 0.017128325 -0.002427308
2 0.0005678989 0.0009249838 -0.0023294622 -0.030531971 0.001831160
3 -0.0019040392 -0.0021614791 0.0022451252 -0.003345983 0.005773503
4 -0.0006015118 0.0010695681 0.0052680258 0.008592513 0.009867972
5 0.0052736054 -0.0003181347 -0.0008505673 -0.000623061 -0.012225140
6 0.0014266119 -0.0101045071 -0.0003073150 -0.016084505 -0.005883687
7 -0.0069002733 -0.0078170620 0.0070058676 -0.007870294 -0.010265335
8 -0.0041963258 0.0039905142 0.0134996961 -0.002149331 -0.007860940
9 0.0020778541 -0.0038834826 0.0052289589 0.007271409 -0.005320848
10 0.0030956487 -0.0005027686 -0.0021452210 0.002502301 -0.001890657
11 -0.0032375542 0.0063916686 0.0009331531 0.004679741 0.004338580
12 0.0014882164 0.003957 8527 0.0136663415 0.000000000 0.003807668

...其中列是1981年至1985年的月收益和第1到第12行是一年中的月份。



我想绘制一个与下图类似的boxplot:



走?

谢谢。

month 添加到包含 month.name (R中内置常量)并将其用作因子。在因子()中设置 levels = 以确保月份按时间顺序排列,而不是按字母顺序排列。

然后将此数据框从宽格式融入长格式。在 ggplot()中使用 month 作为x值, value 作为y。

  df $ month <-factor(month.name,levels = month.name)
library( (df.long,aes(month,value))+ geom_boxplot()
ggplot code>


I have computed monthly returns from a price series. I then build a dataframe as follows:

    y.ret_1981    y.ret_1982    y.ret_1983   y.ret_1984   y.ret_1985
1   0.0001015229  0.0030780203 -0.0052233836  0.017128325 -0.002427308
2   0.0005678989  0.0009249838 -0.0023294622 -0.030531971  0.001831160
3  -0.0019040392 -0.0021614791  0.0022451252 -0.003345983  0.005773503
4  -0.0006015118  0.0010695681  0.0052680258  0.008592513  0.009867972
5   0.0052736054 -0.0003181347 -0.0008505673 -0.000623061 -0.012225140
6   0.0014266119 -0.0101045071 -0.0003073150 -0.016084505 -0.005883687
7  -0.0069002733 -0.0078170620  0.0070058676 -0.007870294 -0.010265335
8  -0.0041963258  0.0039905142  0.0134996961 -0.002149331 -0.007860940
9   0.0020778541 -0.0038834826  0.0052289589  0.007271409 -0.005320848
10  0.0030956487 -0.0005027686 -0.0021452210  0.002502301 -0.001890657
11 -0.0032375542  0.0063916686  0.0009331531  0.004679741  0.004338580
12  0.0014882164  0.0039578527  0.0136663415  0.000000000  0.003807668

... where columns are the monthly returns for the years 1981 to 1985 and the rows 1 to 12 are the months of the year.

I would like to plot a a boxplot similar to the one below:

So what can I go? And I would like my graph to read the months of the years instead of integer 1 to 12.

Thank you.

解决方案

First, add new column month to your original data frame containing month.name (built-in constant in R) and use it as factor. It is import to set also levels= inside the factor() to ensure that months are arranged in chronological order not the alphabetical.

Then melt this data frame from wide format to long format. In ggplot() use month as x values and value as y.

df$month<-factor(month.name,levels=month.name)
library(reshape2)
df.long<-melt(df,id.vars="month")
ggplot(df.long,aes(month,value))+geom_boxplot()

这篇关于带R中ggplot2的Boxplot - 按月返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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