-1

The idea for this came from this (Combining Bar and Line chart (double axis) in ggplot2). This is the code that I am using

     #Data generation
     Month1 <- c(201812,20191,20192,20193,20194,20195,20196,
       20197,20198,20199,201910,201911,201912,20201
       ,20202,20203,20204,20205,20206,20207
       ,20208,20209,202010,202011)
     annualjobgrowth<- c(44400,46000,42600,40500,42800,40500,36000,
                        34000,32300,29900,21900,24500,21000,
                        23300,16000,-6200,-275600,-249500,-149200,
                        -136500,-129900,-122800,-113900,-109500)
        Rate <- 
       c(3.3,3.4,3.1,3.0,3.1,2.9,2.6,2.5,2.3,2.1,1.6,1.7,1.5,1.7,1.1,-0.4,
      -19.5,-17.6,-10.5,-9.6,-9.1,-8.6,-8.0,-7.7)
       cesyoy <- data.frame(Month1,annualjobgrowth, Rate)

      #Chart
      library(ggplot2)
      firstces<-ggplot(cesyoy)  + 
      geom_bar(aes(x=Month1, y=annualjobgrowth),stat="identity", fill="tan1", 
      colour="sienna3")+
      geom_line(aes(x=Month1, y=Rate),stat="identity")+
      geom_text(aes(label=Rate, x=Month1, y=Rate), colour="black")+
      geom_text(aes(label=annualjobgrowth, x=Month1, y=0.9*annualjobgrowth), 
      colour="black")
      ggsave("firstces.png")

The expected result is a bar chart with 24 separate bars. Each one measuring the annualjobgrowth as provided above. The result is one of no bars. What would I include in the geom_bar section in order to have it interpret this data correctly?

Tim Wilcox
  • 1,275
  • 2
  • 19
  • 43

1 Answers1

0

Your Month1 variable is numeric. If you change it to a date you will get the anticipated result with the rest of your code. Since the scales of annualjobgrowth and Rate are very different you can use a secondary axis and scale the data to fit both into the same plot. It is still quite crowded, but you get the idea.

 
Month1 <- c(201812,20191,20192,20193,20194,20195,20196,
            20197,20198,20199,201910,201911,201912,20201
            ,20202,20203,20204,20205,20206,20207
            ,20208,20209,202010,202011)
annualjobgrowth<- c(44400,46000,42600,40500,42800,40500,36000,
                    34000,32300,29900,21900,24500,21000,
                    23300,16000,-6200,-275600,-249500,-149200,
                    -136500,-129900,-122800,-113900,-109500)
Rate <- 
  c(3.3,3.4,3.1,3.0,3.1,2.9,2.6,2.5,2.3,2.1,1.6,1.7,1.5,1.7,1.1,-0.4,
    -19.5,-17.6,-10.5,-9.6,-9.1,-8.6,-8.0,-7.7)
cesyoy <- data.frame(Month1,annualjobgrowth, Rate)
cesyoy$Month1 <- as.Date(sub("(....)(.*)", "\\1-\\2-01", Month1))

#Chart
library(ggplot2)
coeff <- 3e4 # pick some scaling coefficient
firstces <- ggplot(cesyoy, aes(x=Month1))  + 
  geom_bar(aes(y=annualjobgrowth),stat="identity", fill="tan1",
           colour="sienna3")+
  geom_line(aes(y=Rate*coeff),stat="identity")+
  geom_text(aes(label=Rate, x=Month1, y=Rate*coeff), colour="black")+
  geom_text(aes(label=annualjobgrowth, x=Month1, y=0.9*annualjobgrowth), 
            colour="black") +
  scale_y_continuous(name = "Annual Job Growth",
    sec.axis = sec_axis(~./coeff, name="Rate")
  )
firstces

Created on 2020-12-23 by the reprex package (v0.3.0)

user12728748
  • 8,106
  • 2
  • 9
  • 14