美文网首页R语言
[R语言]Chapter 6 基本图形

[R语言]Chapter 6 基本图形

作者: 巩翔宇Ibrahimovic | 来源:发表于2019-12-05 22:40 被阅读0次

在海南岛仓促的学习笔记,后期会来订正的。

Chapter 6 基本图形

6.1 条形图

barplot(height)
install.packages("vcd")

6.1.1 简单的条形图

library(vcd)
counts <- table(Arthritis$Improved)
counts

#简单条形图
barplot(counts,main = "Simple Bar Plot",
        xlab = "Improvement",ylab = "Frequency")
#水平条形图
barplot(counts,main = "Horizontal Bar Plot",
        xlab = "Frequency",ylab = "Improvement",horiz = T)

counts <- table(Arthritis$Improved,Arthritis$Treatment)
counts
#堆砌条形图
barplot(counts,main = "Stacked Bar Plot",
        xlab = "Treatment",ylab = "Frequency",
        col = c("red","yellow","green"),
        legend=row.names(counts))
#分组条形图
barplot(counts,main = "Grouped Bar Plot",
        xlab = "Treatment",ylab = "Frequency",
        col = c("red","yellow","green"),
        legend=row.names(counts),beside = T)

6.1.3 均值条形图

#排序后均值的条形图
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)
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"))
6.1.5 棘状图
library(vcd)
attach(Arthritis)
counts <- table(Treatment,Improved)
spine(counts,main = "Spinogram Example")
detach(Arthritis)

6.2 饼图

par(mfrow=c(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 Percentages")
library(plotrix)
pie3D(slices,labels = lbls,explode = 0.1,
      main="3D Pie Chart")
mytable <- table(state.region)
lbl3 <- paste(names(mytable),"\n",mytable,sep = "")
pie(mytable,labels = lbl3,main = "Pie Chart from a Table\n(with Sample Size)")

饼图

library(plotrix)
slices <- c(10,12,4,16,8)
lbls <- c("US","UK", "Australia","Germany","France")
fan.plot(slices,labels = lbls,main = "Fan Plot")

6.3 直方图

par(mfrow=c(2,2))
hist(mtcars$mpg)
hist(mtcars$mpg,breaks = 12,col="red",xlab = "Miles per gallon",
     main = "colored hisgram with 12 bins")
hist(mtcars$mpg,freq=F,breaks = 12, col="red",xlab = "Miles per gallon",
     main = "hisgram runplot density curve  ")
rug(jitter(mtcars$mpg))
lines(density(mtcars$mpg),col="blue",lwd=2)

x <- mtcars$mpg
h <- hist(x,breaks = 12, col="red",xlab = "Miles per gallon",
          main = "hisgram with normal curve and box  ")
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)
box()

6.4 核密度图

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")

#可比较的核密度图
library(sm)
attach(mtcars)
cyl.f <- factor(cyl,levels = c(4,6,8),
                labels = c("4 cylinder","6 cylinder","8 cylinder"))
sm.density.compare(mpg,cyl,xlab="Miles Per Gallon")
title(main="MPG distribution by Car Cylinders")

colfill <- c(2:(1+length(levels(cy1.f))))
legend(locator(1),levels(cy1.f),fill = colfill)
detach(mtcars)

6.5 箱线图

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

boxplot(mpg ~ cyl,data=mtcars,
        notch=T,
        varwidth=T,
        col="red",
        main="Car Mileage Data",
        xlab = "Number of Cylinders",
        ylab = "Miles Per Gallon")

#两个交叉因子的箱线图
mtcars$cyl.f <- factor(mtcars$cyl,
                       levels = c(4,6,8),
                       labels = c("4","6","8"))
mtcars$am.f <- factor(mtcars$am,
                      levels = c(0,1),
                      labels = c("auto","standard"))
boxplot(mpg ~ am.f * cyl.f,
        data = mtcars,
        varwidth=T,
        col=c("gold","darkgreen"),
        main="MPG Distribution by Auto Type",
        xlab = "Auto Type",ylab = "Miles Per Gallon")

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",ylab = "Miles Per Gallon",
      xlab = "Numbers of Cylinders")

6.6 点图

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

#分组、排序、着色后的点图
x <- mtcars[order(mtcars$mpg),]
x$cyl <- factor(x$cyl)
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,
         labels = row.names(x),
         cex=.7,
         groups = x$cyl,
         gcolor = "black",
         color = x$color,
         pch = 19,
         main = "Gas Mileage for Car Models\ngrouped by cylinders",
         xlab = "Miles Per Gallon")

相关文章

网友评论

    本文标题:[R语言]Chapter 6 基本图形

    本文链接:https://www.haomeiwen.com/subject/vqajgctx.html