簡體   English   中英

在 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))

我現在想添加一個密度 plot(最好是所有三個變量)。 我在這里找到了 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.

我也找到了這個解決方案

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.. 我只知道這一點,因為幾個月前我遇到了類似的問題來制作類似的 plot。下面的代碼用於添加密度曲線:

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))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM