学习小组Day4笔记--axin

R语言基础

安装Rstudio

电脑用户名为英文

Rstudio界面

引自生信星球

引自生信星球

几个命令

  • 散点图
    plot(rnorm(50))

    Rplot

  • 箱式图
    boxplot(iris$Sepal.Length~iris$Species,col = c("lightblue","lightyellow","lightpink"))
    iris$Sepal.Length表示:iris数据框的Sepal.Length这一列数据

    boxplot

  • 显示文件列表
    list.files

  • 删除
    rm(list = ls()) #清空所有变量

  • 列出历史命令
    history()

  • 清空控制台
    ctrl+l

R for data science

ggplot(data=mpg) + geom_point (mapping = aes(x=displ,y=hwy))

displ:引擎排量-L 35个,单位为升,小数
hwy:燃油效率:每加仑汽油能跑的公里数(高速路)单位英里/加仑,燃油效率高说明省油。 27个,整数。
class:车型
——生信星球

mpg.png
  • 用不同颜色color的点代表车型class
    ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
    color = class

注:相似语句:
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
注:color="blue"在aes()外

  • 用不同大小size的点代表车型class
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))
#> Warning: Using size for a discrete variable is not advised.
size = class

警告:不建议将无序变量种类(class)映射为有序图形属性大小(size)。

  • 用不同透明度alpha的点代表车型class
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
Warning message:Using alpha for a discrete variable is not advised. 
alpha = class

警告:不建议将无序变量种类(class)映射为有序图形属性透明度(alpha)。

  • 用不同形状shape的点代表车型class
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))
Warning messages:
1: The shape palette can deal with a maximum of 6 discrete values because more than 6 becomes difficult to discriminate; you have 7.Consider specifying shapes manually if you must have them. 
2: Removed 62 rows containing missing values (geom_point). 
shape = class

警告:ggplot2 只能同时使用 6 种形状,缺失的62行是suv车型没有被分配到形状
验证62:

 count(mpg,class)
# A tibble: 7 x 2
  class          n
  <chr>      <int>
1 2seater        5
2 compact       47
3 midsize       41
4 minivan       11
5 pickup        33
6 subcompact    35
7 suv           62
  • 手动设置图形属性,所有点设为蓝色
    ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
    color = "blue"

注:color="blue"在aes()外,原因:

此时颜色不会传达关于变量的信息,只是改变图的外观
要想手动设置图形属性,需要按名称进行设置,将其作为几何对象函数的一个参数。这也就是说,需要在函数 aes() 的外部进行设置。
此外,还需要为这个图形属性选择一个有意义的值"blue"。
——R for data science

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。