-1

I have a dataset showing data on all tornadoes that have happened in the US between 1950-2020. I would like to make a barplot showing the total count of tornados by state (st).

First, I got the sum of tornadoes by state using:

statecount = dplyr::count(tornadodata, st, sort = TRUE)

So the data now looks like this (there's 53 total rows): raw data

And then I attempted to plot this using:

barplot(statecount$n, yaxt = "n")
axis(side=2, las=1, at=seq(0, 10000, by=1000))

This was the result:

barplot

I can tell that the graph itself is correct, but I don't know how to get the states to show up on the x axis? I've tried reducing the font size using cex.axis, and also tried rotating the labels using las=2 inside the plot(n) call, but these haven't worked. Just as a test, I tried plotting only the top 5 states using:

barplot(head(statecount, 5)$n)

And still the x axis didn't show up. Not sure what I'm doing wrong here, any pointers would be much appreciated.


Data in dput format

statecount <- structure(list(
  st = c("TX", "KS", "OK", "FL", "NE", "IA", "IL", "MS", "MO", "AL"), 
  n = c(9055L, 4338L, 4029L, 3464L, 2923L, 2654L, 2609L, 2401L, 2381L, 2293L)), 
  row.names = c(NA, 10L), class = "data.frame")
jay.sf
  • 60,139
  • 8
  • 53
  • 110
M A
  • 11
  • Can you post the output of `dput(statecount)` or, if it's too big, of `dput(head(statecount, 10))`? – Rui Barradas Sep 17 '22 at 04:20
  • @RuiBarradas sure, here it is: structure(list(st = c("TX", "KS", "OK", "FL", "NE", "IA", "IL", "MS", "MO", "AL"), n = c(9055L, 4338L, 4029L, 3464L, 2923L, 2654L, 2609L, 2401L, 2381L, 2293L)), row.names = c(NA, 10L), class = "data.frame") – M A Sep 17 '22 at 04:30

2 Answers2

0

Function barplot needs a named vector to have x axis names. This vector can be created from the data with setNames.

statecount <- structure(list(
  st = c("TX", "KS", "OK", "FL", "NE", "IA", "IL", "MS", "MO", "AL"), 
  n = c(9055L, 4338L, 4029L, 3464L, 2923L, 2654L, 2609L, 2401L, 2381L, 2293L)), 
  row.names = c(NA, 10L), class = "data.frame")

sc <- with(statecount, setNames(n, st))
barplot(sc)

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


Since you are using dplyr pipes, a ggplot2 solution would be

suppressPackageStartupMessages({
  library(ggplot2)
  library(dplyr)
})

statecount <- structure(list(
  st = c("TX", "KS", "OK", "FL", "NE", "IA", "IL", "MS", "MO", "AL"), 
  n = c(9055L, 4338L, 4029L, 3464L, 2923L, 2654L, 2609L, 2401L, 2381L, 2293L)), 
  row.names = c(NA, 10L), class = "data.frame")

statecount %>%
  mutate(st = reorder(st, n, decreasing = TRUE)) %>%
  ggplot(aes(st, n)) +
  geom_col(fill = "grey") +
  theme_classic()

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

Always read the full help page of the functions you use. Here ?barplot reveals that you may use names.arg=.

barplot(statecount$n, names.arg=statecount$st)

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110