我试图创建一个自定义直方图使用ggplot2,但我坚持对如何绘制箱.我有24个bin,每个bin中的数据限制和比例如下.

bin_limits <- c(0, 0.01, 0.025, seq(0.05, 0.95, by = 0.05), 0.975, 0.99, 1)

props <- c(0.07700, 0.03275, 0.04300, 0.05850, 0.04775, 0.04375,
           0.03750, 0.03825, 0.03250, 0.03325, 0.04000, 0.03000, 
           0.02950, 0.02675, 0.03000, 0.02950, 0.03700, 0.03725, 
           0.04325, 0.04250, 0.05425, 0.04675, 0.03575, 0.07325)

我想要的是一个直方图显示框的宽度的限制和高度的比例.我更喜欢用ggplot2来做这件事,但我也可以用其他方法.我现在能想到的最接近的东西是一个散点图,可以使用下面的代码制作.

data.frame(limit = bin_limits[-1], prop = props) %>% 
  ggplot(aes(x = limit, y = prop)) +
  geom_point() +
  ylim(0,.1)

推荐答案

## a vector with the center of each bin
bin_ctr <- (bin_limits[-1] + bin_limits[-length(bin_limits)])/2
## a vector with the width of each bin
bin_wdt <- (bin_limits[-1] - bin_limits[-length(bin_limits)])
## put everything in a dataframe
bin_prop <- data.frame(bin_ctr, bin_wdt, props)

library(ggplot2)

ggplot(bin_prop, aes(x=bin_ctr, y=props, width=bin_wdt)) + 
  geom_bar(stat="identity", position="identity", 
           fill = "dodgerblue4", color = "white")

创建于2024-01-26,共reprex v2.0.2

R相关问答推荐

查找图下的面积

使用gggrassure减少地块之间的空间

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

当两个图层映射到相同的美学时,隐藏一个图层的图例值

使用tidy—select创建一个新的带有mutate的摘要变量

如何使下一个按钮只出现在Rshiny 的一段时间后?""

传递ggplot2的变量作为函数参数—没有映射级别以正确填充美学

ComplexHEAT:使用COLUMN_SPLIT时忽略COLUMN_ORDER

Geom_Hline将不会出现,而它以前出现了

计算直线上点到参考点的总距离

将数据集旋转到长格式,用于遵循特定名称模式的所有变量对

为什么我对圆周率图的蒙特卡罗估计是空的?

层次树图的数据树

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

在GT()中的列之间添加空格

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用

如何在R中使用因子行求和?

无法保存gglot的所有pdf元素

如何使用list_rind在列表中保留已命名但不包含第0行的记录?

臭虫?GradeThis::grade_this_code()在`-code-check`块中失败