R_for_Data_Science icon indicating copy to clipboard operation
R_for_Data_Science copied to clipboard

参考答案新版本 B.13

Open hope-data-science opened this issue 1 year ago • 0 comments
trafficstars

最近在课程中,通过师生通力合作,对一些可视化的参考答案做了更多的实现,供参考:

B.13 版本1

library(ggplot2)
#产生正态分布的密度函数
dist_func1 <- function(x) {
  dnorm(x, mean = 20, sd = 10)
}
dist_func2 <- function(x) {
  dnorm(x, mean = 35, sd = 18)
}
ggplot() +
  stat_function(fun = dnorm, 
                args = list(mean = 20, sd = 10), 
                color = "dodgerblue"
  )+ #画出第一条曲线
  geom_ribbon(aes(ymin = 0, ymax = ..y..), alpha = 0.3, fill='skyblue',stat = "function", fun = dist_func1)+#填充曲线
  annotate("text", x = 20, y = 0.03, color='deepskyblue',label = "Without 
protective 
measures")+#添加注释
  stat_function(fun = dnorm, 
                args = list(mean = 35, sd = 18),
                color = "skyblue"
  )+ #画出第二条曲线#填充曲线
  geom_ribbon(aes(ymin = 0, ymax = ..y..), alpha = 0.6, fill='dodgerblue',stat = "function", fun = dist_func2)+
  annotate("text", x = 50, y = 0.01, color='white',label = "With 
protective 
measures")+#添加注释
  geom_hline(yintercept=0.024,linetype='dashed',color='blue')+#添加水平虚线
  geom_text(aes(x=70,y=0.026),label='Healthcare system capacity',size=6,color='deepskyblue')+#在虚线上添加文本
  labs(y=NULL,
       x = "Days since the first case",
       title = "Flattening the curve",
       subtitle = "Mitigation efforts can help to reduce the number of daily cases and to
reduce the pressure on the healthcare system",
       caption='SOURCE:CDC')+#添加标题
  annotate("text", x = 0.2, y = 0.049, color='grey',label = "Number of daily case")+
  
  scale_x_continuous(expand=expansion(mult=0))+
  scale_y_continuous(expand=expansion(mult=0))+#使图像从坐标原点出发
  expand_limits(x=c(-10,90),y=c(0,0.05))+#限制x轴的范围
  theme(
    panel.background=element_rect(fill='white'),#背景变白
    panel.grid = element_blank(),#消除网格
    axis.ticks = element_blank(),#消除x轴的标签
    axis.text=element_blank(),
    axis.line = element_line(color='grey',size = 1,arrow=arrow(type='open',length=unit(0.3,'cm'))),#调节坐标轴的颜色并变箭头
    axis.title.y= element_text(angle=0, margin=margin(t = -20, l = 0, r = -100)),#调整y轴文本的位置
    plot.caption = element_text(hjust=0)#调整左下角文字的位置
  )

版本2

library(ggplot2)
#产生正态分布的密度函数
dist_func1 <- function(x) {
  dnorm(x, mean = 20, sd = 10)
}
dist_func2 <- function(x) {
  dnorm(x, mean = 35, sd = 18)
}
ggplot() +
  stat_function(fun = dnorm, 
                args = list(mean = 20, sd = 10), 
                color = "dodgerblue"
  )+ #画出第一条曲线
  geom_ribbon(aes(ymin = 0, ymax = ..y..), alpha = 0.3, fill='skyblue',stat = "function", fun = dist_func1)+#填充曲线
  annotate("text", x = 20, y = 0.03, color='deepskyblue',label = "Without 
protective 
measures")+#添加注释
  stat_function(fun = dnorm, 
                args = list(mean = 35, sd = 18),
                color = "skyblue"
  )+ #画出第二条曲线#填充曲线
  geom_ribbon(aes(ymin = 0, ymax = ..y..), alpha = 0.6, fill='dodgerblue',stat = "function", fun = dist_func2)+
  annotate("text", x = 50, y = 0.01, color='white',label = "With 
protective 
measures")+#添加注释
  geom_hline(yintercept=0.024,linetype='dashed',color='blue')+#添加水平虚线
  geom_text(aes(x=70,y=0.026),label='Healthcare system capacity',size=6,color='deepskyblue')+#在虚线上添加文本
  labs(y=NULL,
       x = "Days since the first case",
       title = "Flattening the curve",
       subtitle = "Mitigation efforts can help to reduce the number of daily cases and to
reduce the pressure on the healthcare system\n\n\nNumber of daily case",
       caption='SOURCE:CDC')+#添加标题
  scale_x_continuous(expand=expansion(mult=0))+
  scale_y_continuous(expand=expansion(mult=0))+#使图像从坐标原点出发
  expand_limits(x=c(-10,90),y=c(0,0.05))+#限制x轴的范围
  theme(
    panel.background=element_rect(fill='white'),#背景变白
    panel.grid = element_blank(),#消除网格
    axis.ticks = element_blank(),#消除x轴的标签
    axis.text=element_blank(),
    axis.line = element_line(color='grey',size = 1,arrow=arrow(type='open',length=unit(0.3,'cm'))),#调节坐标轴的颜色并变箭头
    axis.title.y= element_text(angle=0, margin=margin(t = -20, l = 0, r = -100)),#调整y轴文本的位置
    plot.caption = element_text(hjust=0)#调整左下角文字的位置
  )

hope-data-science avatar Oct 17 '24 02:10 hope-data-science