I have the following data frame:
date DGS1MO DGS3MO DGS6MO DGS1 DGS2 DGS3 DGS5 DGS7 DGS10 DGS20 DGS30
1 2006-02-28 4.47 4.62 4.74 4.73 4.69 4.67 4.61 4.57 4.55 4.70 4.51
2 2006-03-31 4.65 4.63 4.81 4.82 4.82 4.83 4.82 4.83 4.86 5.07 4.90
3 2006-04-28 4.60 4.77 4.91 4.90 4.87 4.87 4.92 4.98 5.07 5.31 5.17
4 2006-05-31 4.75 4.86 5.08 5.07 5.04 5.03 5.04 5.06 5.12 5.35 5.21
5 2006-06-30 4.54 5.01 5.24 5.21 5.16 5.13 5.10 5.11 5.15 5.31 5.19
6 2006-07-31 5.02 5.10 5.18 5.11 4.97 4.93 4.91 4.93 4.99 5.17 5.07
Using melt (from reshape2) I got this data frame:
date variable value
1 2006-02-28 DGS1MO 4.47
2 2006-03-31 DGS1MO 4.65
3 2006-04-28 DGS1MO 4.60
4 2006-05-31 DGS1MO 4.75
5 2006-06-30 DGS1MO 4.54
6 2006-07-31 DGS1MO 5.02
As you can see I have 1, 3, 6 month, along with 10, 20, 30 year time horizons. I would like to plot box-and-whisker plot for each of these columns and have the following code:
bwplot <- ggplot(df, aes(x = variable, y = value, color = variable)) +
stat_boxplot(geom = "errorbar") +
geom_boxplot() +
bwplot
However, the issue is the distance (space) between the boxplots for each variable is the same. Ideally, there should be very small distance between the boxplots for 1 month and 3 month. And the gap between the boxplots for 10 year and 20 year should be wide. To remedy, I have tried to convert the variables into numbers (1/12, 3/12, 6/12, 1, 2, etc.) and then tried this code:
levels(df$variable) <- c(0.83, 0.25, 0.5, 1, 2, 3, 5, 7, 10, 20, 30)
bwplot <- ggplot(df, aes(x = as.numeric(as.character(df$variable)), y = value, color = variable)) +
stat_boxplot(geom = "errorbar") +
geom_boxplot() +
bwplot
But what I am getting is only one huge boxplot for the entire time horizon followed by this warning msg:
Warning messages:
1: Continuous x aesthetic -- did you forget aes(group=...)?
If I try
group = variable
I get
Error: Continuous value supplied to discrete scale
What is the right way of doing this?
Thanks.
