美文网首页
瑞德学习R语言day03

瑞德学习R语言day03

作者: __method__ | 来源:发表于2021-05-21 21:00 被阅读0次

factor(变量)

因子类型的转化


R语言绘图

使用ggplot2绘图
导入依赖

library(ggplot2)

绘制柱状图

> mtcars$gear
 [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
> class(mtcars$gear)
[1] "numeric"
> barplot(mtcars$gear)
  • 统计一组数中元素的情况
> table(mtcars$gear)

 3  4  5 
15 12  5 
> counts = table(mtcars$gear)
> barplot(counts)

添加横坐标标签和定义柱状图颜色

> barplot(counts, names.arg = c("3 gears","4 gears", "5 挡"))
> barplot(counts, names.arg = c("3 gears","4 gears", "5 挡"), col="red")
> barplot(counts, names.arg = c("3 gears","4 gears", "5 挡"), col="lightblue")
> barplot(counts, names.arg = c("3 gears","4 gears", "5 挡"), col="pink")

标签显示变成竖直

> par(las=2)
> barplot(counts, names.arg = c("3 gears","4 gears", "5 挡"), col="pink")

多个条状图绘制



上面这样没有图例很难辨认数据的情况
指定字段绘制图形

> counts1 = table(mtcars$cyl, mtcars$gear)
> counts1
   
     3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2
> barplot(counts1, names.arg =  c("3 gears","4 gears", "5 gears"), col=c("pink", "blue", "yellow"),legend=rownames(counts1))
> rownames(counts1)
[1] "4" "6" "8"

自定义图例 传入一个向量

> barplot(counts1, names.arg =  c("3 gears","4 gears", "5 gears"), col=c("pink", "blue", "yellow"),legend=c("4 cyl", "6 cyl","8 cyl"))

指定 beside = TRUE

> barplot(counts1, names.arg =  c("3 gears","4 gears", "5 gears"), col=c("pink", "blue", "yellow"),legend=c("4 cyl", "6 cyl","8 cyl"), beside = TRUE)

ggplot绘图"套路"

> ggplot(mtcars) # ggplot函数没有任何显示, 这里只是做数据集等准备操作
> p = ggplot(mtcars)
> p + geom_bar()
错误: stat_count() requires an x or y aesthetic.
Run `rlang::last_error()` to see where the error occurred.
> p = ggplot(mtcars, aes(x=factor(cyl))) #指定 cyl为横坐标
> p + geom_bar()

更换数据集mpg绘图

> p1 = ggplot(mpg, aes(class))
> aes(class)
Aesthetic mapping: 
* `x` -> `class`
> table(mpg$class)

   2seater    compact    midsize    minivan     pickup subcompact        suv 
         5         47         41         11         33         35         62 
> p1 + geom_bar()
p1 = ggplot(mpg, aes(class))
# p1 + geom_bar(aes(weight=displ))
p1 + geom_bar(aes(weight=year)) # 这里是属于这类所有值的相加

双重条形图

p1 = ggplot(mpg, aes(class))
p1 + geom_bar(aes(fill= drv)) # fill 是让ggplot自动根据因子的水平分数分配颜色等值

指定为水平条状图

p1 = ggplot(mpg, aes(class))
# p1 + geom_bar(aes(fill= drv)) # fill 是让ggplot自动根据因子的水平分数分配颜色等值
# 水平
p1 + geom_bar(aes(fill= drv), position = position_stack(reverse = TRUE))+ coord_flip() + theme(legend.position = "bottom")

直方图

> hist(mtcars$wt)
# 变成总面积为1的概率分布直方图
> hist(mtcars$wt, freq = F)

直方图参数增加
br 横坐标刻度范围 xlab横轴标签 main是标题

hist(mtcars$wt, freq = F, br=seq(0, 6, by =0.5),col = 'red', xlab = 'weight of cars', main = '车重直方图')

举一反三

> barplot(mtcars$wt, xlab = "车的重量", ylab = "数量", main = "数据标题", col = 'yellow')

相关文章

  • 瑞德学习R语言day03

    factor(变量) 因子类型的转化 R语言绘图 使用ggplot2绘图导入依赖 绘制柱状图 统计一组数中元素的情...

  • 瑞德学习R语言day07

    曲线绘制 dnorm(x, mean = 0, sd = 1, log = FALSE) 的返回值是正态分布概率密...

  • 瑞德学习R语言day06

    基本统计数值 查看键值情况 平均数和中位数 标准差和方差 数据总结 查看筛选子集的的统计结果选择重量大于或等于 3...

  • 瑞德学习R语言day05

    散点图 特性: 两个变量之间的关系分布图 精细化 数据拟合 简单说一下 lm 函数 -formula:指要拟合的模...

  • 瑞德学习R语言day04

    ggplot 直方图 箱线图 用在箱线图中的~: boxplot(y~x1),表示将x1视作分组变量, 分组输出y...

  • 瑞德学习R语言day02

    R依赖包的安装 R中的数据结构 通过数据集来了解R中数据结构查看变量的详情 查看变量内部的数据类型 变量的筛选 数...

  • day5 阿来

    继续学习R语言 R语言数据学习 数据R语言学习.png 数据输入 数据输出 总结 R语言学习的第二天,熟悉了很多操...

  • 第八周作业汇总(蚂蚁私塾一期三班)

    本周学习书籍:《助推》— 查理德·H·泰勒(美)、卡斯·R·桑坦斯(美) 整理人:陆瑞林-20171029 一、本...

  • R语言-0基础学习4-实战1-常见操作

    R语言学习系列R语言-0基础学习1-数据结构R语言-0基础学习2-构建子集R语言-0基础学习3-循环排序信息处理函...

  • 学习小组Day4笔记--行

    正式开始学习R语言了,今天笔记内容为R语言基础和ggplot2的入门学习 R语言基础 1、认识R与RStudio ...

网友评论

      本文标题:瑞德学习R语言day03

      本文链接:https://www.haomeiwen.com/subject/imxgjltx.html