Article directory
1. Use R's basic drawing system to draw pictures
There are two types of functions in the basic drawing system: one is high-level drawing functions (functions that directly generate graphics); the other is low-level drawing functions (adding new graphics or elements based on graphics)
1. Function plot()
#准备数据
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose,drugA)
plot(dose,drugA,type = 'b') #默认type=p
lty lines of different types, pch points of different characteristics.
plot(dose,drugA,type='b',xlab = "Dosage",ylab = "Response",lty=1,pch=15)
lines(dose,drugB,type='b',lty=2,pch=17)
legend("topleft",title = "Drug Type",legend=c("A","B"),lty=c(1,2),pch = c(15,17))
2. Histogram and Density Curve
Introducing data from the MASS package on weight change in young women with anorexia
> library(MASS)
> data(anorexia)
> str(anorexia)
'data.frame': 72 obs. of 3 variables:
$ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
$ Prewt : num 80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
$ Postwt: num 80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
hist(Prewt)#直方图
plot(density(Prewt))#密度图
Parameter col: set the color of the shape;
parameter las: the value is 1 to set the horizontal representation of the ordinate.
hist(Prewt,freq = FALSE,xlab = "体重(lbs)",col='red',main = "治疗前体重直方图",las=1)
lines(density(Prewt),col='blue',lwd=2)
rug(Prewt)
3. Bar chart
Take, for example, a data set from a trial of a new treatment for rheumatoid arthritis:
First, look at the data. table() is used to generate a frequency statistics table of categorical variables.
> library(vcd)
> data(Arthritis)
> attach(Arthritis)
> counts<-table(Improved)
> counts
Improved
None Some Marked
42 14 28
In addition to displaying histograms, barplot() can also display two-dimensional contingency table data.
barplot(counts,xlab="Improvement",ylab = "Frequency",las=1)
counts<-table(Improved,Treatment)
barplot(counts,xlab="Improvement",ylab = "Frequency",col=c("red","yellow","green"),las=1,beside=TRUE)
legend("top",legend=rownames(counts),fill=c("red","yellow","green"))
4. Pie chart
**Pie chart:** Drawn using pie().
paste0(): Strings can be concatenated according to the third parameter.
percent <- c(5.8, 27.0, 0.5, 20.8, 12.8, 33.1)
disease <- c("上感","中风","外伤","昏厥","食物中毒","其他")
labs<-paste0(disease,percent,"%")
pie(percent,labels = labs,col = rainbow(6))
5. Box plots and violin plots
**Box plot:** drawn using boxplot().
library(MASS)
data(anorexia)
anorexia$wt.change<-anorexia$Postwt-anorexia$Prewt
boxplot(wt.change~Treat,data=anorexia,ylab="Weight change(lbs)",las=1)
**Violin plot:** Drawn using vioplot().
library(vioplot)
vioplot(wt.change~Treat,data=anorexia,ylab="Weight change(lbs)",las=1,col="gold")
6. Cleveland Dot Plot
**Cleveland dot plot:** Drawn using dotchart().
dotchart(VADeaths)
dotchart(t(VADeaths),pch = 19)
2. Use the ggplot2 package for drawing
1. Getting to know the ggplot2 package
The ggplot2 package provides a drawing system based on layer syntax, and the principles of various data visualizations are completely consistent. Take the car fuel dataset mtcars as an example.
library(ggplot2)
p<-ggplot(data=mtcars,mapping=aes(x=wt,y=mpg))
p+geom_point()
The variable am (transmission mode) in the data set is a categorical variable 0/1
mtcars$am<-factor(mtcars$am)
ggplot(data=mtcars,aes(x=wt,y=mpg,color=am))+geom_point()
ggplot(data=mtcars,aes(x=wt,y=mpg,color=am))+geom_smooth()
ggplot(data=mtcars,aes(x=wt,y=mpg))+geom_point(aes(color=am))+scale_color_manual(values = c("blue","red"))+stat_smooth()
ggplot2 can also group plots.
ggplot(data=mtcars,aes(x=wt,y=mpg))+geom_point()+stat_smooth()+facet_grid(~am)
The ggplot2 package can also theme plots to a dark theme.
ggplot(data=mtcars,aes(x=wt,y=mpg))+geom_point(aes(color=am))+stat_smooth()+theme_dark()
2. Characteristics of the distribution
The distribution features are plotted as a histogram and a line graph with the ggplot2 package, respectively
ggplot(anorexia,aes(x=wt.change,y=..density..))+geom_histogram(binwidth = 2,fill="skyblue",color="black")+labs(x="Weight change(lbs)")+stat_density(geom="line",linetype="dashed",size=1)
ggplot(anorexia,aes(x=wt.change,color=Treat,linetype=Treat))+labs(x="Weight change(lbs)")+stat_density(geom="line",size=1)
**Box plot:** drawn using geom_boxplot().
**ggpubr package:** Provides statistical difference functions for adding component comparisons on parallel boxplots.
ggplot(anorexia,aes(x=Treat,y=wt.change),fill=Treat)+geom_boxplot()+theme_bw()
library(ggpubr)
mycomparisons<-list(c("CBT","Cont"), c("CBT", "FT"), c("Cont", "FT"))
ggplot(anorexia,aes(x=Treat,y=wt.change),fill=Treat)+geom_boxplot()+stat_compare_means(comparisons=mycomparisons,method="t.test",color="blue")+theme_bw()
Violin diagram
# position:字符串形式的位置参数,确定位置(也可以是返回位置参数的函数)
ggplot(anorexia,aes(x=Treat,y=wt.change),fill=Treat)+geom_violin()+geom_point(position=position_jitter(0.1),alpha=0.5)+theme_bw()
3. Composition of proportions
The scale is overlaid onto the bar chart as follows:
#scale_fill_brewer()一般用于箱线图和条形图等需要填充的图
#1.计数
ggplot(Arthritis,aes(x=Treatment,fill=Improved))+
geom_bar(color="red")+
scale_fill_brewer()+
theme_bw()
#2.比例
ggplot(Arthritis,aes(x=Treatment,fill=Improved))+
geom_bar(color="red",position = "fill")+
scale_fill_brewer()+
theme_bw()
You can also place bar charts side by side
ggplot(Arthritis,aes(x=Treatment,fill=Improved))+
geom_bar(color="red",position = "dodge")+
scale_fill_brewer()+
theme_bw()
4.ggsave() save graphics
p<-ggplot(Arthritis,aes(x=Treatment,fill=Improved))+
geom_bar(color="red",position = "dodge")+
scale_fill_brewer()+
theme_bw()
ggsave("myplot.png",p)
ggsave("myplot.tiff", width = 15, height = 12, units = "cm", dpi = 500)
3. Other graphics
1. Pyramid Chart
Pyramid chart : drawn using pyramid(), it is a back-to-back bar chart
library(epiDisplay)
data(Oswego)
pyramid(Oswego$age,Oswego$sex,col.gender = c(2,4),bar.label = TRUE)
2. Horizontal Stacked Bar Chart
library(sjPlot)
data(efc)
view_df(efc) #查看数据集信息
library(dplyr)
qdata<-dplyr::select(efc,c82cop1:c90cop9)
plot_stackfrq(qdata)
3. Heatmap
> data(mtcars)
> dat<-scale(mtcars)#进行数据的中心化和标准化
> class(dat)
[1] "matrix" "array"
> heatmap(dat)
4. Three-dimensional scatter plot
Three-dimensional scatter plot : use scatterplot3d() to draw. The parameter type is used to draw the graph, the default is "p" (point), here it is set to "h" (vertical line segment). The parameter angle is the angle between the x-axis and the y-axis.
library(scatterplot3d)
data(trees)
scatterplot3d(trees,type="h",highlight.3d = T,angle=55,pch=16)
If you want to dynamically adjust 3D graphics, you can use plot3d() to draw.
library(rgl)
plot3d(trees)
5. Word Cloud Map
> library(wordcloud2)
> head(demoFreqC)
V2 V1
1 数据 2304
3 统计 1413
4 用户 855
5 模型 846
7 分析 773
8 数据分析 750
> wordcloud2(demoFreqC)
The main input of wordcloud2 is the word frequency statistics table.
Summarize
Tip: Here is a summary of the article:
the above is what I will talk about today. This article only briefly introduces the usage of R language to draw various basic graphics.