stata中心化处理_带有stata第2部分自定义配色方案的covid 19可视化

stata中心化处理

This guide will cover an important, yet, under-explored part of Stata: the use of custom color schemes. In summary, we will learn how to go from this graph:

本指南将涵盖Stata的一个重要但尚未充分研究的部分:自定义配色方案的使用。 总而言之,我们将学习如何从该图开始:

to this graph which implements a matplotlib color scheme, typically used in R and Python graphs, in Stata:

该图实现了matplotlib配色方案 ,通常在Stata中用于R和Python图:

Image for post

This guide also touches upon advanced use of locals and loops which are essential for automation of tasks in Stata. Therefore, the guide assumes a basic knowledge of Stata commands. Understanding the underlying underlying logic here can also be applied to various Stata routines beyond generating graphs.

本指南还涉及高级使用局部变量和循环,这对于Stata中的任务自动化至关重要。 因此,本指南假定您具有Stata命令的基本知识。 除了生成图形外,了解此处的基本逻辑还可以应用于各种Stata例程。

This guide build on the first article where data management, folder structures, and an introduction to automation of tasks are discussed in detail. While it is highly recommended to follow the first guide to set up the data and the folders, for the sake of completeness, some basic information is repeated here.

本指南以第一篇文章为基础 ,其中详细讨论了数据管理,文件夹结构以及任务自动化的简介。 虽然强烈建议您遵循第一本指南来设置数据和文件夹,但是为了完整起见,此处重复一些基本信息。

The guide follows a specific folder structure, in order to track changes in files. This folder structure and code used in the first guide can be downloaded from Github.

该指南遵循特定的文件夹结构,以便跟踪文件中的更改。 可以从Github下载此第一指南中使用的文件夹结构和代码。

In case you are starting from scratch, create a main folder and the following five sub-folders within the root folder:

如果您是从头开始,请在根文件夹中创建一个主文件夹和以下五个子文件夹:

Image for post

This will allow you to use the code, that makes use of relative paths, given in this guide.

这将允许您使用本指南中提供的使用相对路径的代码。

The guide is split into five steps:

该指南分为五个步骤:

  • Step 1: provides a quick summary on setting up the COVID-19 dataset from Our World in Data.

    第1步 :提供有关从Our World in Data中建立COVID-19数据集的快速摘要。

  • Step 2: discusses custom graph schemes for cleaning up the layout

    步骤2 :讨论用于清理布局的自定义图形方案

  • Step 3: introduces line graphs and their various elements

    步骤3 :介绍折线图及其各种元素

  • Step 4: introduces color palettes and how to integrate them into line graphs

    第4步 :介绍调色板以及如何将它们集成到折线图中

  • Step 5: shows how the whole process of generating graphs with custom color schemes can be automated using loops and locals

    步骤5 :展示如何使用循环和局部变量自动生成带有自定义配色方案的图形的整个过程

步骤1:重新整理资料 (Step 1: A refresher on the data)

In case you are starting from scratch, start a new dofile, and set up the data using the following commands:

如果您是从头开始的,请启动一个新的文件,然后使用以下命令设置数据:

clear
cd <your main directory here with sub-folders shown above>***********************************
**** our worldindata ECDC dataset
***********************************insheet using "https://covid.ourworldindata.org/data/ecdc/full_data.csv", clear
save ./raw/full_data_raw.dta, replacegen year = substr(date,1,4)
gen month = substr(date,6,2)
gen day = substr(date,9,2)destring year month day, replace
drop date
gen date = mdy(month,day,year)
format date %tdDD-Mon-yyyy
drop year month day
gen date2 = date
order date date2drop if date2 < 21915gen group = .replace group = 1 if ///
location == "Austria" | ///
location == "Belgium" | ///
location == "Czech Republic" | ///
location == "Denmark" | ///
location == "Finland" | ///
location == "France" | ///
location == "Germany" | ///
location == "Greece" | ///
location == "Hungary" | ///
location == "Italy" | ///
location == "Ireland" | ///
location == "Netherlands" | ///
location == "Norway" | ///
location == "Poland" | ///
location == "Portugal" | ///
location == "Slovenia" | ///
location == "Slovak Republic" | ///
location == "Spain" | ///
location == "Sweden" | ///
location == "Switzerland" | ///
location == "United Kingdom"keep if group==1ren location country
tab country
compress
save "./temp/OWID_data.dta", replace**** adding the population datainsheet using "https://covid.ourworldindata.org/data/ecdc/locations.csv", clear
drop countriesandterritories population_year
ren location country
compress
save "./temp/OWID_pop.dta", replace**** merging the two datasetsuse ./temp/OWID_data, clear
merge m:1 country using ./temp/OWID_popdrop if _m!=3
drop _m***** generating population normalized variablesgen total_cases_pop = (total_cases / population) * 1000000
gen total_deaths_pop = (total_deaths / population) * 1000000
***** clean up the datedrop if date < 21960
format date %tdDD-Mon
summ date***** identify the last date
summ date
gen tick = 1 if date == `r(max)'
***** save the file
compress
save ./master/COVID_data.dta, replace

步骤2:自定义图形方案 (Step 2: Custom graph schemes)

In the first guide, we learnt how to make line graphs using the Stata’s xtline command. This was done by declaring the data to be a panel dataset. To summarize, we use the following commands to generate a panel data line graph:

在第一个指南中 ,我们学习了如何使用Stata的xtline命令制作线图。 这是通过将数据声明为面板数据集来完成的。 总而言之,我们使用以下命令来生成面板数据折线图:

summ date
local start = `r(min)'
local end = `r(max)' + 30xtline total_cases_pop ///
, overlay ///
addplot((scatter total_cases_pop date if tick==1, mcolor(black) msymbol(point) mlabel(country) mlabsize(vsmall) mlabcolor(black))) ///
ttitle("") ///
tlabel(`start'(15)`end', labsize(small) angle(vertical) grid) ///
title(COVID19 trends for European countries) ///
note(Source: ECDC via Our World in Data) ///
legend(off) ///
graphregion(fcolor(white)) ///
scheme(s2color)
graph export ./graphs/medium2_graph1.png, replace wid(1000)

which gives us this figure:

这给了我们这个数字:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值