《R语言实战》中ggplot2之Salaries数据集调用

2019-07-18  本文已影响0人  Ashu

学会用R语言中的ggplot2这个包做图非常重要,这两天学习《R语言实战》第19章ggplot2 做图,当把第409页给的代码敲到RStudio里,运行时报错:不存在Salaries数据集,图没法画出来,接下来很多图形的代码运用
Salaries数据集做示例,如果这一步报错,往后的操作也将报错,可能是哪里出了错,花了很多时间尝试解决。书中的代码和运行结果如下:

data(Salaries,package = "car")
library(ggplot2)
ggplot(Salaries,aes(x=rank,y=salary))+
  geom_boxplot(fill="cornflowerblue",
               color="black",notch=TRUE)+
  geom_point(position = "jitter",color="blue",alpha=.5)+
  geom_rug(side="1",color="black")

###运行时,在console显示报错:

> data(Salaries,package = "car")
Warning message:
In data(Salaries, package = "car") : 没有‘Salaries’这个数据集
> library(ggplot2)
> ggplot(Salaries,aes(x=rank,y=salary))+
+   geom_boxplot(fill="cornflowerblue",
+                color="black",notch=TRUE)+
+   geom_point(position = "jitter",color="blue",alpha=.5)+
+   geom_rug(side="1",color="black")
Error in ggplot(Salaries, aes(x = rank, y = salary)) : 
  找不到对象'Salaries'
屏幕快照 2019-07-17 16.41.49.png 屏幕快照 2019-07-17 16.48.03.png
data(Salaries, package="car")
library(ggplot2)
ggplot(Salaries, aes(x=rank, y=salary)) +
  geom_boxplot(fill="cornflowerblue",
               color="black", notch=TRUE)+
  geom_point(position="jitter", color="blue", alpha=.5)+
  geom_rug(side="l", color="black")

- 运行后,在console处依旧出现报错,

data(Salaries, package="car")
Warning message:
In data(Salaries, package = "car") : 没有‘Salaries’这个数据集
> library(ggplot2)
> ggplot(Salaries, aes(x=rank, y=salary)) +
+   geom_boxplot(fill="cornflowerblue",
+                color="black", notch=TRUE)+
+   geom_point(position="jitter", color="blue", alpha=.5)+
+   geom_rug(side="l", color="black")
Error in ggplot(Salaries, aes(x = rank, y = salary)) : 
  找不到对象'Salaries'
 Salaries
屏幕快照 2019-07-17 16.33.49.png
install.packages("carData")
install.packages("car")
library(carData)
library(car)
data(Salaries, package="car")
library(ggplot2)
ggplot(Salaries, aes(x=rank, y=salary)) +
  geom_boxplot(fill="cornflowerblue",
               color="black", notch=TRUE)+
  geom_point(position="jitter", color="blue", alpha=.5)+
  geom_rug(side="l", color="black")
###console 输出的结果
> library(carData)
> library(car)
> data(Salaries, package="car")
Warning message:
In data(Salaries, package = "car") : 没有‘Salaries’这个数据集
> library(ggplot2)
> ggplot(Salaries, aes(x=rank, y=salary)) +
+   geom_boxplot(fill="cornflowerblue",
+                color="black", notch=TRUE)+
+   geom_point(position="jitter", color="blue", alpha=.5)+
+   geom_rug(side="l", color="black")
Warning: Ignoring unknown parameters: side
屏幕快照 2019-07-17 18.39.06.png
library(car)
#data(Salaries, package="car")  用#把这行代码注释掉
library(ggplot2)
ggplot(Salaries, aes(x=rank, y=salary)) +
  geom_boxplot(fill="cornflowerblue",
               color="black", notch=TRUE)+
  geom_point(position="jitter", color="blue", alpha=.5)+
  geom_rug(side="l", color="black")
屏幕快照 2019-07-17 18.52.48.png

总结:

对于大牛来说,是个简单的小问题,但是对我这位新手来说,独立思考和解决问题,能激发和鼓励我继续修行R语言,以后书上给一个数据集或者函数,使用前加载它关联的包(没安装就安装),遇到问题就Google或百度,独立搜索和解决,印象深刻,希望我的分享能帮到初学者。

课程分享

生信技能树全球公益巡讲

https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g

B站公益74小时生信工程师教学视频合辑

https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw

招学徒:

https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw

上一篇下一篇

猜你喜欢

热点阅读