cjp
cjp

Reputation: 23

r ggplot adding factor category labels to appear

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.

Upvotes: 0

Views: 1441

Answers (2)

erc
erc

Reputation: 10123

I think you get your desired plot when you remove factor() from aes() and add breaks:

ggplot(example, aes(x = 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, breaks = levels(example$WindDirCompass)) 

enter image description here

Using factor() had led to the unused level being removed:

> levels(example$WindDirCompass)
[1] "N"  "NE" "E"  "SE" "S"  "SW" "W"  "NW"
> levels(factor(example$WindDirCompass))
[1] "N"  "NE" "E"  "S"  "SW" "W"  "NW"

Upvotes: 1

Gregor Ma
Gregor Ma

Reputation: 26

If it's just that factor levels drop because they have 0 incidents, you might just need to add: + scale_x_discrete(drop=FALSE)

In your code this looks like:

ggplot(transect_weather_summary, 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)

If this is not the problem you should give some data examples

Upvotes: 0

Related Questions