简体   繁体   English

将 R ggplot 中直方图中的 y 轴归一化为比例

[英]Normalizing y-axis in histograms in R ggplot to proportion

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 轴以反映每个 bin 组成的比例(0 到 1),而不是像使用 y=..density.. 那样将条形的面积总和为 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.感谢您的任何帮助。

Note that ..ncount.. rescales to a maximum of 1.0, while ..count.. is the non scaled bin count.请注意, ..ncount.. ..count..缩放到最大值 1.0,而..count..是未缩放的 bin 计数。

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

Which gives:这使:

在此处输入图片说明

As of the latest and greatest ggplot2 version 3.0.0, the format has changed.从最新最好的 ggplot2 版本 3.0.0 开始,格式已经改变。 Now you can wrap the y value in stat() rather than messing with .. stuff.现在您可以将y值包装在stat()而不是搞乱..东西。

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

As of ggplot2 0.9, many of the formatter functions have been moved to the scales package, including percent_format() .从 ggplot2 0.9 开始,许多格式化程序功能已移至 scales 包,包括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())

I just wanted to scale the axis, to divide the y-axis by 1000, so I did:我只是想缩放轴,将 y 轴除以 1000,所以我做了:

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

Summarizing the above answers:总结以上答案:

library(tidyverse)

mydataframe <- data.frame(name = c("A", "B", "C", "D"),
                          value = c(0.0000354, 0.00768, 0.00309, 0.000123))

ggplot(mydataframe, aes(x = value)) +
  geom_histogram(aes(y = stat(count / sum(count)))) +
  scale_y_continuous(labels = scales::percent_format()) +
  labs(x="", y="")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM