美文网首页此地古同R语言
R小姐:Wilkinson点图“白头”Density_2d等高线

R小姐:Wilkinson点图“白头”Density_2d等高线

作者: 鲨瓜 | 来源:发表于2018-11-24 16:20 被阅读2次

    观此一文,乃知绘图章节大势已去,浩浩荡荡向你涌来,缠缠绵绵弃你而去,人世间最开心的事莫过于此,难得落个一生快活儿。

    今日一文,Wilkinson点图非为要塞,Density_2d等高线当派重兵把守,预防饮毛茹血之鞑虏小人乘机偷袭。

    他日将排兵布阵,汇总绘图章节之梗概,献于各位英雄豪杰,小家碧玉。

    1

    入门

    library(ggplot2)
    library(gcookbook)
    library(tidyverse)
    
    #入门
    #利用 filter 提取Year等于2009且healthexp大于2000的数据
    countries2009 <- filter(countries,Year==2009 & healthexp>2000)
    #传入数据
    ggplot(countries2009,aes(x=infmortality)) +
      #绘制Wilkinson点图
      geom_dotplot()
    
    image

    2

    进阶

    #进阶
    #传入数据
    ggplot(countries2009,aes(x=infmortality)) +
      #设置堆叠类型,分组宽度
      geom_dotplot(stackdir = 'center',binwidth = 0.25,
                   #设置填充色,和边框颜色
                   fill='cornsilk',colour='lightblue') + 
      #设置辅助刻度线及颜色
      geom_rug(colour='red') +
      #设置Y轴标题
      ylab(label = 'Count') +
      #设置X轴标题
      xlab(label = 'Infmortality')+
      #设置背景主题
      theme_bw() +
      #删除所有网格线
      theme(panel.grid = element_blank(),
            #设置Y轴标签的角度并调整位置
            axis.text.y = element_text(angle = 60,hjust = 1,vjust = -.5),
            #设置X、Y轴的标题颜色
            axis.title = element_text(colour = 'blue'))
    
    image
    #如何将箱图与点图分开?
    #传入数据
    ggplot(heightweight,aes(x=sex,y=heightIn)) +
      #将sex因子转换为数值,一定要表明group,绘制箱图
      geom_boxplot(aes(x=as.numeric(sex) + 0.2,group=sex),width=.25,
                   #设置箱图填充色,边框色
                   fill='cornsilk',colour='grey60')+
      #将sex因子转换为数值,一定要表明group,绘制点图
      geom_dotplot(aes(x=as.numeric(sex) - 0.2,group=sex),
                   #以Y轴堆叠,宽度,类型
                   binaxis = 'y',binwidth = .5,stackdir = 'center',
                   #设置填充色
                   fill='red') +
      #确定X轴的取值范围
      scale_x_continuous(breaks = 1:nlevels(heightweight$sex),
                         #传入X轴标签
                         labels = levels(heightweight$sex))
    
    image

    3

    精通

    #精通
    #传入数据
    ggplot(faithful,aes(x=eruptions,y=waiting)) +
      #绘制点图和二维等高线
      geom_point() + stat_density2d()
    
    image
    ggplot(faithful,aes(x=eruptions,y=waiting)) +
      geom_point() +
      #以颜色区分密度
      stat_density_2d(aes(fill=..density..),geom = 'raster',contour = FALSE,h=c(0.5,5))
    
    image
    ggplot(faithful,aes(x=eruptions,y=waiting)) +
      geom_point() +
      #以灰度区分密度
      stat_density2d(aes(alpha=..density..),geom = 'tile',contour = FALSE)
    
    image

    登山才知胸怀豪情壮志,入海方晓知识无垠无边。咱们一起登山入海可好?

    下期再见。

    你可能还想看

    等你很久啦,长按加入古同社区

    image

    相关文章

      网友评论

        本文标题:R小姐:Wilkinson点图“白头”Density_2d等高线

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