假设我们通过如下命令产生了一组数据:
n = 50
set.seed(0)
x = runif(n, min=-2, max=2)
y = x^3 + rnorm(n)
请以x作为横轴,y作为纵轴作折线图,效果如下图。
请以x作为横轴,y作为纵轴做散点图,要求cex=2,颜色为红色,点的形状为默认的空心圆形,标题为数学公式 \(y=x^3\)。
请以x作为横轴,y作为纵轴做散点图,要求下标为奇数的点的形状为空心圆形,下标为偶数的点的形状为实心圆形。
假设额外还有一组数据是通过如下命令产生的:
x2 = sort(runif(n, min=-2, max=2))
y2 = x^2 + rnorm(n)
请在题2的基础上叠加上述数据的散点图,要求新加的数据为蓝色的实心圆形。
在题4导出的图的基础上通过legend()增加图例,图例的名字为“三次函数”和“二次函数”,图例的位置以不覆盖掉原有的点为好。
在题2的基础上叠加曲线 \(y=x^3\),颜色为灰色,类型为虚线,并在恰当的位置上添加文字“这是一个三次函数曲线”。
在题2的基础上修改纵坐标标签的位置为seq(-8,6,2),同时修改纵坐标的标签为数学符号\(x^3\)。
n = 50
set.seed(0)
x = runif(n, min=-2, max=2)
y = x^3 + rnorm(n)
x2 = sort(runif(n, min=-2, max=2))
y2 = x2^2 + rnorm(n)
#1
data <- data.frame(x, y)
dat <- data[order(x),]
plot(dat,type = "l", xlab = "x", ylab = "y")
#2
plot(x,y,cex=2,col="red",main=expression(y==x^3))
#3
plot(x,y,pch=ifelse(which(x==x)%%2==0,16,1)) #plot(x,y,pch = c(1,16));plot(x, y, pch=ifelse(seq_along(x)%%2,1,16));match(x,x)、seq(x)函数也可
#4
plot(x,y,cex=2,col="red",main=expression(y==x^3))
points(x2,y2,col="blue",pch=16)
#5
plot(x,y,xlab = 'x',ylab = 'y',cex=2,col="red",main=expression(y==x^3))
points(x2,y2,col="blue",pch=16)
legend("bottomright", legend = c("三次函数", "二次函数"),col = c("red", "blue"), pch = c(1, 16), pt.cex = c(2, 1))
#6
plot(x,y,xlab = 'x',ylab = 'y',cex=2,col="red",main=expression(y==x^3))
curve(x^3,type = 'l',add=T,col="gray",lty=2)
text(0, 2, "这是一个三次函数曲线")
#7
plot(x,y,xlab = 'x',ylab=expression(x^3),cex=2,col="red",main=expression(y==x^3))
axis(2,at= seq(-8,6,2),labels = seq(-8,6,2))
covid19.csv文档里面记录了某地区新冠病人的住院信息。先根据上节课的内容对数据进行预处理,即把“入院时间”异常值修正,并计算相应的疗程作为新的变量添加到数据框中。
covid <- read.csv("C:/Users/cy/Documents/covid19.csv", header = TRUE,na.strings = "", row.names = "序号")
covid[, "入院时间"] <- as.Date(as.character(covid[, "入院时间"]),
format = "%Y%m%d")
covid[, "出院时间"] <- as.Date(as.character(covid[, "出院时间"]),
format = "%Y%m%d")
covid[, "住院时间"] <- as.numeric(covid[, "出院时间"] -
covid[,"入院时间"]) + 1
covid[covid[, "住院时间"] < 0, "入院时间"] <- as.Date("2020-02-05")
covid[, "住院时间"] <- as.numeric(covid[, "出院时间"] -
covid[, "入院时间"]) + 1
#1
library(ggplot2)
ggplot(data = covid, aes(x = "", y = 年龄)) + geom_boxplot(fill = "green")
#2
ggplot(data = covid, aes(x = 年龄)) +geom_histogram(aes(y = ..density..), binwidth = 5) +geom_density(alpha = 0.3, fill = "red")
#3
ggplot(data = covid, aes(x = 分型, fill = 分型)) +
geom_bar() +
guides(fill = FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
#4
my_plot <- ggplot(data = covid,
aes(x = 分型, y = 住院时间, fill = 分型)) +
geom_boxplot() +
stat_summary(fun.y = "median", geom = "point",
shape = 5, size = 3)
## Warning: `fun.y` is deprecated. Use `fun` instead.
my_plot
#5
my_plot +
labs(title = "新冠病人的疗程与分型的关系",
x = "新冠分型", y = "疗程(按天计)") +
theme(plot.title = element_text(hjust = 1))
#6
ggplot(data = covid, aes(x = 年龄, y = 疗程,
color = 性别, size = 入院时间)) +
geom_point() +
theme(panel.background = element_rect(fill = "white"))
## Warning: Removed 11 rows containing missing values (geom_point).
#7
ggplot(data = covid[!is.na(covid[,"性别"]),], aes(x = "", y = 年龄)) +
geom_boxplot(fill = "green", color = "red",na.rm=T) +
facet_grid(性别 ~ 分型)
#ggplot(data = covid,aes(y=年龄))+geom_boxplot(fill="green",colour="red")+facet_wrap(性别~分型)
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) 输出。for (i in 1:9) {
for (j in 1:i) {
cat(format(i * j, width = 4))
}
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
a=c(0.8,0.1,0.1)
y <- numeric(103)
y[1] <- 1
y[2] <- 3
y[3] <- 5
for (i in 4:103) {
y[i] <- sum(rev(a)*y[(i-3):(i-1)])
}
cat("The values of y1, y2 to y100 are:\n")
## The values of y1, y2 to y100 are:
y <- y[4:103]
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