Christopher Hsing
Christopher Hsing

Reputation: 1

Removed 1 rows containing missing values (position_stack) and ggplot not returning a plot

Trying to create a overlaid barchart with ggplot2 where I have a data set with Countries, Cases, and Deaths and I want to overlay the cases and deaths with each other per country. However, I keep getting the error "Removed 1 rows containing missing values (position_stack)." No too sure what I am doing wrong here.

h1n1_chart <- function(dataset) {
  h1n1_affected <- dataset %>%
    select(Country, Cases, Deaths) %>%
    gather(key = affected, value = population, -Country)

  map <- ggplot(h1n1_affected) +
    geom_col(mapping = aes(x = Country, y = population, fill = affected))
  return(map)
}

Upvotes: 0

Views: 6395

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

You're not getting an error. You're getting a warning. Here's a small subset of genuine H1N1 data:

df <- structure(list(Country = c("Brazil", "Argentina", "India", "Mexico", 
"Australia", "Thailand", "Peru", "Chile", "Venezuela", "Colombia"
), Cases = c(20469L, 9036L, 10375L, 31594L, 36559L, 19365L, 8146L, 
12248L, 1545L, 1090L), Deaths = c(1137L, 538L, 329L, 231L, 180L, 
165L, 143L, 132L, 83L, 82L)), row.names = c(NA, 10L), class = "data.frame")

df
#>      Country Cases Deaths
#> 1     Brazil 20469   1137
#> 2  Argentina  9036    538
#> 3      India 10375    329
#> 4     Mexico 31594    231
#> 5  Australia 36559    180
#> 6   Thailand 19365    165
#> 7       Peru  8146    143
#> 8      Chile 12248    132
#> 9  Venezuela  1545     83
#> 10  Colombia  1090     82

Now if I run your plotting function, it runs as expected:

h1n1_chart(df)

enter image description here

However, if one of my values is missing and I run the function

df$Cases[1] <- NA
h1n1_chart(df)

Then I get a warning:

#> Warning message:
#> Removed 1 rows containing missing values (position_stack). 

and the Brazil cases are of course missing from my plot:

enter image description here

So the warning just means you have an NA in your dataset. You could turn off the warning, but I think it's useful to know when data are missing that you might not realise are missing, since this can affect your interpretation.

Upvotes: 2

Related Questions