ggplot data.frame

3

I have a data.frame (df):

        Mês SpreadMensal
1   jan2006  -3.04045769
2   fev2006  -3.39026284
3   mar2006  -2.52839603
4   abr2006  -1.52792341
5   mai2006  -0.08979180
6   jun2006   1.02865488
7   jul2006   0.54515613
8   ago2006   0.13076121
9   set2006   0.07423530
10  out2006  -0.10478629
11  nov2006  -0.04161989
12  dez2006  -0.40504212
13  jan2007  -0.51056142
14  fev2007  -0.69200513
15  mar2007  -0.73608848
    ...
50  fev2010   4.37919039
51  mar2010   3.96563441
52  abr2010   3.55914902
53  mai2010   2.85757135
54  jun2010   2.03576435
55  jul2010   1.41129808
56  ago2010   0.80415571
57  set2010   0.90434619
58  out2010   0.97753435
59  nov2010   1.30446466
60  dez2010   1.34682208
61  jan2011   1.19608226
62  fev2011   1.07001901
63  mar2011   0.80556310
64  abr2011   0.65837218
65  mai2011   0.30898661
66  jun2011  -0.03946940
67  jul2011  -0.05902839
68  ago2011  -0.55409805
69  set2011  -0.36402331
70  out2011  -0.21302781
71  nov2011  -0.15414390
72  dez2011   0.29072412
73  jan2012   0.89794991
74  fev2012   1.19541625
75  mar2012   1.89563925
76  abr2012   1.98807663
77  mai2012   1.89189602
78  jun2012   2.24721387
79  jul2012   2.00140636
80  ago2012   2.33372249
81  set2012   2.59733963
82  out2012   2.42408844
83  nov2012   2.40611260
84  dez2012   2.35786991
85  jan2013   2.53196473
86  fev2013   2.82097624
87  mar2013   2.84742861
88  abr2013   2.61528861
89  mai2013   2.53958494
90  jun2013   3.15504505
91  jul2013   2.91702023
92  ago2013   3.26063617
93  set2013   3.12586325
94  out2013   2.55812363
95  nov2013   3.02603774
96  dez2013   3.21191655
97  jan2014   3.13692051
98  fev2014   2.71665822
99  mar2014   2.50222843
100 abr2014   2.08358406
101 mai2014   1.67663242
102 jun2014   1.38021497
103 jul2014   1.15440685
104 ago2014   0.99317141
105 set2014   0.66278156
106 out2014   0.87560496
107 nov2014   0.97597018
108 dez2014   0.45712044

I'm trying to make a simple plot with ggplot, but it does not:

ggplot(data = df, aes(x = Mês, y = SpreadMensal)) + geom_line() 

class(df)
[1] "data.frame"

class(df$Mês)
[1] "factor"

One suggestion I saw was to turn the Month column into date:

     df$Mês<-as.Date(df$Mês, format="%m%Y")

class(df$Mês)
[1] "Date"

But this command gives me this:

 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [32] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [63] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [94] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 

My data.frame looks like this:

     Mês SpreadMensal
1   <NA>  -3.04045769
2   <NA>  -3.39026284
3   <NA>  -2.52839603
4   <NA>  -1.52792341
5   <NA>  -0.08979180
6   <NA>   1.02865488
7   <NA>   0.54515613
8   <NA>   0.13076121
9   <NA>   0.07423530
10  <NA>  -0.10478629
11  <NA>  -0.04161989
...
    
asked by anonymous 13.10.2015 / 16:13

2 answers

2

When you try to convert the expression "jan2006" to a date, you are missing the day of the month - without this R there is no way to know what date you want. You can, while doing the conversion, add the day ( paste0("01", mes) ) at the beginning of the value you have, and with that the conversion works, as the code below shows.

#mes <- c("jan2006", "fev2006", "mar2006", "abr2006",
#         "mai2006", "jun2006", "jul2006", "ago2006",
#         "set2006", "out2006", "nov2006", "dez2006",
#         "jan2007", "fev2007", "mar2007")
mes <- c("jan2006", "feb2006", "mar2006", "apr2006",
         "may2006", "jun2006", "jul2006", "aug2006",
         "sep2006", "oct2006", "nov2006", "dec2006",
         "jan2007", "feb2007", "mar2007")
spread <- c(-3.04045769, -3.39026284, -2.52839603, -1.52792341,
            -0.08979180, 1.02865488, 0.54515613, 0.13076121,
            0.07423530, -0.10478629, -0.04161989, -0.40504212,
           -0.51056142, -0.69200513, -0.73608848)
df <- data.frame(mes, spread)
df$dia <- as.Date(paste0("01", df$mes), "%d%b%Y")
ggplot(data = df, aes(x = dia, y = spread)) + geom_line()

Note that the rollover will depend on the locale where the R session is running. On my machine (which is in English), I had to change the names of the months accordingly. You can resolve this by using Sys.setlocale to force the language you want to use.

    
13.10.2015 / 18:09
0

Note that it is not necessary to convert the variable to month.

To facilitate testing with different chart types, let's save the ggplot structure on the g object:

library(ggplot2)
g <- ggplot(df_teste, aes(x = Mês, y = SpreadMensal))

If you plot g , the graph should appear in the rstudio viewer without the rows.

However, geom_line needs to know which points to connect, so it is necessary to set group = 1 within aes() ggplot() or within aes() geom_point . It may be best to use aes() of geom_line to be able to continue using the g object without changing the chart base.

g + geom_line(group = 1)

g + geom_line(group = 1) + geom_point()

The explanation can be found in Cookbook for R

    
23.04.2018 / 19:53