set.seed(123)
x <- c(rnorm(50, mean = 0, sd = 1), rnorm(50, mean = 3, sd = 1))
y <- c(rnorm(50, mean = 0, sd = 1), rnorm(50, mean = 3, sd = 1))
data <- data.frame(x = x, y = y)
library(ggplot2)
ggplot(data, aes(x, y)) +
stat_density2d(aes(fill = ..density..), geom = "polygon") +
geom_point(color = "black", alpha = 0.5) +
scale_fill_gradient(low = "white", high = "red") +
ggtitle("Estymacja gęstości 2D z danymi") +
theme_minimal()
Ten kod generuje dane, a następnie tworzy wykres z estymacją gęstości, gdzie kolor wypełnienia reprezentuje gęstość, a punkty reprezentują oryginalne dane. Wykres jest tworzony za pomocą ggplot2, co daje nam dużą elastyczność w dostosowywaniu wyglądu wykresu.
# Instalacja pakietów (jeśli nie są zainstalowane)
# install.packages("ggplot2")
# Generowanie danych
set.seed(123)
x <- c(rnorm(50, mean = 0, sd = 1), rnorm(50, mean = 3, sd = 1))
y <- c(rnorm(50, mean = 0, sd = 1), rnorm(50, mean = 3, sd = 1))
data <- data.frame(x = x, y = y)
# Wykres z użyciem ggplot2
library(ggplot2)
ggplot(data, aes(x, y)) +
stat_density2d(aes(fill = ..density..), geom = "polygon") +
geom_point(color = "black", alpha = 0.5) +
scale_fill_gradient(low = "white", high = "red") +
ggtitle("Estymacja gęstości 2D z danymi") +
theme_minimal()