0%

R学习笔记 作图Tips

记录之前一段时候作图的一些基础用法以及小技巧,但不局限于ggplot2(但我用的最多的还是ggplot2),持续更新(如果没忘记的话)。。。 1. 如果想减少axis和plot之间的空隙距离,可以使用scale_x_continuous or scale_y_continuous函数,并在函数中的参数expand设置为c(0,0),如:

    scale_x_continuous expand = c(0, 0))  ###连续性
    scale_y_continuous(expand = c(0, 0))
    scale_x_discrete(expand = c(0,0))     ###离散型
    scale_y_discrete(expand = c(0,0))
    

**expand**:A numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes. The defaults are c(0.05, 0) for continuous variables, and c(0, 0.6) for discrete variables.(简单的说,就是设置以倍数来设置距离)
  1. 修改图例的title,比如增加\n;如:

    scale_fill_discrete(name = "Fancy Title\n")
    scale_fill_gradient(name = "aaa\n", low = "red", high = "gold2") 

    还可以用guide()函数来修改legend,比如title position, title text style, label position...
    具体参数可以查看:http://ggplot2.tidyverse.org/reference/guide_legend.html

  2. 对于gglot2主题theme()的常规设置http://ggplot2.tidyverse.org/reference/theme.html

    3.1 对于网格、外边框:

    panel.border = element_blank()
    panel.grid.minor = element_blank()
    panel.grid.major = element_blank()

    3.2 当没有外边框后,可以选择自行添加x轴和y轴:

    axis.line.x = element_line(size = 0.5)
    axis.line.y = element_line(size = 0.5)

    3.3 设置主标题并居中:

    labs(title = "Title")
    plot.title = element_text(hjust = 0.5)

    3.4 设置x or y轴的标签:

    axis.text.x = element_text(size = 10, face = "italic", vjust = 1,hjust = 1,angle = 75)

    3.5 设置x or y轴的标题:

    axis.title.x=element_text(size = 14,margin = margin(c(15,0,0,0)))

    3.6 设置图形与画布之间的距离:

    plot.margin = unit(c(4,2,1.5,1),"lines")
  3. 对于legend图例的常规设置,详细的可以查看http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

    4.1 首先对于连续型数据,直接在gglot()映射即可,而对于离散型数据,需要先将离散数据转化为因子,然后在映射;当然也可以不在ggplot()里映射,在geom_*()可以映射

    #连续型,这里的fpkm为连续型
    p <- ggplot(Enrich, aes(x= Map.Name, y = Test, fill = p.value))
    
    #离散型,这里的sample为离散型
    p <- ggplot(data = data, aes(x = sample, y = fpkm)) +
        geom_boxplot(aes(fill = sample))

    4.2 在theme()函数中设置legend

    #设置legend的位置:       
    legend.position = "right"   #(默认是右边)
    
    #细调legend位置:
    legend.margin = margin(c(0,0,0,15))
    
    #设置legend高度、宽度
    legend.key.height = unit(0.6,"cm")
    legend.key.width = unit(0.5,"cm")
    
    #设置legend title、legend text的格式
    legend.title = element_text(size = 11))
    legend.text = element_text(size = 8)

    4.3 也可以用guides()函数来设置legend,具体见http://ggplot2.tidyverse.org/reference/guide_legend.html

    guides(fill = guide_legend(label.position = "left", label.hjust = 1)) 

    4.3 设置legend的颜色

    #对于连续型数据        
    scale_fill_gradient(limits = c(0,0.05),low = "red", high = "gold2")
    scale_fill_gradient2(name = "R2",low = "white", high = "deepskyblue4", mid = "white",
        midpoint = 0.4, limits = c(0.4,1.05),breaks = c(0.4,0.6,0.8,1.0))
    
    #对于离散型数据
    scale_fill_manual(values = c("orangered","palegreen","turquoise","orchid"))
    scale_color_manual(name = "DEG", values = c("blue", "black", "red"))

    4.4 关于legend的一个小技巧,在我们用scale_fill_gradient2()等函数做legend时,会发现最顶上的text值会跟title值很接近,这是通过设置scale_fill_gradient2()里的limits的最大值来修改text的位置

    #比如legend范围是c(0,1),你可以设置为c(0,1.1)
    从而使得1这个点在legend上的的位置下移了,从而解决了上述的问题
  4. 给图形添加文字or数字,比如柱子上、热图内等等

    #柱形图上
    geom_text(aes(label = Enrich$richFactor, vjust = 0.4, hjust = -0.3), size=3)
    
    #热图内
    geom_text(aes(X2,X1,label=value),color = "black") ##这里的X2,X1分别对应点的y轴坐标和x轴坐标
  5. 对图形翻转

    coord_flip()
  6. 对图形分面,并设定分面的一些的参数

    facet_grid(.~ Term_type, scales = "free", space = "free")
    strip.background = element_rect(fill = "white")   ##背景颜色
    strip.placement = "outside"                       ##分面的那个框在坐标轴(比如Y轴)的范围内还是外
    strip.text = element_blank()                      ##取消分面的那个框
  7. ggplot2 2.2.0版本允许双坐标作图了https://blog.rstudio.org/2016/09/30/ggplot2-2-2-0-coming-soon/

    scale_y_continuous(limits = c(0,max(data$Seqs)*1.2),
        sec.axis = sec_axis(~./810*100, name = "Percentage of Proteins (%)"),expand = c(0,0))
    #即使用sec.axis参数即可
  8. 做拟合曲线的回归方程

    model <- lm(A ~ B, data)
    r2 <- summary(model)$r.squared
    r2 <- paste0("r^2=", r2)
    
    p <- ggplot(data, aes(x = A, y = B)) +
        geom_point(colour = "grey60", size = 1)+
        stat_smooth(method = lm, se = FALSE) +
        annotate("text", label = r2, x = 3.5, y = 1)
  9. 使坐标轴的标签按照输入的(data.frame)顺序排列,可通过设置因子来实现

    data\(Type <- factor(data\)Type,levels = unique(data$Type))

  10. 表达式标题

    labs(x = expression(log2))

  11. 将X轴放置在顶部

    scale_x_continuous(expand = c(0, 0), position = "top")
  12. 修改单个图例的名称以及颜色

    scale_fill_manual(values = "#FF6A6A", name = "", breaks = c("P"), labels = c("upregulated proteins"))
  13. 将文字放在柱状图的内部,并顶格与坐标轴

    # To place text in the middle of each bar in a stacked barplot, you
    # need to set the vjust parameter of position_stack()
    geom_text(aes(label = enrich_up$Term, hjust = 1), size=3, position = position_stack(vjust = 0), hjust = "inward")

本文出自于http://www.bioinfo-scrounger.com转载请注明出处