-2

I am working with R in RStudio and would like to plot via highchart package a graphic that includes on the x-Axis the crime type, and on the y-Axis the arrest rate in %. So to see on which crime type the highest arrest was made. I am working with following code in shiny, which is working but not ploting what exactly I want:

output$top20arrestCrime <- renderHighchart({
  arrestCrimeAnalysis <- cc %>% 
    group_by(Primary.Type, Arrest == TRUE) %>% 
    summarise(Total = n()) %>% 
    arrange(desc(Total))

  hchart(arrestCrimeAnalysis, "column", hcaes(x = Primary.Type, y = Total, color = Total)) %>%
    hc_exporting(enabled = TRUE, filename = "Top_20_Locations") %>%
    hc_title(text = "Top 20 Crime Types") %>%
    hc_subtitle(text = "(2001 - 2016)") %>%
    hc_xAxis(title = list(text = "Crime Type"), labels = list(rotation = -90)) %>%
    hc_yAxis(title = list(text = "Arrest Rate %")) %>%
    hc_colorAxis(stops = color_stops(n = 10, colors = c("#d98880", "#85c1e9", "#82e0aa"))) %>%
    hc_add_theme(hc_theme_smpl()) %>%
    hc_legend(enabled = FALSE)
})

I am working with this dataset: https://www.kaggle.com/currie32/crimes-in-chicago.

when I run the code, it just show me on the x-Axis the crime type (e.g. THEFT, ROBERRY) etc, which is correct and on the y-Axis the sum of thefts for example from 2001-2016. But I want on the y-Axis the Arrest Rate in percentage, so how many arrests happened. and this in a highcharter with the top 20 arrests crime types.

Example screenshot of Shiny app

img

pogibas
  • 27,303
  • 19
  • 84
  • 117
S002
  • 31
  • 7
  • 2
    "It doesn't do exactly what I want" is not a usable problem statement. You need to specify exactly what it is *not* doing. – Robert Harvey Dec 27 '18 at 18:11
  • @RobertHarvey when I run the code, it just show me on the x-Axis the crime type (e.g. THEFT, ROBERRY) etc, which is correct and on the y-Axis the sum of thefts for example from 2001-2016. But I want on the y-Axis the Arrest Rate in percentage, so how many arrests happened. and this in a highcharter with the top 20 arrests crime types. – S002 Dec 27 '18 at 18:21
  • Include that information in your question. A picture of what you're talking about would be nice, too. – Robert Harvey Dec 27 '18 at 18:22
  • @RobertHarvey I did. – S002 Dec 27 '18 at 18:52
  • 1
    Why is this tagged `ggplot2`? – camille Dec 27 '18 at 18:52
  • @S002 the name-calling was a weird move so I'm glad it's gone. This is actually more of a math question than a specific plotting question: arrest rate is generally defined as the number of arrests divided by the population, often then multiplied by some factor. For example, the rate of arrests per 10000 people would be arrests / population * 10000. So you need population counts, ideally for each year since the population may have changed – camille Dec 27 '18 at 20:36
  • Alternatively, you mention wanting a percentage. So if what you want instead is the share of all arrests that are for each type of crime, you would calculate the number of arrests of *one* type divided by the number of arrests of *all* types. It's unclear whether you want a rate or a percentage, since you've used them interchangeably but they're in fact different measures. – camille Dec 27 '18 at 20:37
  • @camille I want something like this: https://www.kaggle.com/lpuglisi/avoiding-arrest-in-chicago scroll down and you will find the plot. also the code is available, but I would like to do it with highcharter, thats why I asked here... – S002 Dec 27 '18 at 21:21

1 Answers1

0

Your problem is you haven't told highcharter to put Arrest Rate on the y axis. You've told it to put Total on the y axis:

arrestCrimeAnalysis <- cc %>% 
    group_by(Primary.Type, Arrest == TRUE) %>% 
    summarise(Total = n()) %>% 
    arrange(desc(Total))
hchart(arrestCrimeAnalysis, "column", hcaes(x = Primary.Type, y = Total, color = Total))

Change y = Total to y = ArrestRate or whatever your rate column name is.

sastrup
  • 189
  • 1
  • 11
  • thanks for your answer sastrup. I only have a column named "Arrest" which is filled with value of "TRUE" or "FALSE". I dont have any arrest rate column. What should I put in y in my case? – S002 Dec 29 '18 at 18:45
  • Then you need to define an arrest rate calculation. It won’t just appear. What do you define as arrest rate? Will it be [# records where Arrest = True] / [# records]? – sastrup Dec 29 '18 at 18:50