R课程记录--第一课
R课程记录–第一课@TOC
本篇的文章结构
1 R的安装以及R studio的安装。
2 老师提供的练习网页的学习。出现的错误,以及订正的方法。
R的安装
R的安装主要步骤:
- 找到R的官网https://cran.r-prigect.org/.可以找到最新的版本
- ,也可以下载最好用的3.5.3.还可以考虑微软发行的开源R版本。
Rstudio的安装
作为R的调试版本。可以下载的地址是https://www.rstudio.com/
课程的内容
练习一
1 安装包
install.packages(‘tidyverse’)
install.packages(‘modelr’)
install.packages(‘gapminder’)
install.packages(‘ggbeeswarm’)
install.packages(‘gganimate’)
install.packages(‘ggstance’)
2 加载library
library(tidyverse)
library(modelr)
options(na.action = na.warn)
library(gapminder)
library(ggbeeswarm)
library(gganimate)
library(ggstance)
一定要检查上述的package和library是否安装正常。
3 查看“gapminder”的数据出来没
4
gapminder %>%
ggplot(aes(year, lifeExp, color = country)) +
geom_line(alpha = 1/3) +
scale_color_viridis_d(guide = F)
练习二
1 在上述package和library的基础上,输入代码
animate_df <- gapminder %>%
filter(continent == ‘Asia’) %>%
arrange(year, lifeExp) %>%
mutate(order = 1:n())
animate_fills <- viridis::viridis(33)
animate_fills[5] <- ‘red’
p <- animate_df %>%
ggplot(aes(lifeExp, order, fill = country)) +
geom_barh(stat = “identity”) +
labs(title=’{closest_state}’) +
scale_fill_manual(values = animate_fills, guide = F) +
scale_y_continuous(breaks=animate_df
o
r
d
e
r
,
l
a
b
e
l
s
=
a
n
i
m
a
t
e
d
f
order, labels=animate_df
order,labels=animatedfcountry, expand = c(0, 0)) +
transition_states(year, transition_length = 1, state_length = 50) +
view_follow(fixed_x = T) +
ease_aes(‘linear’)
animate(p, nframes = 60)
- 提示报错p变量为有数值。差错发现p变量在animat_df这个数据就出现未传递的情况。
老师提供了新版的代码
animate_df <- gapminder %>%
filter(continent == ‘Asia’) %>%
arrange(year, lifeExp) %>%
mutate(order = 1:n())
animate_fills <- viridis::viridis(33)
animate_fills[5] <- ‘red’
animate_df %>%
ggplot(aes(lifeExp, order, fill = country)) +
geom_barh(stat = “identity”) +
labs(title=’{closest_state}’) +
scale_fill_manual(values = animate_fills, guide = F) +
scale_y_continuous(breaks=animate_df
o
r
d
e
r
,
l
a
b
e
l
s
=
a
n
i
m
a
t
e
d
f
order, labels=animate_df
order,labels=animatedfcountry, expand = c(0, 0)) +
transition_states(year, transition_length = 1, state_length = 50) +
view_follow(fixed_x = T) +
ease_aes(‘linear’)
补充解决的方法是加载了’devtools’的安装包,以及devtools::install_github(‘thomasp85/gganimate’) 的代码。并添加了library(ggplot2) 和library(gifski)。出现图形