df <- structure(list(var = c(1.05948875000079, 0.447350300925749, 1.58715715277802, 3.35221946759356, 10.673740787037, 0.000798611111111111, 0.020890879628283, 0.255016180555578, 0.0198229976853839, 2.38157386574204, 0.0626651736100515, 0.359117986110074, 4.7448886342595, 0.403400393517481, 0.34288234953803, 35.8170894675923, 0.71517319444429, 0.306423807869907, 0.299665833333576, 0.0385900578703041, 2.3121235995381, 34.1266861689808, 16.4716899189822, 0.0696409606492078, 1.37993454861144, 2.01074842592632, 0.205752581018541, 0.0885363310189159, 0.00225040509193032, 0.416561539352492, 0.0581732870362423, 0.0912203472218028, 0.369547928240564, 0.368868460646934, 0.0146497337961638, 0.929739918981989, 0.250581990739814, 5.68362587962989, 2.27526233796168, 0.0127318518526024, 0.903574016204587, 0.171842569443915, 0.308981307869156, 1.46733488425888, 38.8849084374998, 0.35137751157361, 0.619270625001302, 25.2428018981494, 2.69385361110999, 0.0759213078711872, 0.0291767592590164, 0.210829791666733, 0.00900724537118717, 0.227335300925705, 0.0179495833345034, 0.0985681712958548, 0.0158914004624994, 1.49760769676003, 0.773096180555445, 0.246598680554717, 3.26379307870236, 6.06291601851858, 1.49258361111085, 0.030361111111663, 1.39958559027701, 2.04585710648033, 0.23477070601726, 0.12810640046166, 0.0638957175915992, 0.0682201851849203, 0.0825652893522271, 0.335391203703152, 0.332132974537434, 0.0499770023149473, 0.501553495369024, 0.709669201387851, 0.0128259490733897, 0.452320601851852, 0.0807233680563944, 0.435611979166667, 0.0052125925929458, 0.0908954050916213, 1.39854887731373, 0.00768380787085604, 0.458994571759745, 0.750400266203063, 0.820049918981062, 1.52724056713007, 0.00143895833304635, 0.473669131944577, 1.05187328703701, 0.00187718749874168, 0.00396535879759877, 3.94119928240776, 0.0159443634251753, 0.130346168980554, 0.225503703703483, 0.0503306249998234, 0.123135439814241, 6.96980355324017, 0.0905845023139759, 0.21340859953728, 0.112461388889286, 0.428182499999249, 0.000459247684037244, 0.111315868055379, 0.922282870370481, 0.459490011574493, 0.887118344908511, 0.516493796295038, 0.00349604166768215, 0.0309514930568359, 0.0613791435184302, 0.0854832175925926, 0.171538472222509, 1.03678987268496, 0.00378994212934264, 0.0178086342579789, 0.0142964004642434, 13.5112740162043, 0.000601111112369431, 0.118927175926941, 0.233937430555622, 0.00819540509195239, 3.32560561342648, 6.63915153935276, 0.0176789930545621, 2.50079357638955, 0.485316365742021, 0.879955011573103, 0.0360148495380525, 0.0134506134247338, 0.00767912037118717, 0.00101491898022316, 0.0179258449072087, 0.113900694444224, 0.130830208333554, 0.171529942130049, 0.07600548611195, 0.112268171296627, 0.508835752313888, 0.00644493055564386, 7.11538414351918, 0.112205289351167, 0.0311748726859137, 0.45691256944504, 0.00543998842714009, 0.984924085647972, 0.25262748842714, 0.340315266204653, 0.062868171295634, 2.15216636574103, 6.05267392361054, 0.187748240740211, 0.0138087731489429, 0.0257476851840814, 0.316824872684148, 1.24485666666594, 22.5055494907416, 0.00303392361159678, 0.00367037036904582, 0.0622061574072749, 0.00183598379570025, 7.30075885416733, 0.431413553241226, 0.00843429398205545, 0.285934664353176, 0.947485393517547, 0.0186059374996909, 10.8247420138893, 0.644189687500949, 0.455972129630822, 0.0466556712957444, 0.00403622685207261, 0.922757222221957, 0.00243952546406675, 0.0707319328713196, 1.46238148148137, 17.6485627430567, 0.0153086921296738, 0.44410671296219, 1.18516290509314, 0.378314652776829, 0.172264386572772, 10.0003591782407, 1.07675229166669, 0.0134436111124577, 1.44476089120463, 0.0240397106469781, 0.0451388888888889, 0.084849537037037, 0.240613425925926, 0.481087962962963, 0.669618055555556, 2.04664351851852, 0.706678240740741, 0.052662037037037, 0.204097222222222, 0.0487962962962963, 1.51077546296296, 0.0938078703703704, 2.86927083333333, 0.127581018518519, 0.0848032407407407, 2.01313657407407, 0.210092592592593, 0.499594907407407, 3.75930555555556, 26.8949305555556, 1.70959490740741, 4.30515046296296, 0.0113888888888889, 0.22712962962963, 0.0810763888888889, 2.54503472222222, 0.00555555555555555, 0.373391203703704, 0.040787037037037, 0.0155092592592593, 1.90126157407407, 0.461539351851852, 0.845706018518518, 0.00864583333333333, 0.410069444444444, 0.0234837962962963, 0.0958564814814815, 0.0132986111111111, 1.00070601851852, 2.3046412037037, 0.14912037037037, 0.383506944444444, 7.97337962962963, 0.477534722222222, 0.0541087962962963, 0.0834027777777778, 1.42221064814815, 0.024212962962963, 0.0741087962962963, 1.60688657407407, 0.00658564814814815, 0.00577546296296296, 2.82027777777778, 0.0286111111111111, 0.578032407407407, 0.220277777777778, 0.262685185185185, 0.0896759259259259, 0.192905092592593, 4.61075231481481, 0.0851041666666667, 0.0808217592592593, 0.114571759259259, 0.141643518518519, 1.13153935185185, 0.111203703703704, 0.0189236111111111, 1.38303240740741, 0.00355324074074074, 3.61516203703704, 2.46391203703704, 0.0400231481481481, 3.85488425925926, 0.101736111111111, 0.0453356481481481, 1.11357638888889, 0.000729166666666667, 13.1296412037037, 0.14025462962963, 0.00393518518518518, 0.276400462962963, 0.891458333333333, 0.569178240740741, 0.0130671296296296, 0.00305555555555556, 0.584930555555556, 0.14, 21.6672453703704, 0.0619212962962963, 0.255960648148148, 0.017662037037037, 0.677673611111111, 1.9725, 0.0456944444444444, 0.153958333333333, 0.118460648148148, 0.471388888888889, 0.0225115740740741, 1.95581018518519, 1.97982638888889, 0.661296296296296, 0.863171296296296, 0.0187962962962963, 0.0920486111111111, 0.042974537037037, 0.23119212962963, 4.84853009259259, 0.104398148148148, 0.926712962962963, 6.29608796296296, 0.00128472222222222, 0.325601851851852, 0.00716435185185185, 1.94552083333333, 0.146238425925926, 8.13050925925926, 0.524016203703704, 2.59824074074074, 0.497893518518518, 0.426840277777778, 0.741689814814815, 0.0213194444444444, 3.77408564814815, 5.91921296296296, 0.0024537037037037, 0.492465277777778, 4.49981481481481, 0.0155787037037037, 0.967048611111111, 0.491840277777778, 0.872881944444444, 0.00209490740740741)), class = "data.frame", row.names = c(NA, -321L))

library(tidyverse)

df %>% 
  ggplot(aes(x = var, y = after_stat(cumsum(count / sum(count))))) +
  geom_histogram(
    binwidth = 1, boundary = 0, fill = "blue", color = "black", alpha = 0.5
  ) +
  stat_bin(binwidth = 1, geom = "text", 
           aes(label = percent(after_stat(cumsum(count / sum(count))), accuracy = 2)), vjust = 0, size = 4)

enter image description here

可以看出,64%名应该在74%左右.

推荐答案

您必须确保在geom_histogramstat_bin中使用相同的参数,即除了binwidth之外,在stat_bin中设置boundary = 0:

library(ggplot2)
library(scales)

df |> 
  ggplot(aes(x = var, y = after_stat(cumsum(count / sum(count))))) +
  geom_histogram(
    binwidth = 1, boundary = 0, fill = "blue", color = "black", alpha = 0.5
  ) +
  stat_bin(
    boundary = 0, binwidth = 1, geom = "text",
    aes(label = percent(after_stat(cumsum(count / sum(count))), accuracy = 2)),
    vjust = 0, size = 4
  )

R相关问答推荐

rvest函数read_html_live()不允许html_elements()正确读取

在处理因素时,Base R grep家族比stringr变体快得多

如何在弹性表中为类别值的背景上色

R形式的一维数字线/箱形图样式图表

基于shiny 应用程序中的日期范围子集xts索引

如何使用stat_extract_all正确提取我的目标值?

在垂直轴中包含多个ggplot2图中的平均值

如何编辑ggplot的图例字使用自定义对象(gtable)?'

用值序列对行进行子集化,并标识序列开始的列

如何在分组条形图中移动相关列?

计算满足R中条件的连续列

线性模型斜率在减少原始数据时提供NA

R中有约束的优化问题:如何用复数和对数效益函数解决问题?

按时间顺序对不同事件进行分组

在R中,我如何使用滑动窗口计算位置,然后进行过滤?

仅在R中的数据集开始和结束时删除所有 Select 列的具有NA的行

如果COLSUM为>;0,则COLNAME为向量

如何平滑或忽略R中变量的微小变化?

如何将这个小列表转换为数据帧?

R仅当存在列时才发生变异