今天没有大项目,介绍几个ggplot2绘图过程中不常用到的有用函数,喜欢的小伙伴欢迎关注个人公众号R语言数据分析指南,我在此现行拜谢了
library(tidyverse)
library(patchwork)
移除分面标签
p1 <- ggplot(mtcars, aes(hp, mpg)) +
geom_point() +xlab(NULL)+ylab(NULL)+
facet_grid(~ vs) +
theme_bw()
p2 <- ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
facet_grid(~cyl ) +xlab(NULL)+ylab(NULL)+
theme_bw()
p3 <- ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
facet_grid(~cyl) +xlab(NULL)+ylab(NULL)+
theme_bw()+theme(strip.text.x = element_blank(),
axis.ticks.length.y = unit(.1, "cm"),
axis.ticks.length.x = unit(-.1, "cm"),
axis.text.x = element_text(margin = margin(t = .2, unit = "cm")))
p1/p2/p3
- strip.text.x 移除分面标签
- axis.ticks.length.x 设置刻度线方向
热图图例修改
pacman::p_load(tidyverse,reshape2,aplot,ggtree)
pal <- wes_palette("Zissou1", 100, type = "continuous")
heatmap <- mtcars %>% scale(center = F) %>%
as.data.frame() %>%
mutate(mtxars=row.names(.)) %>% melt() %>%
ggplot(aes(variable,mtxars,fill=value))+
geom_tile()+
theme_minimal()+
scale_fill_gradientn(colours = pal) +
scale_y_discrete(position="right")+
xlab(NULL) + ylab(NULL)+
theme(axis.text.x =element_text(angle =90,
hjust =1,vjust = 0.5))+
guides(fill = guide_colorbar(direction = "vertical",reverse = F,
barwidth = unit(.5, "cm"),barheight = unit(16, "cm")))+
labs(fill="")
heatmap
- guides()调整图例宽度与高度
将刻度条调整到与图一样的高度顿时感觉整张图都完美了
网友评论