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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM