MLE
MLE

Reputation: 1043

set the last bin of histogram to a interval between a number and infinity in ggplot

I have a really skewed data and I am want to set my histogram's last bin to include a threshold number to infinity so that my histogram will be not skewed. I know we can set xlim or coord_cartisian to zooming but I want to keep all the data.

x=data.frame(100*rbeta(10000,2,50))
ggplot(data=x,aes(x))+geom_histogram(bins=20)+scale_x_continuous(breaks =seq(1,100,by=5))

enter image description here

Upvotes: 1

Views: 1505

Answers (2)

user8557834
user8557834

Reputation: 71

The accepted answer will get a little ugly if the aggreagted bin gets too big. You can map the values:

x <- mapvalues(x,
              from = c(aggBinLow:aggBinHigh),
              to = c(rep.int(aggBinLow,aggBinHigh-aggBinLow+1)))

and add a scale with distinct values:

g +
 scale_x_continuous(breaks=min:aggBinLow,labels=c(sprintf("%s",min:aggBinLow-1),">aggBinLow-1"))

Upvotes: 2

Andrew Gustar
Andrew Gustar

Reputation: 18425

Use geom_histogram(breaks=c(...)) to set customised bins, where c(...) is the vector of values you want. For example c(seq(from=1,to=11,by=1),100000)

Upvotes: 1

Related Questions