How to remove NA (data with missing values) in geom_col?

Dear RStudio Members,

I very much appreciate if anyone can give me some tip to how to removing data with missing values in geom_col. It took me hours to search various sources online. I tried the following methods:

  1. na.omit()
  2. complete
  3. na.rm().

But did not work. My data has 3 variables below:

  1. ID (103 participants): 1-103
  2. Dif_GAD7_0_9 (depression score, with 93 observations) which is simply a difference of pre- (week 0) and post-treatment (week 9) .
  3. Actual_Dif_GAD7_0_9 which represents reliable change or not (1=reliable change, 0=not reliable change, with 93 observations).

I used the following codes:

data2=data1[complete.cases(Dif_GAD7_0_9), ]
data3<-data2[order(Dif_GAD7_0_9),] 
S1A_GAD0_9<-data3 %>%
  arrange(Dif_GAD7_0_9) %>%
  mutate(ID2=factor(ID, levels=ID)) %>%
  ggplot(aes(x = ID2, y = Dif_GAD7_0_9,fill = as.factor(Actual_Dif_GAD7_0_9))) +
  geom_col(position = "identity") + 
  geom_hline(yintercept = 5.07) +
  scale_fill_manual(values = c("Red", "Green" ),
                    labels = c('Not reliable change', 'Reliable change')) +
  theme_classic() +
  labs(x = "Individuals", y = "Total GAD-7 change", 
       fill = "Actual_Dif_GAD7_0_9") + 
  theme(axis.text.x = element_text(size = 5)) + 
  annotate("text", x = 38, y = 10, 
           label = "      N=93\
         Mean change=6.59\
         SD=5.81", 
           col = "Black",
           size = 2.5) +
  ggtitle("A. Week 9 vs week 0") +
  theme(plot.title = element_text(size = 8.5, face ="bold")) +
  theme(axis.title = element_text(size = 7.5, face ="bold")) +
  theme(legend.title = element_text(size = 7.5)) + 
  theme(legend.text = element_text(size = 7.0)) +
  guides(color=c("Red", "Green")) +
  scale_fill_discrete(name="Reliable change", labels=c("Not reliable", "Reliable"))
S1A_GAD0_9

And the output has been with a plot as attached
Rplot.pdf (6.4 KB)

The legend with NA still remains. So, I much acknowledge if anyone can help.

I look forward to receiving your support.

Try this:

data3 %>%
  drop_na() %>%   # add this line
  arrange(Dif_GAD7_0_9) %>%
  ...

DEMO

If you have this tibble:

library(tidyverse)

mydf <- tibble(
  names = c(LETTERS[1:4], NA_character_, "F", "G", NA_character_),
  val = c(NA_real_, 2, 3, NA_real_, 5, 6, NA_real_, 8)
)

and you use drop_na() the result will be

But if you want to remove NA only from some column, you can use drop_na(names)

Or drop_na(val)

It worked perfectly now. Thank you so much for your codes and clarity.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.