假设我们通过如下命令产生了一组数据:
n = 50
set.seed(0)
x = runif(n, min=-2, max=2)
y = x^3 + rnorm(n)
x作为横轴,y作为纵轴作折线图,效果如下图。setwd("D:/homework_R/HW3")
x_1<-sort(x)
y_1<-y[order(x)] #order按照x的升序返回元素下标,使得x-y值对应
plot(x_1,y_1,type='l',lwd=2,xlab="x",ylab="y")
x作为横轴,y作为纵轴做散点图,要求cex=2,颜色为红色,点的形状为默认的空心圆形,标题为数学公式 \(y=x^3\)。plot(x,y,cex=2,col="red",main=expression(y==x^3),xlab="x",ylab="y")
x作为横轴,y作为纵轴做散点图,要求下标为奇数的点的形状为空心圆形,下标为偶数的点的形状为实心圆形。plot(x,y,cex=1,pch=ifelse(seq_along(x)%%2==1,1,16))
4. 假设额外还有一组数据是通过如下命令产生的:
```r
x2 = sort(runif(n, min=-2, max=2))
y2 = x2^2 + rnorm(n)
```
请在题2的基础上叠加上述数据的散点图,要求新加的数据为蓝色的实心圆形。
plot(x,y,cex=2,col="red",main=expression(y==x^3),xlab="x",ylab="y")
points(x2,y2,col="blue",pch=16,cex=2) #尝试用add=TRUE失败
legend()增加图例,图例的名字为“三次函数”和“二次函数”,图例的位置以不覆盖掉原有的点为好。plot(x,y,cex=2,col="red",main=expression(y==x^3),xlab="x",ylab="y")
points(x2,y2,col="blue",pch=16,cex=2) #尝试用add=TRUE失败
legend("bottomright",col=c("red","blue"),legend=c("三次函数","二次函数"),pch=c(1,16))
plot(x,y,cex=2,col="red",main=expression(y==x^3),xlab="x",ylab="y")
curve(x^3,from=-2,to=2,col="grey",lty=2,lwd=3,add=TRUE)
text(-0.9,-7,"这是一个三次函数曲线")
seq(-8,6,2),同时修改纵坐标的标签为数学符号\(x^3\)。plot(x,y,cex=2,col="red",main=expression(y==x^3),xlab="x",ylab=expression(x^3),axes=FALSE)
axis(1)
axis(2,at=seq(-8,6,2))
covid19.csv文档里面记录了某地区新冠病人的住院信息。先根据上节课的内容对数据进行预处理,即把“入院时间”异常值修正,并计算相应的疗程作为新的变量添加到数据框中。
#数据预处理
d <- read.csv("covid19.csv", header = TRUE,na.string="") # header = TRUE 包括列名
#head(d) #查验是否读入正确
d[,"入院时间"] <- as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d")
d[,"出院时间"] <- as.Date(as.character(d[,"出院时间"]), format = "%Y%m%d")
d[,"住院时间"] <- as.numeric(d[,"出院时间"] - d[,"入院时间"]) + 1
d[d$住院时间<0,"入院时间"] <- as.Date("2020-02-05") #修改异常值
d[,"疗程"]<-as.numeric(d[,"出院时间"]-d[,"入院时间"])+1 #算上疗程缺失值的;更新疗程
#或者在已知异常数据是2022年的时候,用以下方法
#index<-which.max(d$入院时间)
#d[index,"入院时间"]<- as.Date("2020-02-05")
#d[index,"疗程"]<-d[index,"出院时间"]-d[index,"入院时间"]+1
library(ggplot2)
ggplot(d,aes(x="",y=年龄))+geom_boxplot(fill="green")
p1<-ggplot(d,aes(x=年龄))
p1+geom_histogram(binwidth=5,color="black",fill="white")+ylab("人数")
#+geom_text(aes(label=as.character(y))stat="bin",binwidth=5,vjust=-0.3)
p1+geom_histogram(aes(y=..count../nrow(d)),binwidth=5,color="black",fill="white")+geom_density(alpha=0.3,fill="red")+ylab("所占总人数的比例")
p1+geom_histogram(aes(y=..density..),binwidth=5,color="black",fill="white")+geom_density(alpha=0.3,fill="red")+ylab("密度") #此时y轴展现的应该是密度函数值而不是频率比例
p2<-ggplot(d,aes(x=分型),legend=FALSE)
p2+geom_bar(aes(fill=分型))+ylab("人数")+guides(fill=FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
ggplot(d, aes(x=分型,y=疗程)) + geom_boxplot(aes(fill=分型)) +
geom_point(stat = "summary",fun= "median", color="white", size = 2) #也可以用stat_summary(geom = "point", fun.y = "median", color="white", size = 2)
ggplot(d, aes(x=分型,y=疗程)) + geom_boxplot(aes(fill=分型)) +
geom_point(stat = "summary",fun= "median", color="white", size = 2)+
labs(x="新冠分型",y="疗程(按天计)")+
ggtitle("新冠病人的疗程与分型的关系")+
theme(plot.title=element_text(hjust=1)) #也可以用stat_summary(geom = "point", fun.y = "median", color="white", size = 2)
ggplot(d,aes(x=年龄,y=疗程))+
geom_point(aes(size=入院时间,color=性别))+
theme(panel.background = element_rect(fill = "white"))
p<-ggplot(d,aes(x="",y=年龄))+geom_boxplot(fill="green",color="red")
p+facet_grid(性别~分型)+xlab("")
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
for(i in 1:9){
for(j in 1:i){
cat(i*j,seq=" ")
}
cat("\n")
}
## 1
## 2 4
## 3 6 9
## 4 8 12 16
## 5 10 15 20 25
## 6 12 18 24 30 36
## 7 14 21 28 35 42 49
## 8 16 24 32 40 48 56 64
## 9 18 27 36 45 54 63 72 81
print(y, digits=3) 输出。a<-c(0.8,0.1,0.1)
y=vector(mode="numeric",length=103)
y[1]=1;y[2]=3;y[3]=5 #放入初始值
for(i in 4:103){
for(j in 1:3){
y[i]<-y[i]+a[j]*y[i-j]
}
}
for(i in 1:100)y[i]<-y[i+3]
for(i in 1:3)y<-y[-(i+100)]
print(y, digits=3)
## [1] 4.40 4.32 4.40 4.39 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38
## [16] 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38
## [31] 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38
## [46] 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38
## [61] 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38
## [76] 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38
## [91] 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38 4.38