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") |
|
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) |
|
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.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") |
|
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)) |
|
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) |
|
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
代码 |
备注 |
library(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) p + geom_point() |
|
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) |
|
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表示居中 |