美文网首页
ggplot图形布局

ggplot图形布局

作者: 曹草BioInfo | 来源:发表于2022-04-09 14:57 被阅读0次
    library(tidyverse)
    library(ggsci)
    

    分面

    mtcar_tbl = rownames_to_column(mtcars, var = 'car')%>%
      mutate(cyl = factor(cyl),
             am = if_else(am == 1, 'A', 'N'),
             vs = if_else(vs == 1, 'V', 'L'))
    

    facet_grid 双变量分面,把am和vs信息也放进图里

    ggplot(mtcar_tbl,aes(x= wt,y = mpg))+
      geom_point(shape = 21,
                 alpha = 0.5,
                 aes(size = disp, fill = factor(cyl)))+
      scale_fill_npg()+
      scale_size(range = c(1,20))+
      facet_grid(vs~am, scales = 'free_x') +#允许自由适应x坐标
      theme_bw()
    

    仅需要通过单变量分面,但面数又太多了的时候:

    small_diamonds = sample_n(diamonds, 500)
    ggplot(data = small_diamonds,aes(x = carat, y =price))+
      geom_point(shape = 21,size = 2,
                 color = 'black', aes(fill = cut))+
      scale_fill_npg()+
      facet_wrap(~color, nrow = 2)+
      theme_bw()+
      theme(legend.position = c(0.9,0.3))
    

    来自不同变量的分面

    library(ggforce)
    
    ggplot(data = mtcar_tbl, aes(x = .panel_x, y = .panel_y))+
      geom_point(shape = 21,
                 aes(fill = factor(cyl)))+
      scale_fill_npg()+
      facet_matrix(rows = vars(disp,wt),cols = vars(mpg,qsec))+
      theme_bw()
    

    局部放大

    library(ggforce)
    
    ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width,
                            color = Species))+
      geom_point(size = 3)+
      scale_fill_npg()+
      facet_zoom(x= Species == 'versicolor',##设置需要放大的条件
                 zoom.size = 1)+#调整放大倍数
      theme_test()
    

    加标记

    ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width,
                            color = Species))+
      geom_point(size = 3)+
      geom_mark_ellipse(aes(label = Species))+##加标记和椭圆
      scale_fill_lancet()+
      theme_test()+
      theme(legend.position = 'none')#去掉旁边的标注
    

    添加子表/图

    iris_stat = group_by(iris,Species)%>%
      summarise(Petal.Length = mean(Petal.Length),
                Petal.Width = mean(Petal.Width))
    
    p0 = ggplot(data = iris, aes(x = Species, y = Sepal.Length))+
      geom_boxplot(aes(fill = Species), outlier.shape = 21)+
      scale_fill_lancet()+
      theme_classic()+
      theme(legend.position = 'none',
            axis.title.x = element_blank(),
            axis.text.x = element_blank(),
            axis.ticks.x = element_blank(),
            plot.background = element_blank())
    

    位置表

    tbl_pos = tibble(x= 7, y = 0.2, tb = list(iris_stat))
    plot_pos = tibble(x = 7, y = 0, plot = list(p0))
    
    library(ggpmisc)
    ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width,
                            color = Species))+
      geom_point(size = 3)+
      geom_table(data = tbl_pos,aes(x = x, y=y ,label = tb))+
      scale_fill_npg()+
      theme_test()+
      theme(legend.position = c(0.1,0.8))
    

    添加子图

    ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width,
                            color = Species))+
      geom_point(size = 3)+
      geom_plot(data = plot_pos,aes(x = x, y=y ,label = plot))+
      scale_fill_npg()+
      theme_test()+
      theme(legend.position = c(0.1,0.8)) 
    

    拼图

    library(patchwork)
    p1 = ggplot(mtcars)+
      geom_point(aes(mpg,disp,color = factor(cyl))) +
      ggtitle( 'Plot 1')
    
    p2 = ggplot(mtcars)+
      geom_boxplot(aes(gear,disp,group = gear, fill = factor(gear))) +
      ggtitle( 'Plot 2')
    
    p3 = ggplot(mtcars)+
      geom_point(aes(hp,wt,color = factor(cyl))) +
      ggtitle('Plot 3')
    
    p4 = ggplot(mtcars)+
      geom_bar(aes(gear)) +
      facet_wrap(~cyl)+
      ggtitle( 'Plot 4')
    
    p1+p2
    p1/p2
    p1/((p2/p3)|p4)
    

    p1在最上层,其他的在下层。其中p2和p3也存在分层,但p4和这个整体并列

    p1/(p2+p3)/p4 + plot_layout(heights = c(1,2,1),widths = c(2,1))+#设置图形的宽高比例
      plot_annotation(title = 'The plots',
                      tag_levels = 'A')&#可以直接指定所有图的标签
      theme_bw()
    

    前面用&才可以修改所有图的theme

    图例收集

    可以通过修改guide_area()的位置来改图例的位置

    p1+p3+guide_area()+plot_layout(guides = 'collect')
    

    相关文章

      网友评论

          本文标题:ggplot图形布局

          本文链接:https://www.haomeiwen.com/subject/ugypsrtx.html