小言_互联网的博客

R语言条形图 bar chart

329人阅读  评论(0)

条形图通过条形的高度或者长短来表示数据多少的图形。用来展示分类变量(categorical)的组成和分布。
R语言的base包里面有barplot可以绘制条形图。ggplot2包里面有geom_bar可以绘制。

barplot

barplot函数

barplot(height, width = 1, space = NULL,
        names.arg = NULL, legend.text = NULL, beside = FALSE,
        horiz = FALSE, density = NULL, angle = 45,
        col = NULL, border = par("fg"),
        main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
        xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
        axes = TRUE, axisnames = TRUE,
        cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
        inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
        add = FALSE, ann = !add && par("ann"), args.legend = NULL, ...)

horiz,默认是FALSE, 条形图是垂直的。设置为TRUE后,条形图会变成水平的。
subset,在有多个属性的data.frame数据上,subset会用到。选出一部分数据作画。
beside,默认是FALSE, height变量会在不同的分类变量下展现为堆砌条形图。设置为TRUE,会变为并列条形图。

简单条形图

例子1

> studentGender <- c("Female","Male")
> studentNumber <- c(340, 234)
> barplot(studentNumber~studentGender )

例子2

我们画图用到的数据是data.frame的Titanic.

summary(d.Titanic <- as.data.frame(Titanic))
  Class       Sex        Age     Survived      Freq       
 1st :8   Male  :16   Child:16   No :16   Min.   :  0.00  
 2nd :8   Female:16   Adult:16   Yes:16   1st Qu.:  0.75  
 3rd :8                                   Median : 13.50  
 Crew:8                                   Mean   : 68.78  
                                          3rd Qu.: 77.00  
                                          Max.   :670.00  

> head(d.Titanic)
  Class    Sex   Age Survived Freq
1   1st   Male Child       No    0
2   2nd   Male Child       No    0
3   3rd   Male Child       No   35
4  Crew   Male Child       No    0
5   1st Female Child       No    0
6   2nd Female Child       No    0
barplot(Freq ~ Class, data = d.Titanic, subset = Age == "Adult" & Sex == "Male" & Survived=="Yes")

堆砌条形图 stacked bars

barplot(Freq ~ Class + Sex, data = d.Titanic, subset = Age == "Adult" & Survived=="Yes", legend = TRUE)

分组条形图 group bars

group bars又叫 juxtaposed bars。默认的barplot在多个分布属性下生成的是stacked bars。将函数参数beside设置为TRUE,就是分组条形图。

barplot(Freq ~ Class + Sex, data = d.Titanic, subset = Age == "Adult" & Survived=="Yes", beside=TRUE, legend = TRUE)

条形图设置颜色

在基于上面分组条形图的基础上,设置了颜色。用到了包RColorBrewer.

library(RColorBrewer)
mypalette<-brewer.pal(4,"Greens")
summary(d.Titanic <- as.data.frame(Titanic))
barplot(Freq ~ Class + Sex, data = d.Titanic, subset = Age == "Adult" & Survived=="Yes", beside=TRUE, legend = TRUE, col=mypalette)

ggplot2 条形图

简单条形图

library("ggplot2")
studentGender <- c("Female","Male")
studentNumber <- c(340, 234)
studentData <- data.frame(studentGender, studentNumber)
ggplot(studentData, aes(studentGender, studentNumber)) + 
  geom_bar(stat="identity")

堆砌条形图 stacked bars

library("ggplot2")
summary(d.Titanic <- as.data.frame(Titanic))
ggplot(d.Titanic , aes(Class, Freq)) + 
  geom_bar(stat="identity",aes(fill=Sex))

分组条形图 group bars

基于上面的R script, 设置geom_bar的position参数为dodge,会生成分组条形图。

summary(d.Titanic <- as.data.frame(Titanic))
ggplot(d.Titanic , aes(Class, Freq)) + 
geom_bar(stat="identity",position="dodge",aes(fill=Sex))


我们可以在上面的script加上coord_flip将图形反转一下。

summary(d.Titanic <- as.data.frame(Titanic))
ggplot(d.Titanic , aes(Class, Freq)) + 
 geom_bar(stat="identity",aes(fill=Sex)) + 
 coord_flip()


转载:https://blog.csdn.net/santiagozhang/article/details/104514348
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场