R语言基本图形设置
Posted 一只数据狗的成长之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言基本图形设置相关的知识,希望对你有一定的参考价值。
在图形设计中特别要注意图形参数的设置,一个参数错误会导致脚本无法运行,报错后也不知道如何修改,这个时候是最尴尬的。
#R语言基本数据图形
#当显示为四分之一图形时可以用par(mfrow=c(1,1))实现显示整个图片,在脚本后面加入
barplot(height = 5)
library(vcd)
counts <- table(Arthritis$Improved)#读入Arthritis数据集
counts
barplot(counts,main = "Simple Bar Plot",xlab = "Improvement",ylab = "Frequency"
,horiz = T)#简单条形图,horiz = 表示把条形图转置过来
#绘制堆砌直方图和分组条形图
library(vcd)
counts <- table(Arthritis$Improved, Arthritis$Treatment)#读入Arthritis数据集
counts
barplot(counts,main = "Stacked Bar Plot",
xlab = "Treatment",ylab = "Frequency",
col=c("red","yellow","green"),
legend=rownames(counts),beside = T)#不加beside为堆砌条形图,加上为分组条形图
states <- data.frame(state.region,state.x77)
means <- aggregate(states$Illiteracy,by=list(state.region),FUN=mean)#均值条形图
means
means <- means[order(means$x),]
means
barplot(means$x,names.arg = means$Group.1,col = rainbow(10))
title("Mean Illiteracy Rate")
par(mar=c(5,8,4,2))#增加边界的大小
par(las=2)#旋转条形的标签
counts <- table(Arthritis$Improved)
barplot(counts,
main = "Treatment Outcome",
horiz = T,
cex.names = 0.8,#缩小字体大小
names.arg = c("No Improvement","Some Improvement",
"Marked Improvement"),col = c("yellow","green","red"))#修改标签文本
#棘状图
library(vcd)
attach(Arthritis)
counts <- table(Treatment,Improved)
spine(counts, main = "Spinorgram Examole")#画棘状图
detach(Arthritis)
#饼图
par(mfrow=c(2,2))#图形显示为2乘2区域
slices <- c(10,12,4,16,8)
lbls <- c("US","UK","Australia","Germany","France")
pie(slices,labels = lbls,main = "Simple pie Chart")
pct <- round(slices/sum(slices)*100)
lbls2 <- paste(lbls," ",pct,"%",sep = " ")
pie(slices,labels = lbls2,col = rainbow(length(lbls2)),
main = "pie chart with percengtager")
library(plotrix)
pie3D(slices,labels=lbls,explode=0.1,
main="3D pie chart")
mytable <- table(state.region)
lbls3 <- paste(names(mytable),"\n",mytable,sep = " ")
pie(mytable,labels = lbls3,main = "pie Chart from a Table\n (with sample sizes")
#绘制核密度图
par(mfrow=C(2,1))
d <- density(mtcars$mpg)
plot(d)
d <- density(mtcars$mpg)
plot(d,main="kernel Density of Miles Per Gallon")
polygon(d,col = "red",border = "blue")#下方填充为红色,边线为蓝色
rug(mtcars$mpg,col = "brown")#添加棕色的轴线
#箱线图
boxplot(mtcars$mpg,main="Box plot",ylab="Miles per Gallon")
boxplot(mpg ~ cyl, data = mtcars,
main="car Mileage Data",
xlab = "Number of Cylinders",
ylab = "Miles per Gallon",
col="blue")
#小提琴图
library(vioplot)
par(mfrow=c(1,1))
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","8cyl"),
col="gold")
title("Violin plots of Miles per Gallon",ylab='Miles per Gallon',
xlab="Number of Cylinders")
#点图
dotchart(mtcars$mpg,labels = row.names(mtcars),cex = .8,
main = "Gas Mileage for Car Models",
xlab = "Miles Per Gallon")
#分组,排序,着色后的点图
x <- mtcars[order(mtcars$mpg),]#根据每加仑汽油行驶英里数升序对数据框mtcars进行排序,结果保存为x
x$cyl <- factor(x$cyl)#将数值向量转化为一个因子
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
labels = row.names(x)#各数据点的标签取各自数据框的行名即(车辆型号)
dotchart(x$mpg,
labels = row.names(x),
cex = .9,
groups = x$cyl,
gcolor = "black",#数字4和6,8显示为黑色
color = x$color,#点和标签的颜色来自向量color
pch = 19,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles per Gallon",bg="light blue")
以上是关于R语言基本图形设置的主要内容,如果未能解决你的问题,请参考以下文章