Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

ggplot2 - How to plot categorical variable frequency on ggplot in R

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...