Error in plotting with 2 different y-axes

54 Views Asked by At

I am trying to re-create a graph two different datasets (Unemployment Rate(left), and Average Hourly Earnings/(GDP/population)) with two different y axis, using ggplot like this image from FRED. Is this possible? graph from fred

I used this code (see below) but unfortunately it produced a plot (see below) that produces an error. Is there a way to adjust the y axis so we can see two plot together?

Thank you very much for your support.

R code

GRA <- ggplot() +
  geom_line(data=AVG_GDP_POP_RATIO, aes(x=date,y= value/100, color= "AVG_GDP_POP_RATIO"), size = 1.25) +
  geom_line(data=UNRT, aes(x=date,y= value/100, color= "Unemployment Rate"), size = 1.25) +
  scale_y_continuous(name= "AVG_GDP_POP_RATIO", labels = scales::percent_format(accuracy = 0.1), limits = c(0.015, 0.25), breaks = c(0, 0.05, 0.10, 0.15, 0.2, 0.25), expand = c(0, 0), sec.axis = sec_axis(~. *1000, name="Percent")) +
  xlab("Date") +
  ylab("Percent") +
  theme_x + theme(legend.position = c(.40,.90)) +
  scale_color_manual(name= NULL,values = c("#FFE98F","#00A99D","#FFE98F","#A7ACD9")) +
  theme(legend.key.width =  unit(.82, "cm")) +
  coord_cartesian(clip = "off")

ggsave(width = 10, height = 7, dpi = "retina",plot = GRA, "GRA.png", type = "cairo-png")

Error:

Warning messages:
1: Using ragg device as default. Ignoring `type` and `antialias` arguments 
2: Removed 64 rows containing missing values (`geom_line()`). 

R plot

0

There are 0 best solutions below