I am using ggplot in Rstudio to try and create a barplot showing the number of indicidents when the wind comes from a certain direction.
My data does not include wind coming from all directions (categorised as "N", "NE","E","SE","S","SW","W","NW"). As such my plots look strange, as they miss off certain wind directions.
Is there a way to add in category classes and force these to appear on the graph?
Update: Subset of my data using dput is:
structure(list(Sum.of.Incidents = c(1L, 2L, 2L, 0L, 1L, 0L, 0L,
2L, 2L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 5L, 0L, 4L, 1L,
1L, 0L, 0L, 0L, 0L), WindDirCompass = structure(c(2L, 2L, 7L,
8L, 6L, 5L, 2L, 1L, 1L, 2L, 8L, 8L, 7L, 3L, 7L, 1L, 8L, 6L, 6L,
3L, 8L, 6L, 7L, 7L, 7L, 8L, 8L, 6L), .Label = c("N", "NE", "E",
"SE", "S", "SW", "W", "NW"), class = "factor")), row.names = c(NA,
-28L), .Names = c("Sum.of.Incidents", "WindDirCompass"), spec = structure(list(
cols = structure(list(Sum.of.Incidents = structure(list(), class = c("collector_integer",
"collector")), WindDirCompass = structure(list(), class = c("collector_character",
"collector"))), .Names = c("Sum.of.Incidents", "WindDirCompass"
)), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"), class = c("tbl_df",
"tbl", "data.frame"))
The current code I am running to produce the plot:
ggplot(example, aes(x = factor(WindDirCompass),
y = Sum.of.Incidents)) +
geom_bar(stat = "identity", fill = "#990033") +
labs(title = "", x = "Wind direction", y = "Number of incidents") +
scale_x_discrete(drop = FALSE)
I have also ordered by the directions:
example$WindDirCompass <-
factor(example$WindDirCompass,
c("N", "NE","E","SE","S","SW","W","NW"))
As such "SE" is missing. Is there a way to force this factor to appear. I have other instances where only one or two directions have incidents and so most wind directions are then missing.