1
str(tidy_factors)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   70650 obs. of  4 variables:
     $ date    : Date, format: "1992-06-01" "1992-06-02" ...
     $ Factor  : Factor w/ 5 levels "CMA","HML","MKT",..: 3 3 3 3 3 3 3 3 3 3 ...
     $ Variable: Factor w/ 2 levels "Centrality","Return": 2 2 2 2 2 2 2 2 2 2 ...
     $ Value   : num  -0.0012 -0.0022 -0.0012 -0.0029 0.0003 -0.0043 -0.0037 -0.0038 0.0026 -0.0024 ...

I would like to understand the pattern in the Value that the Factor takes over time (date).

library(tidyverse)    
tidy_factors %>% filter(Variable=="Centrality")%>%
group_by(date) %>%  
ggplot(aes(x=date,y=Factor, fill=Value))+
geom_bar(stat="identity")

enter image description here

I get to visualize it in a nice manner but the dates on the x axis are indistinguishable. When I try to scale_x_date to get a better understanding of the values that it takes according to different periods I get the following error:

tidy_factors %>% filter(Variable=="Centrality")%>%
  group_by(date) %>%  
  ggplot(aes(x=date,y=Factor, fill=Value))+
  geom_bar(stat="identity")+
  scale_x_date(date_breaks = "1 year", date_labels="%Y")

Error in charToDate(x) :
    character string is not in a standard unambiguous format

I also tried "1 years","1 month" ecc... The dates are already unique for each Factor level. Can you tell me what is the problem?

Mr Frog
  • 296
  • 2
  • 16
  • Just a dummy suggestion, removing `group_by(date)` does it change something ? Right now, I do not see any error in your code. Can you provide a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Apr 14 '20 at 22:00
  • The problem is that it is plotting a separate bar for every date, and then labelling the plot with every date. You want a plot that treats the date as a continuous variable, e.g. maybe a `geom_segment` from (x, y) to (x, y+1). – user2554330 Apr 14 '20 at 23:20
  • geom_bar used in that way seems the best solution for my plot. Isn't there any other way to change the breaks in x? – Mr Frog Apr 15 '20 at 17:44

0 Answers0