简体   繁体   English

直方图y轴在散点图中缩放为1,y轴更大

[英]Histogram y axis scaled to 1 inside scatterplot with greater y-axis

Alternate title: How can I scale my y-axis for the histogram only to range 0-1? 备用标题:如何仅将直方图的y轴缩放到0-1范围?

Horrible question title, so example to demonstrate. 可怕的问题标题,以举例说明。 The data here are set so that the ranges are nearly equal to my data ranges on the y-axis... about 0 to 3.5. 此处的数据设置为使范围几乎等于我在y轴上的数据范围...大约为0到3.5。

library(ggplot2)
x<-runif(100)*200
y<-runif(100)*3
xy<-data.frame(x,y)

p <- ggplot(xy) + theme_bw()
p + geom_point(aes(x, y)) + 
  geom_histogram(aes(x), alpha=1/10)

I want the histogram 'y-range' to be scaled to a max of 1. The first part of this answer shows an example, saying: 我希望将直方图 “ y-range”缩放到最大值1。 此答案的第一部分显示了一个示例,说:


You were close, but need to use (..density..)*binwidth rather than ..count../sum(..count..) 您的位置接近,但需要使用(..density ..)* binwidth而不是..count ../ sum(.. count ..)

# Your data:
all <- data.frame(fill=rep(LETTERS[1:4],c(26,24,23,29)),
                  Events=c(1,1,3,1,1,6,2,1,1,2,1,1,1,1,5,1,2,2,1,1,1,1,2,1,2,1,2,3,1,3,2,5,1,1,1,2,1,1,1,1,1,1,1,1,1,4,3,3,5,3,1,2,2,3,3,9,8,1,1,2,2,1,2,39,43,194,129,186,1,2,7,4,1,12,3,2,3,8,20,5,1,4,9,51,12,7,6,7,7,9,17,18,8,7,6,10,27,11,21,89,47,1))

bw <- 20 # set the binwidth
# plot
p1<-ggplot(all,aes(x=Events, fill=fill)) + 
  geom_histogram(aes(y=(..density..)*bw), position='dodge', binwidth=bw)
p1

but it doesn't work for me, failing with an error about there being no variable 'bw': 但这对我不起作用,失败并出现一个错误,提示没有变量“ bw”:

bw <- 30
p <- ggplot(xy) + theme_bw()
p + geom_point(aes(x, y)) + 
  geom_histogram(aes(x=x, y=..density.. * bw), alpha=1/10)

Error in eval(expr, envir, enclos) : object 'bw' not found

Goodness me, I found the notation I needed... 天哪,我找到了我需要的符号...

y=..ncount.. 

From: Normalizing y-axis in histograms in R ggplot to proportion 来自:将R ggplot中的直方图中的y轴归一化

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

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