zoukankan      html  css  js  c++  java
  • R语言与医学统计图形-【32】海盗图、词云图、日历图

    1.海盗图

    参数众多,其语法与基础包类似。

    基础图。

    #devtools::install_github('ndphillips/yarrr')
    #install.packages('yarrr')
    library(yarrr)
    
    #基本海盗图
    str(pirates)
    pirateplot(formula = age ~ favorite.pirate,
               data = pirates,
               xlab = 'Favorite Pirate',
               ylab = 'Age',
               main="")
    

    image.png
    散点图展示年龄分布,盒形图展示平均年龄,beans展示年龄大致分布,越胖越集中。

    不同主题的海盗图。

    #theme
    par(mfrow=c(2,2))
    for(i in 1:4){
      pirateplot(formula = age~favorite.pirate,
                 data=pirates,
                 xlab = 'Favorite Pirate',
                 ylab = 'Age',
                 gl.lwd = 0, #不显示背景网格线
                 bty = 'l', #边框类型
                 pal = 'xmen', #调色板piratepal函数种的xmen色系
                 avg.line.lwd = 0.5, #均值线宽
                 main = paste('Theme is:',i),
                 theme = i)
    }
    

    image.png
    设置透明度。

    #设置透明度
    pirateplot(formula = age~favorite.pirate,
               data=pirates,
               xlab = 'Favorite Pirate',
               ylab = 'Age',
               gl.lwd = 0,
               bty = 'l',
               pal = rainbow(6),
               avg.line.lwd = 1.2,
               point.col = rainbow(6),
               point.o = 0.5, #点透明度
               inf.f.o = 0.8, #盒子透明度
               bar.f.o = 0.2, #添加透明度bar图
               bean.f.o = 0.4) #bean条带透明度
    

    image.png
    自定义坐标轴。

    #自定义坐标轴
    pirateplot(formula = age~favorite.pirate,
               data=pirates,
               xlab = 'Favorite Pirate',
               ylab = 'Age',
               gl.lwd = 0,
               bty = 'n',
               ylim = c(-10,50), #注意范围要能容下x轴标签
               pal = rainbow(6),
               avg.line.lwd = 1.2,
               point.col = 0.8,
               bar.f.o = 0.2,
               bean.f.o = 0.4,
               xaxt = 'none',
               yaxt = 'none') #不绘制坐标轴
    axis(2,at=seq(0,50,5))
    pirate <- unique(pirates$favorite.pirate)
    text(1:6,-5,labels = sort(pirate),srt=45)
    

    image.png

    2.词云图

    #install.packages('wordcloud2')
    library(wordcloud2)
    
    wordcloud2(demoFreq,size = 1.6)
    wordcloud2(demoFreq,size = 1.6,
               color = 'random-light', #词云颜色
               backgroundColor = 'black')#背景色
    
    #形状
    wordcloud2(demoFreq,size = 0.7,
               shape = 'star') #形状
    
    #中文词云
    wordcloud2(demoFreqC,size = 2,
               fontFamily = 'STKaiti',
               minRotation = -pi/6,
               maxRotation = -pi/6,
               rotateRatio = 1) #旋转比例
    
    
    #以单词样式展示
    letterCloud(demoFreq,
                word = 'hello',
                color='random-light',
                backgroundColor='grey')
    

    image.png

    3.日历图

    可展示随时间的变化。

    如一年中每一天的大气污染物数据。

    #install.packages('openair')
    library(openair)
    
    #ts函数生成时间序列数据(不包含对应时间)
    value <- ts(data = sample(0:300,366,replace = T),
                start = as.Date('2016-01-01'),
                frequency = 1,
                end = as.Date('2016-12-31')
                )
    #seq函数生成时间,与value对应
    date <- seq(from=as.Date('2016-01-01'),
                by=1,
                length.out = 366)
    pm25 <- data.frame(pm25=value,date=date)
    
    calendarPlot(pm25,pollutant = 'pm25',year = 2016)
    

    image.png

    只展示前3个月的数据。

    calendarPlot(selectByDate(pm25,month = c(1,2,3),year = 2016), #取子集
                 pollutant = "pm25",year = 2016)
    

    image.png

    从上看出计算机默认语言为中文,所以展示的日历也是中文,星期都显示不全,要解决这个问题只需:
    Sys.setlocale("LC_TIME", "English")即可。
    image.png

  • 相关阅读:
    9. 远程分支与本地分支管理
    8. Git 远程协作
    7. Git stash命令
    6. Git版本处理
    5. Git 本地分支命令
    4. Git 日志命令
    JVM垃圾回收分析
    python常用模块
    ubuntu18配置jetty9
    logback spring配置
  • 原文地址:https://www.cnblogs.com/jessepeng/p/12354308.html
Copyright © 2011-2022 走看看