r - Using ggplot2, how can I create a histogram or bar plot where the last bar is the count of all values greater than some number? -
i plot histogram of info show distribution, have few outliers high compared of values, < 1.00. rather having 1 or 2 bars scrunched @ far left , nil until far right side of graph, i'd have histogram except outliers , add together bar @ end label underneath ">100%". can ggplot2 using geom_bar() this:
x <- c(rnorm(1000, mean = 0.5, sd = 0.2), rnorm(10, mean = 10, sd = 0.5)) info <- 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, = 5), ">100"), "%"))
problem that, size need be, labels end overlapping or needing plotted @ angle readability. don't need of bars labeled. there way either
a) plot in different manner other geom_bar() don't need manually add together lastly bar or b) label of bars?
i seek reply b.
i don't know if there parameter allow b) can manually define function you. i.e.:
library(ggplot2) x <- c(rnorm(1000, mean = 0.5, sd = 0.2), rnorm(10, mean = 10, sd = 0.5)) info <- data.frame(table(cut(x, breaks=c(seq(0,1, by=0.05), max(x))))) #the function remove 1 label every n labels remove_elem <- function(x,n) { (i in (1:length(x))) { if (i %% n == 0) {x[i]<-''} } return(x) } #make inital labels outside ggplot (same way before). labels <-paste0(c(seq(5,100, = 5),'>100'),'%')
now using function within ggplot function:
ggplot(data, aes(x = var1, y = freq)) + geom_bar(stat = "identity") + scale_x_discrete(labels = remove_elem(labels,2))
outputs:
i don't know if looking trick!
r ggplot2 histogram geom-bar
No comments:
Post a Comment