R语言实战 - 基本图形(1)- 条形图

1. 条形图

1.1 简单的条形图

> 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="Frequency",
+         horiz=TRUE)
> 

     

1.2 堆砌条形图和分组条形图

> library(vcd)
> 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)
> 

     

1.3 均值条形图

> states <- data.frame(state.region, state.x77)
> means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)
> means
        Group.1        x
1     Northeast 1.000000
2         South 1.737500
3 North Central 0.700000
4          West 1.023077
> means <- means[order(means$x),]
> means
        Group.1        x
3 North Central 0.700000
1     Northeast 1.000000
4          West 1.023077
2         South 1.737500
> barplot(means$x, names.arg=means$Group.1)
> title("Mean Illiteracy Rate")
> 

1.4 条形图的微调

> par(mar=c(5,8,4,2))
> par(las=2)
> counts <- table(Arthritis$Improved)
> barplot(counts,
+         main="Treatment Outcome",
+         horiz=TRUE, cex.names=0.8,
+         names.arg=c("No Improvement", "Some Improvement", "Marked Improvement"))
> 

1.5 棘状图

> library(vcd)
> attach(Arthritis)
> counts <- table(Treatment, Improved)
> spine(counts, main="Spinogram Example")
> detach(Arthritis)
> 

 

posted on 2017-09-17 17:04  你的踏板车要滑向哪里  阅读(743)  评论(0编辑  收藏  举报

导航