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.
我想要缩放直方图的y轴,以反映每个箱子组成的比例(0到1),而不是像使用y=.密度那样使条的面积和为1。是否,或有最高的杆为1,如y=..ncount.所做的事。
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:
这给了我一个面积为1的直方图,但是高度为2000和1000:
and y=..ncount.. gives me a histogram with highest bar 1.0, and rest scaled to it:
和y = . . ncount . .给我一个最高条1.0的直方图,然后按比例缩放:
but I would like to have the first bar have a height of 0.5, and the other two 0.25.
但是我想要第一个杆的高度是0。5,另外两个0。25。
R does not recognize these uses of scale_y_continuous either.
R也不知道scale_y_continuous的这些用法。
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.
谢谢你的帮助。
2 个解决方案
#1
49
Note that ..ncount..
rescales to a maximum of 1.0, while ..count..
is the non scaled bin count.
注意,. . ncount . .扫描最大为1.0,而..计数是未缩放的bin计数。
ggplot(mydataframe, aes(x=value)) +
geom_histogram(aes(y=..count../sum(..count..)))
Which gives:
这使:
#2
12
As of ggplot2 0.9, many of the formatter functions have been moved to the scales package, including percent_format()
.
在ggplot2 0.9之后,许多格式化程序函数都被转移到scale包中,包括percent_format()。
library(ggplot2)
library(scales)
mydataframe <- data.frame(name = c("A", "B", "C", "D"),
value = c(0.0000354, 0.00768, 0.00309, 0.000123))
ggplot(mydataframe) +
geom_histogram(aes(x = value, y = ..ncount..)) +
scale_y_continuous(labels = percent_format())
#1
49
Note that ..ncount..
rescales to a maximum of 1.0, while ..count..
is the non scaled bin count.
注意,. . ncount . .扫描最大为1.0,而..计数是未缩放的bin计数。
ggplot(mydataframe, aes(x=value)) +
geom_histogram(aes(y=..count../sum(..count..)))
Which gives:
这使:
#2
12
As of ggplot2 0.9, many of the formatter functions have been moved to the scales package, including percent_format()
.
在ggplot2 0.9之后,许多格式化程序函数都被转移到scale包中,包括percent_format()。
library(ggplot2)
library(scales)
mydataframe <- data.frame(name = c("A", "B", "C", "D"),
value = c(0.0000354, 0.00768, 0.00309, 0.000123))
ggplot(mydataframe) +
geom_histogram(aes(x = value, y = ..ncount..)) +
scale_y_continuous(labels = percent_format())