• R绘制箱线图


    代码大部分来自boxplot()函数的帮助文件,可以通过阅读帮助文件,调整代码中相应参数看下效果,进而可以理解相应的作用,帮助快速掌握barplot()函数的用法。

    语法

    Usage(来自帮助文件)

    1. barplot(height, ...)
    2. ## Default S3 method:
    3. barplot(height, width = 1, space = NULL,
    4. names.arg = NULL, legend.text = NULL, beside = FALSE,
    5. horiz = FALSE, density = NULL, angle = 45,
    6. col = NULL, border = par("fg"),
    7. main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
    8. xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
    9. axes = TRUE, axisnames = TRUE,
    10. cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
    11. inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
    12. add = FALSE, ann = !add && par("ann"), args.legend = NULL, ...)
    13. ## S3 method for class 'formula'
    14. barplot(formula, data, subset, na.action,
    15. horiz = FALSE, xlab = NULL, ylab = NULL, ...)

     

    可以从上述barplot()函数的帮助文件中看到,这个函数用法可以分为两类。

    箱线图

    该实例为heigh是一个矩阵。

    1. rand.data <- replicate(8, rnorm(100, 100, sd =1.5) ) #生成数据 100 X 8
    2. boxplot(rand.data) #绘制箱线图

    分类变量的箱线图

    1. data(mtcars)
    2. mtcars$cyl.f <- factor(mtcars$cyl,
    3. levels = c(4,6,8),
    4. labels = c("4", "6", "8"))# 创建气缸数量的因子
    5. mtcars$am.f <- factor(mtcars$am, levels=c(0,1), labels=c("auto", "standard")) #创建变速箱类型的因子
    6. boxplot(mpg~ am.f * cyl.f,
    7. data = mtcars,
    8. varwidth = TRUE,
    9. col=c("gold", "darkgreen"),
    10. main ="MPG Distribution by Auto Type",
    11. xlab="Auto Type",
    12. ylab="Miles Per Gallon") #绘制箱线图

     

     

    1. > barplot(VADeaths, beside = TRUE,
    2. + col = c("lightblue", "mistyrose", "lightcyan",
    3. + "lavender", "cornsilk"),
    4. + legend.text = rownames(VADeaths), ylim = c(0, 100))
    5. > title(main = "Death Rates in Virginia", font.main = 4)

     

     

    1. > require(grDevices)
    2. > hh <- t(VADeaths)[, 5:1]
    3. > mybarcol <- "gray20"
    4. > mp <- barplot(hh, beside = TRUE,
    5. + col = c("lightblue", "mistyrose",
    6. + "lightcyan", "lavender"),
    7. + legend.text = colnames(VADeaths), ylim = c(0,100),
    8. + main = "Death Rates in Virginia", font.main = 4,
    9. + sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
    10. + cex.names = 1.5)
    11. > VADeaths
    12. Rural Male Rural Female Urban Male Urban Female
    13. 50-54 11.7 8.7 15.4 8.4
    14. 55-59 18.1 11.7 24.3 13.6
    15. 60-64 26.9 20.3 37.0 19.3
    16. 65-69 41.0 30.9 54.6 35.1
    17. 70-74 66.0 54.3 71.1 50.0

    1. > segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
    2. > stopifnot(dim(mp) == dim(hh)) # corresponding matrices
    3. > mtext(side = 1, at = colMeans(mp), line = -2,
    4. + text = paste("Mean", formatC(colMeans(hh))), col = "red")

    1. > barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
    2. + legend.text = rownames(VADeaths))
    3. > title(main = list("Death Rates in Virginia", font = 4))

     

    1. > # Border color
    2. > barplot(VADeaths, border = "dark blue")

    1. > # Formula method
    2. > barplot(GNP ~ Year, data = longley)
    3. > barplot(cbind(Employed, Unemployed) ~ Year, data = longley)

     

    1. > barplot(Freq ~ Class + Survived, data = d.Titanic,
    2. + subset = Age == "Adult" & Sex == "Male",
    3. + main = "barplot(Freq ~ Class + Survived, *)", ylab = "# {passengers}", legend.text = TRUE)

     

    1. > # Alternatively, a mosaic plot :
    2. > mosaicplot(xt[,,"Male"], main = "mosaicplot(Freq ~ Class + Survived, *)", color=TRUE)
    3. > par(op)

     

     

    1. > # Default method
    2. > require(grDevices) # for colours
    3. > tN <- table(Ni <- stats::rpois(100, lambda = 5))
    4. > r <- barplot(tN, col = rainbow(20))
    5. > #- type = "h" plotting *is* 'bar'plot
    6. > lines(r, tN, type = "h", col = "red", lwd = 2)

    barplot(tN, col = heat.colors(12), log = "y")

     

     

    barplot(tN, col = gray.colors(20), log = "xy")

     

    1. > barplot(height = cbind(x = c(465, 91) / 465 * 100,
    2. + y = c(840, 200) / 840 * 100,
    3. + z = c(37, 17) / 37 * 100),
    4. + beside = FALSE,
    5. + width = c(465, 840, 37),
    6. + col = c(1, 2),
    7. + legend.text = c("A", "B"),
    8. + args.legend = list(x = "topleft"))

     

     

    1. > barplot(tN, space = 1.5, axisnames = FALSE,
    2. + sub = "barplot(..., space= 1.5, axisnames = FALSE)")

     

     

    1. > barplot(VADeaths, plot = FALSE)
    2. [1] 0.7 1.9 3.1 4.3
    3. > barplot(VADeaths, plot = FALSE, beside = TRUE)
    4. [,1] [,2] [,3] [,4]
    5. [1,] 1.5 7.5 13.5 19.5
    6. [2,] 2.5 8.5 14.5 20.5
    7. [3,] 3.5 9.5 15.5 21.5
    8. [4,] 4.5 10.5 16.5 22.5
    9. [5,] 5.5 11.5 17.5 23.5
    mp <- barplot(VADeaths) # default

     

    1. > tot <- colMeans(VADeaths)
    2. > tot
    3. Rural Male Rural Female Urban Male Urban Female
    4. 32.74 25.18 40.48 25.28
    5. > text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")

     

     

    参考:

    《R语言实战》(第2版)(2016年5月出版,人民邮电出版社)

    帮助文件

     

  • 相关阅读:
    抖音小店和商品橱窗一样吗?有什么区别?新手适合做哪个?
    共同见证丨酷雷曼武汉运营中心成立2周年
    快速排序(递归和非递归两种方法实现)
    [word] Word如何删除所有的空行? #职场发展#学习方法
    [黑马程序员SpringBoot2]——基础篇2
    MYSQL和MongoDB的分析
    阿里云大数据专业认证(ACP),值得报名吗?
    (黑马出品_05)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
    Spring与MyBatis整合
    [ubuntu]修改主机名
  • 原文地址:https://blog.csdn.net/u011375991/article/details/132923900