-2

I'm creating a bar chart using ggplot2 in R and I want to remove the missing values in my x-values, I have tried a few different ways of getting ride of "NA"s but nothing seems to change the output. Currently my code reads (data is not publicly available so I have changed variable names):

ggplot(data = mydata, aes(x= x_mydata, y= y_mydata, na.rm = TRUE)) +
  stat_summary(fun.y = "mean", geom="bar", na.rm = TRUE)
mdavis
  • 15
  • 1
  • 4
  • Possible duplicate of [Remove rows with NAs (missing values) in data.frame](https://stackoverflow.com/questions/4862178/remove-rows-with-nas-missing-values-in-data-frame) – divibisan Jun 25 '18 at 15:54
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 25 '18 at 15:55
  • There is no `na.rm` parameter in `aes`, so that won't do anything. You should remove `NA` values from your data before plotting (or as you pass your data to `data=`. – divibisan Jun 25 '18 at 15:55
  • Possibly relevant: In `stat_summary` you can pass additional arguments to the function you are using with the `fun.args` argument. That would look like `fun.args = list(na.rm = TRUE)` in the `stat_summary` layer. – aosmith Jun 25 '18 at 15:57

3 Answers3

1

I don't know exactly what you want, but wrapping your data name in na.omit should work. So something like ggplot(data = na.omit(df), aes = ...

Curtis
  • 449
  • 1
  • 4
  • 17
0

I cannot reproduce. This works just fine

mydata <- data.frame(
  x_mydata = c(1, NA, 1, 2, 2, 2, 3, 3, NA),
  y_mydata = c(1,2,NA,4,5, 6,7, NA, NA)
)

ggplot(data = mydata, aes(x= x_mydata, y= y_mydata)) +
  stat_summary(fun.y = "mean", geom="bar", na.rm = TRUE)

Note that there is no na.rm aesthetic so you don't need to pass it in the aes().

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
0

I would recommend to use

data = mydata[!is.na(mydata$x_mydata), ]

If you are using a data.table, it is more simply

data = mydata[!is.na(x_mydata)]