How do I make a histogram that shows numbers of assaults by school level?

Viewed 45

I have a dataset with three columns. One column has the name of a school in an area. The second column has the school level (elementary, middle, or high school). The third column has the number of assaults that have taken place at each school. How could I make a histogram with this information in R? Here is my code:

hist_math2 <- ggplot(data=philadelphia.schools$SCHOOL_LEVEL_NAME, aes(x=Assaults)) +
  geom_histogram(binwidth=1, color="black", fill = "blue") +  
  xlim(0,80) +
  ylim(0,35) +
  xlab("number of schools") + 
  ylab("Assaults")
  facet_wrap(~ SCHOOL_LEVEL_NAME)
hist_math2

Here is what it looks like when I run the code, but instead of number of schools at the bottom, I was thinking it would be better to have school levels? I'm open to any suggestions about how to organize it.

Dataset looks like this:

| School_Name | School_Level| Assaults|
---------------------------------------
|JFK High Sch | High School |   45    |
---------------------------------------
|JFK Middle Sc| Middle Schoo|   22    |
______________________________________

here is the dput!:

structure(list(School_code = c(1010L, 1020L, 1030L, 1050L, 
1100L
), Attendance = c(84L, 81L, 90L, 91L, 81L), Enrollment = 
c(1155L, 
854L, 419L, 286L, 611L), New_student = c(177L, 146L, 9L, 6L, 
99L), Withdrawals = c(232L, 181L, 38L, 13L, 110L), 
African_American = c(92L, 
98L, 90L, 96L, 98L), White = c(2L, 1L, 4L, 0L, 1L), Asian = 
c(4L, 
1L, 1L, 0L, 0L), Latino = c(1L, 1L, 4L, 2L, 1L), Other = c(1L, 
1L, 1L, 1L, 0L), Pacific_Islander = c(0L, 0L, 0L, 0L, 0L), 
Low_income_family = c(86L, 
88L, 86L, 80L, 88L), SCHOOL_NAME_1 = c("JOHN BARTRAM HIGH 
SCHOOL", 
"WEST PHILADELPHIA HIGH SCHOOL", "HIGH SCHOOL OF THE FUTURE", 
"PAUL ROBESON HIGH SCHOOL", "SAYRE HIGH SCHOOL"), SCHOOL_NAME_2 
= c("BARTRAM, JOHN HIGH", 
"WEST PHILADELPHIA HIGH SCHOOL", "HIGH SCHOOL OF THE FUTURE", 
"ROBESON, PAUL HIGH SCHOOL", "SAYRE, WILLIAM L. HIGH SCHOOL"), 
ADDRESS = c("2401 S. 67TH ST.", "4901 CHESTNUT ST.", "4021 
PARKSIDE AVE.", 
"4125 LUDLOW ST.", "5800 WALNUT ST."), SCHOOL_ZIP = c(19142L, 
19139L, 19104L, 19104L, 19139L), ZIP_PLUS_4 = c(NA, NA, NA, 
NA, NA), CITY = c("PHILADELPHIA", "PHILADELPHIA", 
"PHILADELPHIA", 
"PHILADELPHIA", "PHILADELPHIA"), STATE_CD = c("PA", "PA", 
"PA", "PA", "PA"), PHONE_NUMBER = c(2154926450, 2154712902, 
2158235502, 2158238207, 2154712904), SCH_START_GRADE = c(9L, 
9L, 9L, 9L, 9L), SCH_TERM_GRADE = c(12L, 12L, 12L, 12L, 12L
), HPADDR = c("", "www.philasd.org/schools/westphila", 
"www.philasd.org/schools/hsof", 
"www.philasd.org/schools/robeson", 
"www.philasd.org/schools/sayre"
), SCHOOL_LEVEL_NAME = c("HIGH SCHOOL", "HIGH SCHOOL", "HIGH 
SCHOOL", 
"HIGH SCHOOL", "HIGH SCHOOL"), Drugs = c(10L, 6L, 1L, 0L, 
4L), Morals = c(1L, 0L, 1L, 0L, 0L), Assaults = c(31L, 25L, 
14L, 4L, 30L), Weapons = c(10L, 5L, 1L, 0L, 6L), Thefts = c(3L, 
3L, 6L, 1L, 2L), Total_suspensions = c(176L, 250L, 81L, 38L, 
200L), One_suspension = c(137L, 150L, 60L, 28L, 123L), 
Two_suspensions = c(28L, 
58L, 15L, 8L, 45L), Three_suspensions = c(5L, 24L, 4L, 2L, 
20L), Three_plus_suspensions = c(6L, 17L, 2L, 1L, 12L), 
Teacher_attendance = c(94L, 
93L, 95L, 93L, 92L), Special_education = c(21L, 20L, 14L, 
12L, 20L), Gifted_education = c(1L, 1L, 3L, 2L, 1L), 
English_second_language = c(9L, 
2L, 2L, 1L, 0L), Average_salary = c(72346L, 63537L, 63623L, 
70837L, 68200L)), row.names = c(NA, 5L), class = "data.frame")
> 

Here is the new dput:

structure(list(SCHOOL_LEVEL_NAME = c("HIGH SCHOOL", "HIGH 
SCHOOL", 
"HIGH SCHOOL", "HIGH SCHOOL", "HIGH SCHOOL", "MIDDLE SCHOOL", 
"HIGH SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
"MIDDLE SCHOOL", "HIGH SCHOOL"), Assaults = c(31L, 25L, 14L, 
4L, 30L, 10L, 2L, 33L, 22L, 5L, 8L, 1L, 12L, 11L, 12L, 7L, 6L, 
15L, 8L, 12L, 2L, 3L, 16L, 6L, 18L, 17L, 19L, 11L, 3L, 51L)), 
row.names = c(NA, 
30L), class = "data.frame")
2 Answers

Without seeing your data, try this to get a bar plot of the number of assaults by school level, which it sounds like you want:

library(ggplot2)
library(dplyr)

texas.schools %>%
  # group by level to get a sum of the assaults at elem, middle, high
  group_by(School_Level) %>%
  summarise(assaults = sum(Assaults, na.rm = T)) %>%
  # get rid of grouping
  ungroup() %>%
  # now plot the data with bars, change the x and y as needed
  ggplot(aes(x=School_Level, y=assaults)) +
  geom_bar(stat="identity")

I am guessing there are too many schools in the data to make it useful to plot those on the x-axis.

Here is a way of plotting the counts of schools by number of assaults, with a panel per type of school. The type of school must be coerced to factor with the levels in the correct order, if not ggplot would put them in alphabetic order and "HIGH" would come before "MIDDLE".

I have used a bar plot. If you want to change this, uncomment the geom_histogram line and comment out the geom_bar one.

And with a small data set, there is no need to set xlim and ylim.

texas.schools <-
  structure(list(
    SCHOOL_LEVEL_NAME = c("HIGH SCHOOL", "HIGH SCHOOL", 
                          "HIGH SCHOOL", "HIGH SCHOOL", "HIGH SCHOOL", "MIDDLE SCHOOL", 
                          "HIGH SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", "ELEMENTARY SCHOOL", 
                          "ELEMENTARY SCHOOL", "MIDDLE SCHOOL", "HIGH SCHOOL"), 
    Assaults = c(31L, 25L, 14L, 4L, 30L, 10L, 2L, 33L, 22L, 5L, 8L, 
                 1L, 12L, 11L, 12L, 7L, 6L, 15L, 8L, 12L, 2L, 3L, 16L, 6L, 18L, 
                 17L, 19L, 11L, 3L, 51L)), 
    row.names = c(NA, 30L), class = "data.frame")


library(ggplot2)

levls <- c("ELEMENTARY SCHOOL", "MIDDLE SCHOOL", "HIGH SCHOOL")

texas.schools |>
  dplyr::mutate(SCHOOL_LEVEL_NAME = factor(SCHOOL_LEVEL_NAME, levels = levls)) |>
  ggplot(aes(x = Assaults)) +
  #geom_histogram(binwidth = 1, color = "black", fill = "blue") +
  geom_bar(color = "black", fill = "blue") +
  #xlim(0,80) +
  #ylim(0,35) +
  xlab("Assaults") +
  ylab("number of schools") + 
  facet_wrap(~ SCHOOL_LEVEL_NAME) -> hist_math2

hist_math2

Created on 2022-09-24 with reprex v2.0.2

Related