KaC
KaC

Reputation: 287

Ignore outliers in geom_bar and geom_histogram in ggplot

I have dataframe

DF <- data.frame(respondent = factor(c(1, 2, 3, 4, 5, 6)), choice = as.numeric(c(1, 3, 3, 5, 10, 1000)))

When I plot it, the "1000" value makes the plot difficult to interpret:

library(ggplot2)
ggplot(DF, aes(choice, 1,  ymax=10))+
  geom_bar(stat = "identity")+
  scale_y_continuous (limits = quantile(DF$choice, c(0, 10)
  theme(axis.text.x = element_text(angle = 0, hjust = 1),
        text = element_text(size=20)) 

How can I remove the outlier from the plot? Is there a ggplot equivalent of ylim that I could simply set to "10"? There is a post on dealing with outliers in geom_boxplot, but I can't get the solutions to work in either geom_bar or geom_histogram.

Upvotes: 0

Views: 5888

Answers (1)

zack
zack

Reputation: 5405

I think xlim is what you're looking for?

ggplot(DF, aes(choice, 1)) +
  geom_bar(stat = "identity") + 
  xlim(c(0, 11))

You'll get the warning Warning message: Removed 1 rows containing missing values (position_stack). indicating a data-point has been removed due to limits imposed.

I removed the other bits of code to make it a 'minimal example' - if it's important to your question then feel free to follow up.

Upvotes: 2

Related Questions