我必须用ggplot来表示3个泊松分布的混合体,并计算各组分的密度,但它不起作用。每次ggplot要么是空的,要么是平的。
我试过这种方法,一种是把所有东西都分开的方法。
# Rstudio
# Definisci i parametri delle tre distribuzioni di Poisson e i pesi
weights<- c(0.2, 0.08, 0.72)
lambda <- c(2.5, 30.4, 14.3)
# Crea un dataframe con i dati della mistura di Poisson
data <- tibble(x = 0:20, pdf_mixture = rowSums(sapply(1:3, function(i) weights[i] * dpois(0:20, lambda[i]))))
# Crea il grafico utilizzando ggplot2
ggplot(data, aes(x = x, y = pdf_mixture, color = factor(1:3))) +
geom_line(size = 1) +
labs(title = "Funzione di Densità della Mistura di 3 Poisson",
x = "Valori",
y = "Densità") +
scale_color_manual(values = c("red", "green", "purple"),
labels = c("Componente 1", "Componente 2", "Componente 3"))
字符串
这就是错误:
<error/rlang_error>
Error in `geom_line()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (21)
✖ Fix the following mappings: `colour`
---
Backtrace:
▆
1. ├─base (local) `<fn>`(x)
2. └─ggplot2:::print.ggplot(x)
3. ├─ggplot2::ggplot_build(x)
4. └─ggplot2:::ggplot_build.ggplot(x)
5. └─ggplot2:::by_layer(...)
6. ├─rlang::try_fetch(...)
7. │ ├─base::tryCatch(...)
8. │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
9. │ │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
10. │ │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
11. │ └─base::withCallingHandlers(...)
12. └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
13. └─l$compute_aesthetics(d, plot)
14. └─ggplot2 (local) compute_aesthetics(..., self = self)
15. └─ggplot2:::check_aesthetics(evaled, n)
型
1条答案
按热度按时间41zrol4v1#
您可以将颜色作为列添加到数据中。
字符串