楼主: mmuokay
1696 0

[程序分享] Recreate New York Times Cancer Graph using SAS—应用SAS来制作纽约时报风格的图表 [推广有奖]

  • 1关注
  • 7粉丝

大专生

83%

还不是VIP/贵宾

-

威望
0
论坛币
14051 个
通用积分
0
学术水平
21 点
热心指数
21 点
信用等级
21 点
经验
1647 点
帖子
32
精华
0
在线时间
94 小时
注册时间
2014-6-5
最后登录
2023-8-22

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

This New York Times Cancer Graph is nice piece of work.


times_cancer.gif

The original graph is in :

http://www.nytimes.com/imagepages/2007/07/29/health/29cancer.graph.web.html

I tried to reproduce this graph using a common statistical tool SAS.

Here is my code, the code is very heavy, so any suggestions of simplification will be appreciated.


/* 导入数据并对数据进行调整 */
proc import datafile = "E:\Web\SAS\NYT_PLOTS\nyt_plot.csv" out = nyt_plot
dbms = csv
replace;
getnames = yes;
run;

data _null_;
set nyt_plot;
call symput("nb",_n_);
call symput(cats("cancer",_n_),cancer);
run;

data nyt_plot;
set nyt_plot;
id = _n_;
format men_cases comma9.
men_deaths comma9.
women_cases comma9.
women_deaths comma9.;
run;

proc sort data = nyt_plot; by descending id; run;

/* 应该SAS Graph Template Language进行制图 */

Proc Template;
Define statgraph nyt_design;

/* Dynamic变量的作用类似宏变量 */

Dynamic xvar yvar1 yvar2 yvar3 yvar4 title footnote;

/* Title与footnote的书写,对于文字的大小,颜色等属性都可以使用textattrs来进行设置*/

Begingraph / designwidth = 800px designheight = 800px border = false borderattrs = (thickness = 3px);
Entrytitle title / textattrs=(color=black family = "arial" style=italic size=12pt weight = bold) border=false;
Entryfootnote halign = left footnote / backgroundcolor = grey border = true opaque = true
textattrs=(color=black family = "arial" style=italic size=8pt) pad =(bottom =5) ;

/* First Row */

/* 第一行的制作,这里使用的是layout lattice,将Graph进行了分割,并设置了各个模块,这里可以看到是2行3列 */
/* 第一行中,主要将报纸的名字,发行日期等书写上 */

Layout Lattice / Rows = 2 Columns = 3 Rowweights = (.08 .92) Columnweights = (.4 .2 .4) ;
Layout overlay;
Entry halign = left "The New York Times" / textattrs = (family = "old english text mt" size = 12pt weight= bold );
Endlayout;
Layout overlay;
Entry "" ;
Endlayout;
Layout overlay;
Entry halign = right "July 29, 2007" / textattrs=(weight =bold family = "arial");
Endlayout;

/* Second Row */

/* 第二行,涉及的就是图中的水平柱形图*/

/* Left Barchart */
Layout overlay / walldisplay = (fill) wallcolor = white
x2axisopts=(reverse = true griddisplay = on gridattrs = (pattern = 2 color = CXD2D2D2) display=(tickvalues)
tickvalueattrs =( family = "arial") labelattrs = (family = "arial")
linearopts=(tickvaluesequence =(start = 50000 end = 200000 increment = 50000)))
y2axisopts = ( display = (line) );
Drawtext textattrs=(family = "verdana" size=9pt weight =bold color = CX000000) "MEN" /
width = 30 xspace = wallpercent yspace = wallpercent x = 100 y = 101.8 justify = right;
Entry halign = left "NEW CASES" / valign = top pad = (top = 20px left = 40px) textattrs=(family="arial" color = CX737167);
Entry "DEATHS" / valign = top pad = (top = 60px left = 0px) textattrs=(family="arial" color = CX737167);
Entry halign = left "Leading Causes" / valign =center textattrs=(family = "calibri" color = CX211E0D size = 16pt weight=bold);
Entry halign = left "of Cancer Deaths" / valign = center pad = (top = 40px) textattrs=(family = "calibri" color = CX211E0D size = 16pt weight=bold);
Entry halign = left "This year, there will be more" / valign = center pad = (top = 85px) textattrs=(family = "calibri" color = CX211E0D size = 12pt);
Entry halign = left "than 1.4 milllon new cases" / valign = center pad = (top = 115px) textattrs=(family = "calibri" color = CX211E0D size = 12pt );
Entry halign = left "of cancer in United States," / valign = center pad = (top = 145px) textattrs=(family = "calibri" color = CX211E0D size = 12pt );
Entry halign = left "and 559,650 deaths" / valign = center pad = (top = 175px) textattrs=(family = "calibri" color = CX211E0D size = 12pt );
Entry halign = left "At right, the 12 cancers that" / valign = center pad = (top = 205px) textattrs=(family = "calibri" color = CX211E0D size = 12pt );
Entry halign = left "will claim the greatest number" / valign = center pad = (top = 235px) textattrs=(family = "calibri" color = CX211E0D size = 12pt);
Entry halign = left "of lives in 2007." / valign = center pad = (top = 265px) textattrs=(family = "calibri" color = CX211E0D size = 12pt);
Barchart x = xvar y = yvar1 / xaxis = x2 yaxis = y2 orient = horizontal display = (fill) fillattrs=(color=CXDBE3C2);
Barchart x = xvar y = yvar3 / xaxis = x2 yaxis = y2 orient = horizontal barwidth=0.5 display = (fill) fillattrs=(color=CX7E895F) ;
Drawline x1=120 x2=155 y1 = 95 y2=95 / lineattrs=(thickness = 0.5 color=CX737167)
x1space=wallpixel y1space = wallpercent
x2space=wallpixel y2space = wallpercent;
Drawline x1=180 x2=200 y1 = 89 y2=89 / lineattrs=(thickness = 0.5 color=CX737167)
x1space=wallpixel y1space = wallpercent
x2space=wallpixel y2space = wallpercent;
Drawline x1=65 x2=65 y1 = 573 y2=600 / lineattrs=(thickness = 0.5 color=CX737167)
x1space=wallpercent y1space = wallpixel
x2space=wallpercent y2space = wallpixel;
Endlayout;
/* Cancer Name */
Layout overlay;
%Macro test;
%do i = 1 %to &nb;
Entry halign=center "&&cancer&i" / valign=top pad=(top=%eval(46+(&i-1)*53)px) textattrs = (family ="monotype sans wt j" size = 11pt);
%end;
%Mend;
%test;
Endlayout;
/* Right Barchart */
Layout overlay / walldisplay = (fill) wallcolor = white
x2axisopts=(griddisplay = on gridattrs = (pattern = 2 color = CXD2D2D2) display=(tickvalues)
tickvalueattrs =( family = "arial") labelattrs = (family = "arial")
linearopts=(tickvaluesequence =(start = 50000 end = 150000 increment = 50000)))
yaxisopts = (display = (line) );
Drawtext textattrs=(family = "verdana" size=9pt weight = bold color = CX000000) "WOMEN" /
width = 30 xspace = wallpercent yspace = wallpercent x = 0 y = 101.8 justify=left;
Entry "NEW CASES" / valign = top pad = (top = 20px left = 170px) textattrs=(family="arial" color = CX737167);
Entry "DEATHS" / valign = top pad = (top = 60px left = 0px) textattrs=(family="arial" color = CX737167);
Barchart x = xvar y = yvar2 / xaxis = x2 orient = horizontal display = (fill) fillattrs=(color = CXD8E2E1);
Barchart x = xvar y = yvar4 / xaxis = x2 orient = horizontal barwidth=0.5 display = (fill) fillattrs=(color=CX6D7D72) ;
Drawline x1=195 x2=160 y1 = 95 y2=95 / lineattrs=(thickness = 0.5 color=CX737167)
x1space=wallpixel y1space = wallpercent
x2space=wallpixel y2space = wallpercent;
Drawline x1=120 x2=100 y1 = 89 y2=89 / lineattrs=(thickness = 0.5 color=CX737167)
x1space=wallpixel y1space = wallpercent
x2space=wallpixel y2space = wallpercent;
Drawline x1=32.5 x2=32.5 y1 = 573 y2=600 / lineattrs=(thickness = 0.5 color=CX737167)
x1space=wallpercent y1space = wallpixel
x2space=wallpercent y2space = wallpixel;
Endlayout;
Endlayout;
Endgraph;
end;
Run;

Proc Sgrender Data = nyt_plot Template = nyt_design;
Dynamic yvar1="men_cases" yvar2 = "women_cases" yvar3 = "men_deaths" yvar4 = "women_deaths" xvar="cancer"
title="Recreating The New York Times Cancer Graph" footnote = "Version 1.0";
Run;

Also you can download the data and code from dropbox : https://www.dropbox.com/sh/kz9b1x7je5mux4g/LMrLhZhPDZ
Author Blog:http://blog.data2biz.org/
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Create Cancer Using Times creat 纽约时报 replace cancer import null

要论坛币加 wx mediabeta
您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 05:06