[英]Histogram how to change y-axis from counts to frequency and standardize across two data sets
I have two data sets showing the lengths of fish and would like to create side by side histogram plots to compare the data.我有两个数据集显示鱼的长度,并想创建并排直方图来比较数据。 The issue I'm having is scaling the y-axis and bin sizes so that they are comparable.我遇到的问题是缩放 y 轴和 bin 大小,以便它们具有可比性。 Instead of counts, I wanted to use %frequency of the data.我想使用数据的 %frequency 而不是计数。 I'm also having issues with plotting them side by side when they're coming from two different sources.当它们来自两个不同的来源时,我也遇到了将它们并排绘制的问题。 Can you use the facet_grid or facet_wrap to do this?您可以使用 facet_grid 或 facet_wrap 来执行此操作吗?
Any help would be much appreciated!任何帮助将非常感激!
EDIT编辑
I used this code which just gives a basic histogram with the counts..我使用了这段代码,它只是给出了一个带有计数的基本直方图..
ggplot(snook, aes(sl)) +geom_histogram(binwidth = 20, color="black", fill= "light blue")+
ggtitle("All Snook")+
labs(x="Standard Length(mm)", y="Counts")+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Below are the results from using the code offered by SimeonL below下面是使用下面 SimeonL 提供的代码的结果
opar <- par(mfrow = c(1,2))
hist(snook$sl, breaks = seq(0, 1000, length = 50), freq = T, main = "All Snook", xlab = "Length (mm)", ylim = c(0, 50), las = 1)
hist(gut_Snook$SL, breaks = seq(0, 1000, length = 50), freq = T, main = "Culled Snook", xlab = "Length (mm)", ylim = c(0, 50), las = 1)
par(opar)
This is close, however it looks like it's still using the counts for the y-axis rather than % frequency.这很接近,但看起来它仍在使用 y 轴的计数而不是 % 频率。
Two options in base R:基础 R 中的两个选项:
set.seed(23)
df1 <- data.frame(f_size = rnorm(120, 20, 15))
x.1 <- approxfun(c(0, 100), c(0, nrow(df1)))
df2 <- data.frame(f_size = rnorm(70, 5, 5))
x.2 <- approxfun(c(0, 100), c(0, nrow(df2)))
opar <- par(mfrow = c(1,2))
hist(df1$f_size, breaks = seq(-20, 70, length = 40), freq = T, main = "", xlab = "df1_size",
ylim = x.1(c(0, 25)), las = 1, yaxt = "n", ylab = "% Cases")
axis(2, at = x.1(seq(0, 25, 5)), labels = seq(0, 25, 5), las = 1)
hist(df2$f_size, breaks = seq(-20, 70, length = 40), freq = T, main = "", xlab = "df2_size",
ylim = x.2(c(0, 25)), las = 1, yaxt = "n", ylab = "")
axis(2, at = x.2(seq(0, 25, 5)), labels = seq(0, 25, 5), las = 1)
par(opar)
breaks <- seq(-20, 70, length = 40)
df1.perc <- aggregate(df1$f_size, by = list(cut(df1$f_size, breaks, labels = F)), FUN = function(x) (length(x)/nrow(df1))*100)
df2.perc <- aggregate(df2$f_size, by = list(cut(df2$f_size, breaks, labels = F)), FUN = function(x) (length(x)/nrow(df2))*100)
opar <- par(mfrow = c(1,2))
bp <- barplot(height = merge(data.frame(Group.1 = 1:length(breaks)), df1.perc, all.x = T)$x,
xlab = "df1_size", ylab = "% Cases", ylim = c(0, 25), las = 1)
axis(1, at = approx(breaks, bp, xout = seq(-40, 70, by = 10))$y, labels = seq(-40, 70, by = 10))
bp <- barplot(height = merge(data.frame(Group.1 = 1:length(breaks)), df2.perc, all.x = T)$x,
xlab = "df1_size", ylab = "", ylim = c(0, 25), las = 1)
axis(1, at = approx(breaks, bp, xout = seq(-40, 70, by = 10))$y, labels = seq(-40, 70, by = 10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.