{ggdist}:如果需要了解自己数据的分布,一定要画这几个图看看!
install.packages("ggdist")
library(ggdist)
library(ggplot2) # install.packages("ggplot2")
library(distributional) # install.packages("distributional")
library(patchwork) # install.packages("patchwork")
set.seed(000)
mydata <- data.frame(
groups = LETTERS[1:4],
values = rnorm(1000, mean = c(2, 8, 5, 7), sd = c(1, 1.5, 2, 5)),
types = c(rep("T1", 4), rep("T2", 4))
)
summary(mydata)

mydata1 <- data.frame(
groups = LETTERS[1:4],
mean = c(2, 8, 5, 7),
sd = c(1, 1.5, 2, 5)
)
mydata1

ggplot(mydata, aes(x = groups, y = values, fill = groups)) +
stat_slabinterval(geom = "slab") + # 相当于 stat_slab()
theme_ggdist()

ggplot(mydata1, aes(y = groups, xdist = dist_normal(mean, sd))) +
stat_slab(colour = "skyblue") +
theme_ggdist()

p1 <- ggplot(mydata, aes(x = groups, y = values, fill = groups)) +
stat_slabinterval(show.legend = FALSE) + # 相当于 stat_halfeye()
ggtitle('side = "right"') +
theme_ggdist()
p2 <- ggplot(mydata, aes(x = groups, y = values, fill = groups)) +
stat_slabinterval(side = "left",
show.legend = FALSE) +
ggtitle('side = "left"') +
theme_ggdist()
p3 <- ggplot(mydata, aes(x = groups, y = values, fill = groups)) +
stat_slabinterval(side = "both") + # 相当于 stat_eye()
ggtitle('side = "both"') +
theme_ggdist()
p1+p2+p3

ggplot(mydata, aes(x = groups, y = values, fill = groups)) +
stat_slabinterval(density = "histogram") # stat_histinterval()

ggplot(mydata, aes(x = groups, y = values, fill = groups)) +
stat_histinterval(slab_color = "grey",
outline_bars = TRUE)+
theme_ggdist()

ggplot(mydata, aes(x = types, y = values, colour = groups)) +
stat_dots(position = "dodge")

ggplot(mydata, aes(x = groups, y = values, colour = groups)) +
stat_dotsinterval(slab_color = "skyblue") +
theme_ggdist()


[1]. https://mjskay.github.io/ggdist/
2024-06-05 17:27