之前看到师妹画的一张图很好看,是等高线图和密度图的组合。
今天自己模仿了一下,幸得师妹提名:云朵图。
不同分组的点用类似于等高线图的形式呈现,点越密颜色越深。
上侧和右侧为点的密度分布图。
对于NMDS,PCA等二维数据可使用这种方式呈现。
目前看着还比较丑,美化的工作就交给读者了。。。
# 数据
data <- bind_rows(
tibble(x = rnorm(1000, mean = 10,sd = 6),
y = rnorm(1000, mean = 12,sd = 3),group = "a"),
tibble(x = rnorm(1000, mean = 20, sd = 8),
y = rnorm(1000, mean = 25, sd = 2),group = "b"),
tibble(x = rnorm(1000, mean = 30, sd = 7),
y = rnorm(1000, mean = 33, sd = 2),group = "c"))
# 画图
library(ggplot2)
library(aplot)
p1 <- ggplot(data, aes(x = x, y = y,fill=group)) +
stat_density_2d(aes(group = ..group..), geom = "polygon",
colour="burlywood",size = 1,alpha=.7,linetype=2)+
theme_classic()+guides(fill=FALSE)
p2 <- ggplot(data,aes(x = x,fill=group))+
geom_density(alpha=0.5)+
scale_y_continuous(expand = c(0,0))+
theme_minimal()+labs(y=NULL)+
theme(axis.ticks = element_blank(),
panel.background=element_blank(),
panel.grid=element_blank())
p3 <- ggplot(data,aes(y = y,fill=group))+
geom_density(alpha=0.5)+
scale_y_continuous(expand = c(0,0))+
theme_minimal()+ labs(y=NULL)+
theme(panel.background=element_blank(),
panel.grid=element_blank())
p1 %>% insert_top(p2,height = 0.5)%>%
insert_right(p3,width = 0.5)