Home > front end >  Is there a way to remove the N/A boxplots when you select specific rows to visualize?
Is there a way to remove the N/A boxplots when you select specific rows to visualize?

Time:02-08

So I made a boxplot using ggplot with the code below

ggplot(df, aes(x = factor(Letters, level = c("A", "B", "C", "D")), y = Expression))   geom_boxplot()   
  scale_x_discrete(guide = guide_axis(n.dodge=3))   xlab("Letters")   ylab("Expression")

The plot that is generated is good except that all the Letters in the dataframe that haven't been called "E-Z" also are shown in the plot under the category NA. But I don't want the NA category to be shown. You may be thinking why don't I just delete those rows that I don't want visualize. The issue is I'm making 6 of these plots with different rows and I was wondering if there's a way of doing it that doesn't involving making 6 dataframes and removing the rows I don't want each and everytime. First 6 rows of the dataframe are shown below

Letter Expression
A 10
B 20
C 15
D 12
E 16
F 18

CodePudding user response:

You can pipe your df into a filter() and then pipe it to ggplot. This will not change the content of your original dataframe.

library(tidyverse)

df %>% filter(Letters %in% c("A", "B", "C", "D")) %>%
  ggplot(., aes(x = Letters, y = Expression))   
  geom_boxplot()   
  scale_x_discrete(guide = guide_axis(n.dodge=3))   
  xlab("Letters")   
  ylab("Expression")

filter_ggplot

  •  Tags:  
  • Related