r - Remove unused factor levels from a ggplot bar plot -


I want to contrast the opposite, although not only about the legends, plots about it.

Asking other such questions how to keep unused factor levels should be really removed from me. I have many name variables and many columns (wide format) Here's a duplicate example:

  Library (ggplot2) df   

I get this:

 Enter the image details here

Only the names whose names appear in my bar plot of the same variant n (e.g., BK There will be no spaces).

The reuse of the base plot code will be quite easy if I can simply change my output filename and y = var bit, if possible, drops on the result for each plot I do not want to minimize my data frame to use!


na.omit ()

  Library (ggplot2) df & lt; - data.frame (name = c ("A")  , "b", "c"), var1 = c (1, na, 2), var2 = c (3,4, 5), var3 = c (NA, 6,7)) ggplot (df, aes (x) = name, y = var1)) + geom_bar ()   

me var1 is required to use the na.omit () to plot because an NA is present but since na.omit ensures that the value all the columns Exists, the plot removes A as well as there is no NA in var3 . This is similar to my data. I have 15 total responses whose time About IPAP in the sector. I only want to remove those factor levels which do not have the values ​​of those current plotted vectors which are not in the any vector in the entire data frame.

An easy choice na.omit () on your data frame DF remove those rows with NA

  ggplot (na.omit (df), AES (x = name, y = var1) ) + Geom_bar ()  
  ggplot (df [! Is.na (df $ var1),], AES (x = name, y = var1) while viewing your updates) / Code>  

works fine and only understands in . Given that you only plot the name and on , apply na.omit () in the data frame containing only those variables < / P>

  ggplot (na.omit (df [, c ("name", "var1"), aes (x = name, y = var1)) + geom_bar ()    

Comments