使用此示例資料
c(5, 5, 5, 10, 20, 25, 25, 25, 30, 30)
我如何將其繪制成條形圖,顯示 X 軸(5 到 30)上的范圍,Y 顯示所有值的百分比 >= 每個間隔。
即第一個 Bar 將是 100%,因為所有值都等于或大于 5,下一個 Bar 將是 70% 的值 >= 10,然后 60% >= 20 等等......
我可以塑造資料框來做到這一點,但想知道是否有辦法更有效地實作它。
提前感謝您的任何幫助!
uj5u.com熱心網友回復:
我不確定您是否想要 x 軸上 5 到 30 之間的完整值范圍,但是您可以my_range在下面的代碼中使用變數,
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364227.html
下一篇:ggplot:定義點重疊的顏色
