vlambda博客
学习文章列表

R语言|(2)基础绘图


全文共两大部分:基础图+ggplot图



PART1基础图


基础点图
代码
plot(10,10)

x<-10

y<-10

plot(x,y)

x<-c(20,30)

y<-c(40,50)

plot(x,y)

基础线图
代码
备注

x<-c(20,30)

y<-c(40,50)

plot(x,y,'l')

线含点

x<-c(20,30)

y<-c(40,50)

plot(x, y, 'b')

线+点

x<-c(20,30)

y<-c(40,50)


plot(x, y, "c")

线不含点

plot(x, y, "l",main="plot函数绘图演示",xlab="x值",ylab="y值")

plot(x,y,xlim=c(10,50),ylim=c(10,50))

函数:plot(x,y,'type')

基础曲线图(标准正态分布)
代码
备注

x<-pretty(c(-4:4),30)

y<-dnorm(x)

plot(x,y,type="l",xlab="NormalDeviate",ylab="Density",yaxs="i"


函数:pretty(数,分成n等份)

基础直方图
代码
备注

mtcars

hist(mtcars$mpg)

hist(mtcars$mpg, breaks=12, col="red") 

  1. 数据集:mtcars为一个已知数据集,mpg为mtcars其中一个数据集

  2. 间隔:breaks分隔数

  3. 颜色:col表示颜色有面积图的颜色;fill表示线、点类的颜色

x <- mtcars$mpg

h <- hist(x, breaks=10, col="red", xlab="Miles Per Gallon", main="Histogram with Normal Curve")


函数:hist(x,breaks=?,col="颜色",xlab="x标签",main="主标题")

柱状图、条形图
代码
备注

print(h)

xfit <-seq(min(x),max(x),length=40)

yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 

yfit <- yfit*diff(h$mids[1:2])*length(x)

lines(xfit, yfit, col="blue", lwd=2)

  1. h:h为一个数据集

  2. 图:该代码描述的是一个组合图

  3. 线:yfit<-dnorm()和lines是加了一条正态曲线

counts <- table(mtcars$gear)

barplot(counts, main="Car Distribution", xlab="Number of Gears")


counts <- table(mtcars$gear)

barplot(counts, main="Car Distribution", horiz=TRUE, names.arg=c("3 Gears", "4 Gears", "5 Gears"))

  1. 置换:horiz=TRUE

  2. 添加名称:names.arg=c("3 Gears", "4 Gears", "5 Gears")

函数:

1.hist(数据,breaks=间隔)

2.barplot(数据集,自由添加)


饼图
代码
备注

slices <- c(10, 12, 4, 16, 8)

lbls <- c("US", "UK", "Australia", "Germany", "France")

pie(slices, labels=lbls, main="Pie Chart of Countries")

  1. 赋值:占比赋值slices

  2. 标签:lbls

  3. 饼图:pie

slices <- c(10, 12, 4, 16, 8)

lbls <- c("US", "UK", "Australia", "Germany", "France")

pct <- round(slices / sum(slices) * 100)

print(pct)

lbls <- paste(lbls, pct)

lbls <- paste(lbls, "%", sep="")

pie(slices, labels=lbls, col=rainbow(length(lbls)), main="Pie Chart of Countries")

1.添加百分比
函数:pie(占比/份额,自由添加)


直方图
代码
备注

counts <- table(mtcars$vs, mtcars$gear)

barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue", "red"), legend=rownames(counts))

  1.  legend:图例

  2. 堆叠图

counts <- table(mtcars$vs, mtcars$gear)

barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue", "red"), legend=rownames(counts), beside=TRUE)

1.并列图:beside=TRUE

核密度图
代码
备注

d <- density(mtcars$mpg)

print(d)

plot(d, xlab="Miles Per Gallon", main="Kernel Density Plot")

polygon(d, col="red", border="blue")

1.plot只是单纯的画图2.第二步才是填充

点图
代码
备注

dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,main="Gas Milage for Car Models", xlab="Miles Per Gallon")

1.点图

attach(mtcars)

plot(wt, mpg, main="Scatterplot Example", xlab="Car Weight", ylab="Miles Per Gallon", pch=19)

  1. 点图

  2. attach函数attach() 除了可以用目录路径作为参数,也可以使用数据框。

pairs(~mpg+disp+drat+wt, data=mtcars, main="Simple Scatterplot Matrix")

1.散点图矩阵(用于多种数据变量)

library(scatterplot3d)

attach(mtcars)

s3d<-scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE, type="h", main="3D Scatterplot")

fit <- lm(mpg ~ wt+disp)

s3d$plane3d(fit)

1.3D散点图

箱型图
代码
备注

boxplot(mpg~cyl, data=mtcars, main="Car Milage Data", xlab="Number of Cylinders", ylab="Miles Per Gallon")

1.普通箱型图:可以反映占比、最大值、最小值

boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE, col=(c("gold", "darkgreen")), main="Tooth Growth", xlab="Suppliment and Dose")

2.凹形箱线图:好看

小提琴图
代码
备注

library(vioplot)

x1 <- mtcars$mpg[mtcars$cyl==4]

x2 <- mtcars$mpg[mtcars$cyl==6]

x3 <- mtcars$mpg[mtcars$cyl==8]

vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"), col="gold")

title("Violin Plots of Miles Per Gallon")

1.应该是箱型图大类的一种

老师说很好看,颜色为屎黄时,一点都不好看的



PART2    ggplot


无敌ggplot

代码

备注

library(ggplot2)

p <- ggplot(data = mpg, mapping = aes(x = cty, y = hwy))

p + geom_point()

  1. 第一步:提取函数

  2. 第二部:ggplot(data=?,mapping=aes(x=?,y=?,fill=factor(year)))

  3. 第三步:p+geom_point()

  4. 说明:aes中的内容一般都是变量,fill=factor(year)表示颜色根据因子year变化

library(ggplot2)

p <- ggplot(mpg, aes(x = cty, y = hwy, colour = year))

p + geom_point()+stat_smooth()

1.+stat_smooth():添加拟合曲线

library(ggplot2)

p <- ggplot(mpg, aes(x = cty, y = hwy,size =cty,colour = factor(year)))

p + geom_point(aes(x = cty, y = hwy,size =cty,colour = factor(year)))+stat_smooth()

1.size :点的大小变化可以更改

 p + geom_point(aes(colour = class, size = displ), alpha = 0.5) +

  stat_smooth() + scale_size_continuous(range = c(4, 10)) + facet_wrap(~ year, ncol = 1)

  1. scale_size_continuous(range = c(4, 10)),也就是指定我们size的变化范围

  2. facet_wrap(~ year, ncol = 1)将图按year分层,ncol = 1默认分层两列



library(ggplot2)

p <- ggplot(mpg, aes(x = cty, y = hwy,size =cty,colour = factor(year)))

p + geom_point(aes(colour = factor(year), size = cty), alpha = 0.5) +stat_smooth() + scale_size_continuous(range = c(4, 10))

1.透明度:alpha = 0.5,变化范围在0-1之间,数值越大,越不透明。2.注意alpha = 0.5放的位置应该在geom_point()中
+geom_histogram() 直方图
+geom_bar() 条形图

+theme()更偏向于格式的修改

需要图示中删除内容就可以使用

plot.title = element_text(hjust = 0.5)是调整标题的位置

不加这行,标题会居左,加上才会居中。hjust = 0.5其实就是左右移动的意思,0.5表示居中

R语言|(2)基础绘图



This browser does not support music or audio playback. Please play it in WeChat or another browser.