R语言中单变量的各种图形的绘制

一、条形图

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


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值