您的位置:首页 > 其它

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: