Removing category from boxplot

I am trying to make a boxplot on a dataframe with characters (factor levels removed while importing the dataframe using 'stringsAsFactors = FALSE' command). Here is the sample code for my plot:

boxplot(df$H~df$C, outline=F, na.rm = T, ylab="H", xlab="C", main="H by C", ylim=c(min(df$H),max(df$H)))

The problem is that one of categories on the x axis is a '0' which I would like to remove. Is there a way to prevent this category from being plotted?

The easiest method, I think, is to filter the value out of the data frame.

dfNoZero <- df[df$C != "0", ]

Then use dfNoZero in the plot. I treated the zero as a character since you said the column is imported as character.

boxplot has a subset argument. Use that.

You can try something like this:

boxplot(H~C, data=df, subset=(C!="0"), outline=F, na.rm=T, ylab="H", xlab="C", main="H by C", ylim=range(df$H))
1 Like

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