with this example data
c(5, 5, 5, 10, 20, 25, 25, 25, 30, 30)
How can I plot this into a bar chart that shows a range on the X axis (5 to 30) and Y shows the percentage of all values >= each interval.
i.e. the first Bar would be a 100% as all values are equal to or above 5, the next would be 70% for values >= 10, then 60% >= 20 and so on...
I could shape the data frame to do this but was wondering if there is a way to achieve it more efficiently.
Thanks for any help in advance!
CodePudding user response:
I am not sure if you want the full range of values between 5 and 30 on the x-axis, but you can mess around with my_range
variable in the below code,
library(ggplot2)
values <- c(5, 5, 5, 10, 20, 25, 25, 25, 30, 30)
my_range <- seq(5, 30, 1)
# my_range <- unique(x) # This also works but plot will then be 5..30 on x axis by steps of 5
# For each x range value, find percentage of values greater than or equal to
heights <- lapply(my_range, function(x) sum((values >= x)) / length(values))
# ggplot2
df <- data.frame(x = my_range, y = unlist(heights))
ggplot(df, aes(x = x, y = y))
geom_col()