0

I am trying to plot a line graph that shows the frequency of different types of crime committed from Jan 2019 to Oct 2020 in each region in England.

Here's the structure: please imagine there were 9 different regions and, obviously, enough months to cover the time period mentioned above.

    structure(list(Month = c("2019-01", "2019-01", "2019-01", "2019-01", 
"2019-01", "2019-01", "2019-01", "2019-01", "2019-01", "2019-01"
), Region = c("South West", "South West", "South West", "South West", 
"South West", "South West", "South West", "South West", "South West", 
"South West"), Crime = c("Anti social behaviour and sex offences", 
"Criminal damage and arson", "Criminal damage and arson", "Theft and burglary", 
"Theft and burglary", "Anti social behaviour and sex offences", 
"Anti social behaviour and sex offences", "Anti social behaviour and sex offences", 
"Other crime", "Anti social behaviour and sex offences")), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x000002d9ecd91ef0>)

It should look something like this:

enter image description here

I realised that there are no numbers in my data frame, so ggplot probably doesn't know how to plot the number of, for example, Theft and burglary occurrencies for each month.

Any idea how to fix this issue? Thanks in advance for any help you can give!

P.s. Since there are 9 regions I need to analyse, I was thinking of creating a plot for each separate region, unless there's a visually acceptable way to plot all the regions in the same graph?

1 Answer 1

1

Try the following :

  1. Count number of Crime for each Region in each Month.
  2. Create a date column by adding an arbitrary date value. This is useful to show labels on x-axis.
  3. Plot Date on x-axis and count on y-axis showing different colour lines for each Crime.
  4. Create facets for each Region.
library(dplyr)
library(ggplot2)

df %>%
  count(Region, Month, Crime, name = 'count') %>%
  mutate(Date = as.Date(paste0(Month, '-01'))) %>%
  ggplot() + aes(Date, count, col = Crime) + 
  geom_line() + 
  facet_wrap(~Region) + 
  scale_x_date(date_labels = '%b %Y')
Sign up to request clarification or add additional context in comments.

6 Comments

Wow thanks a lot! I am trying to convert the Month column to a Date format using as.Date but I keep getting the "character string is not in a standard unambiguous format" because I'm missing the day. I've looked up everywhere and apparently I can't use as.Date unless I specify the day, which I do not need in my analysis :/ strftime does the job but it transforms everything in character format...
It was working perfectly but now I always get the following error: "x Input Date can't be recycled to size 2200. i Input Date is as.Date(paste0(crimedata$Month, "-01")). i Input Date must be size 2200 or 1, not 10616796." ... what did I do wrong?
What is the exact code that you are using? You don't need to use $ in dplyr pipe. Are you sure you are using the correct dataframe name?
I tried both with and without $. crimedata %>% count(Region, Month, Crime, name = 'Crime occurrencies') %>% mutate(Date = as.Date(paste0(Month, '-01'))) %>% ggplot() + aes(Date, count, col = Crime) + geom_line() + facet_wrap(~Region) + scale_x_date(date_labels = '%b %Y') Without $ I get: "Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error: Aesthetics must be valid data columns. Problematic aesthetic(s): y = count. Did you mistype the name of a data column or forget to add after_stat()?"
The data frame is called "crimedata", and I tried adding group = 1 to solve the "Defaulting to continuous" issue but it didn't work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.