R语言中单变量的各种图形的绘制
2016-02-19 14:55
417 查看
一、条形图
library(vcd)
counts <- table(Arthritis$Improved)
counts
None Some Marked
42 14 28
barplot(counts, main = "Simple Bar Plot", xlab = "Improvement",
ylab = "Frequency")
barplot(counts, main = "Horizontal Bar Plot", xlab = "Frequency",
ylab = "Improvement", horiz = TRUE)
二、分组条形图
counts <- table(Arthritis$Improved, Arthritis$Treatment)
counts
Placebo Treated
None 29 13
Some 7 7
Marked 7 21
barplot(counts, main = "Stacked Bar Plot", xlab = "Treatment",
ylab = "Frequency", col = c("red", "yellow", "green"),
legend = rownames(counts))
barplot(counts, main = "Grouped Bar Plot", xlab = "Treatment",
ylab = "Frequency", col = c("red", "yellow", "green"),
legend = rownames(counts),
beside = TRUE)
三、总数条形图
states是一个数据集
str(states)
newdata<-data.frame(s1=states$state.region,s2=states$Population)
total<-aggregate(newdata$s2,by=list(newdata$s1),FUN=sum) 将by的字段分组,将数据按照FUN方法进行操作
total<-total[order(total$x),] 排序按照人口的总量
barplot(total$x,xlab="population number",ylabel="different position")
绘制出一个条形图
四、饼图
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(lbls)),
main = "Pie Chart with Percentages")
library(plotrix)
pie3D(slices, labels = lbls, explode = 0.1, main = "3D Pie Chart ")
mytable <- table(state.region)
lbls <- paste(names(mytable), "\n", mytable, sep = "")
pie(mytable, labels = lbls,
main = "Pie Chart from a Table\n (with sample sizes)")
五、直方图
par(mfrow = c(2, 2))
hist(mtcars$mpg)
hist(mtcars$mpg, breaks = 12, col = "red",
xlab = "Miles Per Gallon",
main = "Colored histogram with 12 bins")
hist(mtcars$mpg, freq = FALSE, breaks = 12, col = "red",
xlab = "Miles Per Gallon",
main = "Histogram, rug plot, 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 = "Histogram 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()
六、箱图
boxplot(mpg ~ cyl, data = mtcars,
main = "Car Milage Data",
xlab = "Number of Cylinders",
ylab = "Miles Per Gallon")
七、点图
dotchart(mtcars$mpg, labels = row.names(mtcars),
cex = 0.7,
main = "Gas Milage 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 = 0.7,
pch = 19, groups = x$cyl,
gcolor = "black", color = x$color,
main = "Gas Milage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
library(vcd)
counts <- table(Arthritis$Improved)
counts
None Some Marked
42 14 28
barplot(counts, main = "Simple Bar Plot", xlab = "Improvement",
ylab = "Frequency")
barplot(counts, main = "Horizontal Bar Plot", xlab = "Frequency",
ylab = "Improvement", horiz = TRUE)
二、分组条形图
counts <- table(Arthritis$Improved, Arthritis$Treatment)
counts
Placebo Treated
None 29 13
Some 7 7
Marked 7 21
barplot(counts, main = "Stacked Bar Plot", xlab = "Treatment",
ylab = "Frequency", col = c("red", "yellow", "green"),
legend = rownames(counts))
barplot(counts, main = "Grouped Bar Plot", xlab = "Treatment",
ylab = "Frequency", col = c("red", "yellow", "green"),
legend = rownames(counts),
beside = TRUE)
三、总数条形图
states是一个数据集
str(states)
newdata<-data.frame(s1=states$state.region,s2=states$Population)
total<-aggregate(newdata$s2,by=list(newdata$s1),FUN=sum) 将by的字段分组,将数据按照FUN方法进行操作
total<-total[order(total$x),] 排序按照人口的总量
barplot(total$x,xlab="population number",ylabel="different position")
绘制出一个条形图
四、饼图
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(lbls)),
main = "Pie Chart with Percentages")
library(plotrix)
pie3D(slices, labels = lbls, explode = 0.1, main = "3D Pie Chart ")
mytable <- table(state.region)
lbls <- paste(names(mytable), "\n", mytable, sep = "")
pie(mytable, labels = lbls,
main = "Pie Chart from a Table\n (with sample sizes)")
五、直方图
par(mfrow = c(2, 2))
hist(mtcars$mpg)
hist(mtcars$mpg, breaks = 12, col = "red",
xlab = "Miles Per Gallon",
main = "Colored histogram with 12 bins")
hist(mtcars$mpg, freq = FALSE, breaks = 12, col = "red",
xlab = "Miles Per Gallon",
main = "Histogram, rug plot, 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 = "Histogram 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()
六、箱图
boxplot(mpg ~ cyl, data = mtcars,
main = "Car Milage Data",
xlab = "Number of Cylinders",
ylab = "Miles Per Gallon")
七、点图
dotchart(mtcars$mpg, labels = row.names(mtcars),
cex = 0.7,
main = "Gas Milage 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 = 0.7,
pch = 19, groups = x$cyl,
gcolor = "black", color = x$color,
main = "Gas Milage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
相关文章推荐
- Android ViewHolder简洁写法及替代findViewById方法
- 算法学习--模式匹配
- 统计有效、空行、注解代码行数
- Apache benchmark测试工具
- 海思hi3516C+imx122板子,VLC直播或录像mp4后回放,声音都是一顿一顿的。
- 关于VFS文件系统中的superblock、inode、d_entry和file数据结构
- Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.jndi.JndiNameException
- CentOS安装expect
- gnome和kdg中的图标主题规格(QT ,ICON,ubuntu
- Xms Xmx PermSize MaxPermSize 区别
- XCode设置(怎么让代码收缩)
- Java中数值类型之间的转换
- 矩阵快速幂模板
- 最大后验概率(MAP)- maximum a posteriori
- Java内存管理—虚拟机运行时数据区域
- 苏格拉底与失恋者的对话(转文)
- 阿里云Ubuntu部署java web(2) - 配置tomcat
- linux安装nginx
- 年终工作总结
- Java Web应用的配置描述符web.xml