Home > database >  Deleting NAs in single plots [R]
Deleting NAs in single plots [R]

Time:08-01

I have a problem with deleting NAs in my R plot

I already tried [na.rm=TRUE] but they are still appearing in my plot. Completely deleting the NAs out of my whole data set is not an option, because i would lose some important information, i only want to remove them for single plots.

barplot_B204 <- ggplot(ds, aes(x=B204), na.rm = TRUE) 
geom_bar() 
xlab("Partei") 
ylab("count") 
ggtitle("Welche Partei haben Sie mit Ihrer Zweitstimme bei der Bundestagswahl am 26.09.2021 gewählt?")

barplot_B204

CodePudding user response:

You can (temporarily) subset your data and then pass it to the ggplot.

Base R:

library(ggplot2)
ds |>
    subset(!is.na(B204)) |>
    ggplot(aes(x = B204))  
    geom_bar()

Full tidyverse:

library(ggplot2)
library(tidyr)
ds |>
    drop_na(B204) |>
    ggplot(aes(x = B204))  
    geom_bar()
  •  Tags:  
  • r
  • Related