Using ggplot2, how can I create a histogram or bar graph where the last bar is the counter of all values ​​greater than some number?

I would like to build a histogram of my data to show its distribution, but I have a few outliers that are really high compared to most values, 1.00. Instead of having one or two bars knocked down in the far left corner, and then nothing to the right side of the chart, I would like to have a histogram with everything except outliers, and then add a panel at the end, where the label under it is "> 100% " I can do this with ggplot2 using geom_bar () as follows:

 X <- c(rnorm(1000, mean = 0.5, sd = 0.2), 
   rnorm(10, mean = 10, sd = 0.5))
 Data <- data.frame(table(cut(X, breaks=c(seq(0,1, by=0.05), max(X)))))

 library(ggplot2)
 ggplot(Data, aes(x = Var1, y = Freq)) + geom_bar(stat = "identity") +
  scale_x_discrete(labels = paste0(c(seq(5,100, by = 5), ">100"), "%"))

histogram The problem is that for the size I need it, these labels end up overlapping or need to be angled for readability. I really do not need all the bars with the inscription. Is there any way either

  • A) build it differently than geom_bar (), so I don’t need to manually add this last panel or
  • B) only label some of the bars?
+4
source share
1 answer

I will try to answer B.

I do not know if there is a parameter that will allow you to do B), but you can manually determine the function that will do this for you. I.e:.

library(ggplot2)
X <- c(rnorm(1000, mean = 0.5, sd = 0.2), 
       rnorm(10, mean = 10, sd = 0.5))
Data <- data.frame(table(cut(X, breaks=c(seq(0,1, by=0.05), max(X)))))

#the function will remove one label every n labels
remove_elem <- function(x,n) {
  for (i in (1:length(x))) {
    if (i %% n == 0) {x[i]<-''}
  }  
  return(x)  
}

#make inital labels outside ggplot (same way as before). 
labels <-paste0(c(seq(5,100, by = 5),'>100'),'%')

Now, using this function inside the ggplot function:

ggplot(Data, aes(x = Var1, y = Freq)) + geom_bar(stat = "identity") +
  scale_x_discrete(labels = remove_elem(labels,2))

outputs:

enter image description here

I don’t know, this is what you are looking for, but it is a trick!

+1

All Articles