将R ggplot中的直方图中的y轴归一化为比例 [英] Normalizing y-axis in histograms in R ggplot to proportion

查看:705
本文介绍了将R ggplot中的直方图中的y轴归一化为比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想缩放我的直方图的y轴以反映比例(0到1),而不是使条的面积总和为1,因为使用y = .. density .. does或最高的bar为1,因为y = .. ncount ..

我的输入是一个名称和值的列表,格式如下:

 名称值
A 0.0000354
B 0.00768
C 0.00309
D 0.000123

我的失败尝试之一:

  library(ggplot2)
mydataframe< ; read.delim(mydata)
ggplot(mydataframe,aes(x = value))+
geom_histogram(aes(x = value,y = .. density ..))

这给了我一个直方图,区域为1,但是高度为2000和1000:



和y = .. ncount ..给我一个柱状图,最高的柱状图为1.0,并按比例缩放:





但我希望第一个栏的高度为0.5,另外两个为0.25。



R无法识别scale_y_continuous的这些用法。

  scale_y_continuous (formatter =percent)
scale_y_continuous(labels = percent)
scale_y_continuous(expand = c(1 /(nrow(mydataframe)-1),0)

感谢您的任何帮助。

.. ncount .. 重新调整到最大值1.0,而。 .count .. 是非缩放bin数。

  ggplot(mydataframe,aes(x =值))+ 
geom_histogram(aes(y = .. count ../ sum(.. count ..)))

给出:


I have a very simple question causing me to bang my head on the wall.

I would like to scale the y-axis of my histogram to reflect the proportion (0 to 1) that each bin makes up, instead of having the area of the bars sum to 1, as using y=..density.. does, or having the highest bar be 1, as y=..ncount.. does.

My input is a list of names and values, formatted like so:

name    value
A   0.0000354
B   0.00768
C   0.00309
D   0.000123

One of my failed attempts:

library(ggplot2)
mydataframe < read.delim(mydata)
ggplot(mydataframe, aes(x = value)) +
geom_histogram(aes(x=value,y=..density..))

This gives me a histogram with area 1, but heights of 2000 and 1000:

and y=..ncount.. gives me a histogram with highest bar 1.0, and rest scaled to it:

but I would like to have the first bar have a height of 0.5, and the other two 0.25.

R does not recognize these uses of scale_y_continuous either.

scale_y_continuous(formatter="percent")
scale_y_continuous(labels = percent)
scale_y_continuous(expand=c(1/(nrow(mydataframe)-1),0)

Thank you for any help.

解决方案

Note that ..ncount.. rescales to a maximum of 1.0, while ..count.. is the non scaled bin count.

ggplot(mydataframe, aes(x=value)) +
  geom_histogram(aes(y=..count../sum(..count..)))

Which gives:

这篇关于将R ggplot中的直方图中的y轴归一化为比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆