为什么直方图调用 2,值 1.5
Why is histogram calling 2, value 1.5
这是我的数据结构
str(databody)
'data.frame': 259 obs. of 2 variables:
$ Genes_bodies: Factor w/ 259 levels "FBgn0000045",..: 142 5 82 118 107 241 165 66 78 67 ...
$ Frenq : int 4 4 4 4 4 4 5 4 3 2 ...
- attr(*, "na.action")=Class 'omit' Named int [1:1109] 260 261 262 263 264 265 266 267 268 269 ...
.. ..- attr(*, "names")= chr [1:1109] "260" "261" "262" "263" ...
h = hist(databody$Frenq)
h$density = h$counts/sum(h$counts)*100
plot(h,freq=F, col= "red")
dput(h)
structure(list(breaks = c(1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5,
5.5, 6), counts = c(28L, 47L, 0L, 65L, 0L, 69L, 0L, 42L, 0L,
8L), density = c(10.8108108108108, 18.1467181467181, 0, 25.0965250965251,
0, 26.6409266409266, 0, 16.2162162162162, 0, 3.08880308880309
), mids = c(1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25,
5.75), xname = "databody$Frenq", equidist = TRUE), .Names = c("breaks",
"counts", "density", "mids", "xname", "equidist"), class = "histogram")
str(h)
List of 6
$ breaks : num [1:11] 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 ...
$ counts : int [1:10] 28 47 0 65 0 69 0 42 0 8
$ density : num [1:10] 10.8 18.1 0 25.1 0 ...
$ mids : num [1:10] 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 5.25 5.75
$ xname : chr "databody$Frenq"
$ equidist: logi TRUE
- attr(*, "class")= chr "histogram"
h$counts 47 不应该低于 1.5 但低于 2?我做错了什么和错过了什么?还有一种方法可以制作具有 h$ 密度的单个堆叠条形图吗?
hist
会将数据分成连续的间隔,而不是特定的整数值。如果您的数据都是整数值,那么以下选项之一可能更适合您:
# Fake data
set.seed(1095)
x = sample(1:10, 100, replace=TRUE)
# Base graphics
plot(table(x), lwd=3)
# ggplot2
library(ggplot2)
ggplot(as.data.frame(x), aes(factor(x))) +
geom_bar(width=0.5, fill="grey30")
我不确定堆叠条形图在这里是否有效,但以下是如何在 ggplot 中创建一个:
ggplot(as.data.frame(table(x)), aes(x="my_label", y=Freq, fill=Freq)) +
geom_bar(stat='identity', position="stack") +
geom_text(aes(label=paste0(x, ": Freq = ", Freq), y=cumsum(Freq) - 0.5*Freq),
colour="white") +
labs(x="")
这是我的数据结构
str(databody)
'data.frame': 259 obs. of 2 variables:
$ Genes_bodies: Factor w/ 259 levels "FBgn0000045",..: 142 5 82 118 107 241 165 66 78 67 ...
$ Frenq : int 4 4 4 4 4 4 5 4 3 2 ...
- attr(*, "na.action")=Class 'omit' Named int [1:1109] 260 261 262 263 264 265 266 267 268 269 ...
.. ..- attr(*, "names")= chr [1:1109] "260" "261" "262" "263" ...
h = hist(databody$Frenq)
h$density = h$counts/sum(h$counts)*100
plot(h,freq=F, col= "red")
dput(h)
structure(list(breaks = c(1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5,
5.5, 6), counts = c(28L, 47L, 0L, 65L, 0L, 69L, 0L, 42L, 0L,
8L), density = c(10.8108108108108, 18.1467181467181, 0, 25.0965250965251,
0, 26.6409266409266, 0, 16.2162162162162, 0, 3.08880308880309
), mids = c(1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25,
5.75), xname = "databody$Frenq", equidist = TRUE), .Names = c("breaks",
"counts", "density", "mids", "xname", "equidist"), class = "histogram")
str(h)
List of 6
$ breaks : num [1:11] 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 ...
$ counts : int [1:10] 28 47 0 65 0 69 0 42 0 8
$ density : num [1:10] 10.8 18.1 0 25.1 0 ...
$ mids : num [1:10] 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 5.25 5.75
$ xname : chr "databody$Frenq"
$ equidist: logi TRUE
- attr(*, "class")= chr "histogram"
h$counts 47 不应该低于 1.5 但低于 2?我做错了什么和错过了什么?还有一种方法可以制作具有 h$ 密度的单个堆叠条形图吗?
hist
会将数据分成连续的间隔,而不是特定的整数值。如果您的数据都是整数值,那么以下选项之一可能更适合您:
# Fake data
set.seed(1095)
x = sample(1:10, 100, replace=TRUE)
# Base graphics
plot(table(x), lwd=3)
# ggplot2
library(ggplot2)
ggplot(as.data.frame(x), aes(factor(x))) +
geom_bar(width=0.5, fill="grey30")
我不确定堆叠条形图在这里是否有效,但以下是如何在 ggplot 中创建一个:
ggplot(as.data.frame(table(x)), aes(x="my_label", y=Freq, fill=Freq)) +
geom_bar(stat='identity', position="stack") +
geom_text(aes(label=paste0(x, ": Freq = ", Freq), y=cumsum(Freq) - 0.5*Freq),
colour="white") +
labs(x="")