0

First I use set.theme() in sjPlot to set the theme for my ggplots:

# library(ggplot2)
# library(RColorBrewer)
# library(sjPlot)
# library(tidyr)

set_theme(
  geom.outline.color = "antiquewhite4", 
  geom.outline.size = .5, 
  geom.label.size = 1.5,
  geom.label.color = "blue50",
  title.color = "blue", 
  title.size = 1.5, 
  title.align = 'center',
  # axis.angle.x = 90, 
  axis.textcolor = "blue", 
  axis.textsize = 1.15,
  base = theme_bw(),
)

It is not clear to me from this code that every other x-axis label should be plotted. When the column class is integer, this is the result, every other year is labeled. A work around is to change class to character, which plots it correctly. Here is the dataframe:

df<-structure(list(Year = c(2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2020L), Type = c("Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought", "Cool, Cold Below\nNormal Temperatures", 
"Insects", "Tornadoes", "Lightning", "Frost, Freezing", "Heat, Excessive Heat,\nHigh Temp, Low Humidty", 
"Fire, Wildfire", "Wind, High Winds", "Hail", "Excessive Rain,\nMoisture, Humidty", 
"Flood,\nFlash Flooding", "Drought"), `Number of Counties` = c(0L, 
0L, 0L, 0L, 3L, 1L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 5L, 5L, 19L, 19L, 0L, 5L, 0L, 0L, 0L, 5L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 3L, 3L, 0L, 0L, 0L, 3L, 3L, 3L, 0L, 8L, 
0L, 3L, 0L, 0L, 10L, 8L, 3L, 3L, 0L, 0L, 0L, 9L, 4L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 4L, 0L, 4L, 19L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 19L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 24L)), row.names = c(NA, 
-108L), class = c("tbl_df", "tbl", "data.frame"))

And my code

# check class
class(df$Year) # [1] "integer"

## ggplot

# interpolate a color palette 
colourCount = length(unique(df$Type))
getPalette = colorRampPalette(brewer.pal(10, "RdBu"))

# make plot
ggplot(df)+
  geom_bar(aes(x=Year, y=`Number of Counties`, fill=Type), stat="identity") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), legend.key.height=unit(.75, "cm"))+
  ggtitle('VT Disaster Designations: 2012-2020\nNote some counties have more than one\ndisaster type per year')


#change year class to character

df$Year<-as.character(df$Year)

ggplot(df)+
  geom_bar(aes(x=Year, y=`Number of Counties`, fill=Type), stat="identity") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), legend.key.height=unit(.75, "cm"))+
  ggtitle('VT Disaster Designations: 2012-2020\nNote some counties have more than one\ndisaster type per year')

Any help with what is going on with sjPlot and/or ggplot here is appreciated.

ruggntub
  • 95
  • 5

1 Answers1

1

I don't know why the class makes your plot skip every other x-axis tick/label, but you can resolve it by specifying the x-axis breaks with scale_x_continuous(breaks = ...).

# your current code with df$Year as integer
p <- ggplot(df)+
  geom_bar(aes(x=Year, y=`Number of Counties`, fill=Type), stat="identity") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), legend.key.height=unit(.75, "cm"))+
  ggtitle('VT Disaster Designations: 2012-2020\nNote some counties have more than one\ndisaster type per year')

# specify the x-axis breaks
p + scale_x_continuous(breaks = 2012:2020)

p + scale_x_continuous(breaks = df$Year) # alternative
LC-datascientist
  • 1,960
  • 1
  • 18
  • 32