R: 密度图
2021-11-09 本文已影响0人
胡童远
输入
library("ggplot2")
library("reshape2")
df = data.frame(A = rnorm(100, 100, 10),
B = rnorm(100, 110, 10),
id = 1:100)
bx = melt(df, id='id')
ggplot 1
ggplot(bx, aes(x = value, color = variable)) +
geom_density(size = 1) +
theme_classic() +
labs(x="", y="Density", title="", color="") +
theme(axis.title = element_text(size = 15),
axis.text = element_text(size = 15),
axis.text.x = element_text(angle = 60, hjust = 1),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 12))
ggplot 2
ggplot(bx, aes(x = value, color = variable, fill = variable)) +
geom_density(size = 1, linetype="dashed", alpha=0.4) +
theme_classic() +
labs(x="", y="Density", title="", color="") +
theme(axis.title = element_text(size = 15),
axis.text = element_text(size = 15),
axis.text.x = element_text(angle = 60, hjust = 1),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 12))
ggplot3
ggplot() +
geom_density(data = bx[bx$variable=="A",],
mapping = aes(x = value, y = ..density..),
size = 1, alpha=0.4,
color = "#F8766D", fill = "#F8766D") +
geom_density(data = bx[bx$variable=="B",],
mapping = aes(x = value, y = -..density..),
size = 1, alpha=0.4,
color = "#00BFC4", fill = "#00BFC4") +
theme_classic() +
labs(x="", y="Density", title="", color="") +
theme(axis.title = element_text(size = 15),
axis.text = element_text(size = 15),
axis.text.x = element_text(angle = 60, hjust = 1),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 12))