Plotting two data frames with different Y scales in the same line graph in R

That lubridate command worked! Thanks a lot, I just changed the Month and Date column in their respective original datasets to a "date" class.

The reason why there isn't enough data is because that was just a sample of df4, since it has over 26k rows.

A person in another forum suggested not to merge the two datasets in the first place, so
I tried using ggplot with the two datasets separately without merging them, and it works!!! I just need to :

1) figure out how to scale the second Y axis properly for the Unemployment Rate
2) remove the "England" plot, maybe using filter() within geom_line(data =)

I tried scaling the Y axis by using a set of breaks from 0 to 6 but it only grouped them at the bottom of the Y axis on the right, following the left side scale.

Here's the output I have now, and below is the code I used to achieve it


crime_count<-crimedata %>%
  count(Region, Date, Crime, name = 'Crime_occurrencies') %>% #to count categorical variables

ggplot(mapping=aes(Date)) +
  geom_line(aes(y = Crime_occurrencies, colour = Crime), 
            data = crime_count) +
  geom_line(mapping = aes(Date, Unemployment.rate, linetype = "Unemployment Rate"),
            col = "black", data = Unemp_long,) +
  facet_wrap(~Region,
             scales = "free_y") +
  scale_x_date(breaks = seq(as.Date("2019-01-01"), as.Date("2020-10-01"), by="1 month"),
               date_labels = '%m %Y') +
  scale_y_continuous(sec.axis = sec_axis(~ (.- 249) *  0.00853131 + 2.990426)) +
  theme(axis.text.x=element_text(angle =- 90, vjust = 0.5))```

That does not seem right to me at all.
If you have 5 metrics, (4 crime types and an unemployment rate)
2 years I.e. 24 months of data
over 10 regions
that would be at most 5 * 24 * 10 = 1200 records
and if it was just example to show principle, you could through away all but a block of the first 6months, and all but 2 regions, and be sharing only 5 * 6 * 2 =60 records

sorry I had a mental breakdown and started everything from scratch again. Now I have a scatter plot I can hardly interpret :man_facepalming:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.