在ggplot2中将密度线添加到具有多个变量的直方图
Adding a density line to a histogram with multiple variables in ggplot2
我有如下数据:
A <- structure(c(9, 7, 9, 9, 9, 8, 9, 6, 4, 7, 9, 9, 9, 8, 7, 7, 9,
8, 8, 9, 5, 5, 8, 7, 5, 9, 9, 7, 7, 9, 8, 7, 8, 9, 4, 7, 9, 8,
6, 7, 7, 4, 8, 6, 9, 9, 8, 1, 9, 9, 9, 8, 9, 9, 6, 7, 4, 7, 9,
6, 6, 9, 9, 8, 6, 8, 7, 7, 7, 5, 9, 5, 7, 9, 8, 4, 9, 8, 8, 8,
5, 8, 1, 7, 7, 5, 6, 9, 5, 9, 6, 9, 6, 9, 9, 9, 8, 9, 9, 9, 9,
4, 6, 4, 8, 6, 8, 8, 7, 4, 6, 7, 4, 8, 8, 8, 7, 9, 3, 8, 8, 6,
9, 8, 8, 6, 5, 8, 3, 8, 6, 8, 7, 7, 6, 9, 5, 9, 8, 7, 9, 7, 9,
9, 8, 9, 6, 8, 9, 8, 6, 8, 9, 9, 9, 4, 8, 8, 5, 8, 7, 8, 8, 9,
9, 6, 8, 5, 9, 8, 7, 9, 9, 7, 6, 8, 7, 7, 8, 9, 6, 7, 8, 9, 7,
6, 6, 9, 7, 7, 8, 7, 7, 2, 4, 9, 9, 7, 7, 9, 7, 6, 9, 9, 8, 5,
5), label = NA_character_, class = c("labelled", "numeric"))
B <- structure(c(9, 9, 9, 8, 8, 9, 6, 9, 8, 8, 6, 9, 9, 9, 6, 7, 9,
7, 8, 9, 7, 9, 9, 8, 7, 9, 8, 7, 8, 9, 8, 9, 9, 9, 9, 7, 9, 7,
8, 9, 7, 7, 8, 4, 6, 9, 7, 7, 9, 9, 9, 8, 9, 8, 9, 9, 4, 8, 9,
8, 7, 9, 9, 8, 7, 8, 9, 8, 2, 7, 8, 8, 8, 8, 8, 6, 4, 9, 9, 8,
3, 7, 3, 8, 8, 9, 7, 9, 5, 6, 7, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9,
7, 3, 7, 9, 7, 7, 7, 8, 8, 9, 9, 8, 8, 9, 6, 9, 9, 6, 7, 8, 7,
8, 9, 9, 7, 6, 8, 7, 9, 6, 5, 8, 8, 7, 9, 8, 9, 9, 7, 9, 7, 9,
8, 7, 9, 4, 8, 7, 7, 9, 9, 9, 9, 9, 4, 9, 9, 6, 7, 6, 7, 8, 9,
8, 9, 5, 9, 8, 8, 8, 9, 9, 6, 8, 8, 8, 8, 8, 8, 7, 8, 9, 9, 9,
7, 4, 8, 7, 7, 9, 8, 8, 7, 5, 8, 9, 8, 8, 9, 8, 5, 8, 9, 8, 9,
7), label = NA_character_, class = c("labelled", "numeric"))
library(ggplot2)
df <- data.frame(value = c(A, B),
variable = rep(c("tax", "truth"), each = length(A)))
ggplot(df) +
geom_bar(aes(value, fill = variable), position = "dodge") +
scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
我现在想添加一个密度图(最好是所有三个变量)。我发现了这段代码 here,作者是 Thiery,我试图修改它,但我无法让它工作:
ggplot(df, aes(a = A)) +
geom_histogram(aes(y = ..density..), binwidth = 5) +
geom_density()
Error: Aesthetics must be either length 1 or the same as the data (414): a
Run `rlang::last_error()` to see where the error occurred.
我还找到了this solution:
library(MASS) # for fitsidtr(...)
# excellent fit (of course...)
ggplot(df) +
geom_bar(aes(value, fill = variable), position = "dodge") +
scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) +
stat_function(fun=dbeta,args=fitdistr(df$A,"beta",start=list(shape1=1,shape2=1))$estimate)
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
Error in fitdistr(df$A, "beta", start = list(shape1 = 1, shape2 = 1)) :
'x' must be a non-empty numeric vector
但是我运行遇到了同样的问题。
任何人都可以向我解释我做错了什么吗?
您实际上需要将 y
设置为 ..count..
。我只知道这一点,因为几个月前我遇到了类似的问题来制作类似的情节。下面的代码用于添加密度曲线:
ggplot(df, aes(value, fill = variable)) +
geom_density(aes(y = ..count..), size = 0.7, alpha = 0.3) +
geom_bar(position = "dodge") +
scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
我有如下数据:
A <- structure(c(9, 7, 9, 9, 9, 8, 9, 6, 4, 7, 9, 9, 9, 8, 7, 7, 9,
8, 8, 9, 5, 5, 8, 7, 5, 9, 9, 7, 7, 9, 8, 7, 8, 9, 4, 7, 9, 8,
6, 7, 7, 4, 8, 6, 9, 9, 8, 1, 9, 9, 9, 8, 9, 9, 6, 7, 4, 7, 9,
6, 6, 9, 9, 8, 6, 8, 7, 7, 7, 5, 9, 5, 7, 9, 8, 4, 9, 8, 8, 8,
5, 8, 1, 7, 7, 5, 6, 9, 5, 9, 6, 9, 6, 9, 9, 9, 8, 9, 9, 9, 9,
4, 6, 4, 8, 6, 8, 8, 7, 4, 6, 7, 4, 8, 8, 8, 7, 9, 3, 8, 8, 6,
9, 8, 8, 6, 5, 8, 3, 8, 6, 8, 7, 7, 6, 9, 5, 9, 8, 7, 9, 7, 9,
9, 8, 9, 6, 8, 9, 8, 6, 8, 9, 9, 9, 4, 8, 8, 5, 8, 7, 8, 8, 9,
9, 6, 8, 5, 9, 8, 7, 9, 9, 7, 6, 8, 7, 7, 8, 9, 6, 7, 8, 9, 7,
6, 6, 9, 7, 7, 8, 7, 7, 2, 4, 9, 9, 7, 7, 9, 7, 6, 9, 9, 8, 5,
5), label = NA_character_, class = c("labelled", "numeric"))
B <- structure(c(9, 9, 9, 8, 8, 9, 6, 9, 8, 8, 6, 9, 9, 9, 6, 7, 9,
7, 8, 9, 7, 9, 9, 8, 7, 9, 8, 7, 8, 9, 8, 9, 9, 9, 9, 7, 9, 7,
8, 9, 7, 7, 8, 4, 6, 9, 7, 7, 9, 9, 9, 8, 9, 8, 9, 9, 4, 8, 9,
8, 7, 9, 9, 8, 7, 8, 9, 8, 2, 7, 8, 8, 8, 8, 8, 6, 4, 9, 9, 8,
3, 7, 3, 8, 8, 9, 7, 9, 5, 6, 7, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9,
7, 3, 7, 9, 7, 7, 7, 8, 8, 9, 9, 8, 8, 9, 6, 9, 9, 6, 7, 8, 7,
8, 9, 9, 7, 6, 8, 7, 9, 6, 5, 8, 8, 7, 9, 8, 9, 9, 7, 9, 7, 9,
8, 7, 9, 4, 8, 7, 7, 9, 9, 9, 9, 9, 4, 9, 9, 6, 7, 6, 7, 8, 9,
8, 9, 5, 9, 8, 8, 8, 9, 9, 6, 8, 8, 8, 8, 8, 8, 7, 8, 9, 9, 9,
7, 4, 8, 7, 7, 9, 8, 8, 7, 5, 8, 9, 8, 8, 9, 8, 5, 8, 9, 8, 9,
7), label = NA_character_, class = c("labelled", "numeric"))
library(ggplot2)
df <- data.frame(value = c(A, B),
variable = rep(c("tax", "truth"), each = length(A)))
ggplot(df) +
geom_bar(aes(value, fill = variable), position = "dodge") +
scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
我现在想添加一个密度图(最好是所有三个变量)。我发现了这段代码 here,作者是 Thiery,我试图修改它,但我无法让它工作:
ggplot(df, aes(a = A)) +
geom_histogram(aes(y = ..density..), binwidth = 5) +
geom_density()
Error: Aesthetics must be either length 1 or the same as the data (414): a
Run `rlang::last_error()` to see where the error occurred.
我还找到了this solution:
library(MASS) # for fitsidtr(...)
# excellent fit (of course...)
ggplot(df) +
geom_bar(aes(value, fill = variable), position = "dodge") +
scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) +
stat_function(fun=dbeta,args=fitdistr(df$A,"beta",start=list(shape1=1,shape2=1))$estimate)
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
Error in fitdistr(df$A, "beta", start = list(shape1 = 1, shape2 = 1)) :
'x' must be a non-empty numeric vector
但是我运行遇到了同样的问题。
任何人都可以向我解释我做错了什么吗?
您实际上需要将 y
设置为 ..count..
。我只知道这一点,因为几个月前我遇到了类似的问题来制作类似的情节。下面的代码用于添加密度曲线:
ggplot(df, aes(value, fill = variable)) +
geom_density(aes(y = ..count..), size = 0.7, alpha = 0.3) +
geom_bar(position = "dodge") +
scale_fill_manual(values = c(rgb(0,0,1,0.5), rgb(1,0,0,0.5))) +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))