1

Hi suppose I have a dataframe like this.

df = data.frame ( 
    sample= c("a","b","c","d"),
    type = c("orange", "apple","carrots", NA)
    )

What I want is to keep d in the x-axis but because the type is NA I want to be just an empty space there. Curently because of the NA ggplot literally creates a group call NA.

ggplot(data=df, 
       aes(x=sample, y= type , na.rm = TRUE ) )+ 
    geom_point(aes(color = as.factor(type)   ), size = 20, na.rm = TRUE) +
    xlab("") + ylab("")

enter image description here

Ahdee
  • 4,679
  • 4
  • 34
  • 58

3 Answers3

2
ggplot(data=df, aes(x = sample, y = type)) + 
    geom_point(aes(colour = factor(type)), size = 20) +
    scale_colour_discrete(na.value = "transparent")

Check scale_colour_discrete(na.translate = FALSE) as well

d.b
  • 32,245
  • 6
  • 36
  • 77
  • Thanks @d.b that is clever, however, the issue with this is that the NA row is still there. – Ahdee Oct 21 '20 at 23:36
1

You can also try:

library(ggplot2)
#Plot
ggplot(data=subset(df,!is.na(type)), 
       aes(x=sample, y= type) )+ 
  geom_point(aes(color = as.factor(type)   ), size = 20, na.rm = TRUE) +
  xlab("") + ylab("")+
  scale_x_discrete(limits=c('a','b','c','d'))

Output:

enter image description here

Or this (althought it can not be very optimal invoking the data in the function):

#Plot 2
ggplot(data=subset(df,!is.na(type)), 
       aes(x=sample, y= type) )+ 
  geom_point(aes(color = as.factor(type)   ), size = 20, na.rm = TRUE) +
  xlab("") + ylab("")+
  scale_x_discrete(limits=unique(df$sample))

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • 1
    the last example is perfect! Very clever, I didn't think about just adding into the x-axis manually. – Ahdee Oct 21 '20 at 23:39
  • @Ahdee Yeah, it is a trick to get the plot as you want. I hope that was helpful for you! – Duck Oct 21 '20 at 23:40
0

In your example where there is only NA value for group d it'll remove it, but if in your real dataset there are other observations for d na.omit(df) won't remove it

df = data.frame ( 
  sample= c("a","b","c","d", "d"),
  type = c("orange", "apple","carrots", NA, "orange"))
df = na.omit(df)

Then running your ggplot code:

enter image description here