美文网首页
R语言 -- ggplot2 学习

R语言 -- ggplot2 学习

作者: 生信摆渡 | 来源:发表于2020-09-11 17:58 被阅读0次

    菜鸡又来学画图了,菜鸡总共整理了大概一天半

    这些当然只是冰山一角,以后遇到其他问题会继续补充,奥利GAY!

    原始图:



    美化后(自认为):


    library("ggplot2")
    # 随便挑个数据集
    data("iris")
    str(iris)
    
    # 基础版
    aa = ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()
    
    
    # 手动再加个类型
    iris$class = rep(c("A","B","C"), 50)
    ab = ggplot(iris, aes(x = Species, y = Sepal.Length, color = class))
    ab + geom_boxplot()
    
    # 设置box的宽度和去除离群点
    ac = ab + stat_boxplot(geom = "errorbar", width=0.6)
    ac2 = ac + geom_boxplot(width = 0.6, outlier.shape=NA)
    
    # 设置y轴范围,去除坐标轴边缘多余的线
    ad = ac2 + scale_y_continuous(expand = c(0, 0), 
                                 limits = c(4, 8),
                                 breaks = seq(4, 8, 0.5))
                    
    ae = ad + theme_bw()
    # 设置经典的主题;添加主标题和坐标轴标题
    af = ae + labs(x = "This is x-title", 
                 y = "This is y-title", 
                 title = "This is title",
                 subtitle = "This is subtitle")
    
    af2 = af + theme(plot.title = element_text(size = 25,
                                               color = "#E90090",
                                               face = "bold",
                                               hjust = 0.5),
                     plot.subtitle = element_text(size = 15, 
                                                  color = "#00B6E9",
                                                  face = "italic",
                                                  hjust = 0.5))
    
    # 设置坐标轴标题颜色、大小、加粗、线体
    ag = af2 + theme(axis.title.x = element_text(color = "black", 
                                                 size = 15, 
                                                 face = "bold",
                                                 vjust = -5),
                    axis.title.y = element_text(color = "black", 
                                                size = 15, 
                                                face = "bold"))
    
    # 调整画布大小
    ag2 = ag + theme(plot.margin=unit(rep(0.2, 4), "inch"))
    
    # 设置坐标轴标签倾斜方向,大小、颜色
    ah <-  ag2 + theme(axis.text.x = element_text(angle = 10, 
                                                 hjust = 1, 
                                                 colour = "#3D3D3D",
                                                 size = 15),
                      axis.text.y = element_text(angle = 30, 
                                                 hjust = 1, 
                                                 colour = "#3D3D3D",
                                                 size = 15))
    # # 清除网格
    ai = ah + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(),
                     panel.background = element_blank(),
                     # axis.line = element_line(colour = "black"),
                     panel.border = element_rect(colour = "black", fill=NA))
    
    # 隐藏图例
    ai +  theme(legend.position = "none")
    
    # 修改图例标题内容、图例标签名称
    
    # 自定义fill的颜色
    ak = ai + scale_color_manual(values=c("#E90B91", "#E88C00", "#231FEC"), 
                           name="My legend",
                           breaks=c("C", "B", "A"),
                           labels=c("Key_C", "Key_B", "Key_A"))
    # 修改图例标题样式
    al = ak + theme(legend.title=element_text(size=15, 
                                         colour = "#8100A9", 
                                         angle = 45,
                                         hjust = 0.4,
                                         vjust = 0.4))
    
    # 隐藏图例标题
    al2 = al + theme(legend.title=element_blank())
    
    # 修改图例标签样式
    am = al2 + theme(legend.text = element_text(colour = "#F7772E",
                                     angle = 45,
                                     size=13,
                                     # hjust = -1.5,
                                     vjust = 0.4,
                                     face="bold"))
    # 设置legend边框
    am + theme(legend.background = 
                   element_rect(
                       fill = NA, 
                       size = 0.5, 
                       linetype = "dotted",
                       color = "black"
                    )
    )
    
    # 去除key背景
    am2 = am + theme(legend.key = 
                         element_rect(
                             fill = NA,
                             size = 30
                         )
    )
    
    # 修改图例key大小
    an = am2 + theme(legend.key.width=unit(0.4,"inches"),
               legend.key.height=unit(0.4,"inches"))
    
    # 修改图例方向、位置
    ao = an + theme(legend.direction = "horizontal",
                    legend.position = c(0.27, 0.8))
    
    # 添加抖动的散点图,但是如何让散点图分布在对应箱线图内呢?
    ap = ao + geom_jitter(
        aes(color = class), 
        position = position_jitter(w = 0.1, h = 0.05), 
        size = 1, 
        alpha = 0.4
        )
    
    # 调整X轴标题位置
    aq = ao + theme(axis.title.x = element_text(vjust = -2))
    
    
    
    
    • 添加主次ticks
    • 如何解决文字溢出问题

    相关文章

      网友评论

          本文标题:R语言 -- ggplot2 学习

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