ggplot() +
geom_histogram(data = df1, aes(x=meanf,fill = "g", color="g"), alpha = 0.6,binwidth = 0.02)+
geom_histogram(data = df2, aes(x=meanf,fill = "b", color="b"), alpha = 0.4,binwidth = 0.02)+
scale_colour_manual(name="N1", values=c("g" = "green", "b"="blue"), labels=c("b"="1", "g"="2")) +
scale_fill_manual(name="N2", values=c("g" = "green", "b"="blue"), labels=c("b"="1", "g"="2"))+
theme_bw()+
ggsave('temp.jpg')
我得到了直方图计数的情节,但是我希望在0到1之间缩放它。从这个问题Normalizing y-axis in histograms in R ggplot to proportion,我理解如何为一个数据帧做这个,但是如果我使用两个数据帧怎么办?数据框,如我的问题?
答案 0 :(得分:0)
首先缩放数据!只需创建一个临时数据集,其y轴在0和某个数字之间缩放 - 将所有内容除以该特定数字(如果您希望最顶部为1.00,则将除数设置为最高数字)。
答案 1 :(得分:0)
geom_histogram(data = df1, aes(y = ..ncount..,x=meanf,fill = "g", color="g"))
应该这样做。
如果您希望两个直方图都用相同的除数进行归一化:
首先获得原始直方图的y范围。 Refer here
ggobj <- ggplot() +
geom_histogram(data = df1, aes(x=meanf,fill = "g", color="g"), alpha = 0.6,binwidth = 0.02)+
geom_histogram(data = df2, aes(x=meanf,fill = "b", color="b"), alpha = 0.4,binwidth = 0.02)
y_max <- ggplot_build(ggobj)$panel$ranges[[1]]$y.range[2]
然后重新创建直方图并使用你得到的y_range进行缩放。
p <- ggplot() +
geom_histogram(data = df1, aes(y_max=y_max, y=..count../y_max,x=meanf,fill = "g", color="g"), alpha = 0.6,binwidth = 0.02)+
geom_histogram(data = df2, aes(y_max=y_max, y=..count../y_max,x=meanf,fill = "b", color="b"), alpha = 0.4,binwidth = 0.02)