写在前面。
R
、Python
以及SAS
等这些工具,学会了基础的编程语法
后,可视化也是很重要的一部分。
但是也没必要专门再去学习每种图形的具体画法,甚至于去记忆代码。
本文介绍一些工具,可以获取这几种编程语言画具体图形的代码
。
主要内容为:
- R语言的The R Graph Gallery
- SAS的
ODS图形设计器
- Python的The Python Graph Gallery 和pyecharts
以下逐个介绍。
R
R Base
语法和Tidyvers
的语法还是存在差异的,喜欢专注于哪个就使用哪个。
更多的应该专注于解决具体的问题
,而R
的可视化,还是首推ggplot2
,它也是Tidyvers
集合包中的成员。
掌握了语法后,具体图形的画法可以从The R Graph Gallery网站中去寻找,代码都是可以直接复制的。
The R Graph Gallery
网站链接如下,The R Graph Gallery可以直接点击进入主页。

想找具体的图形的画法,可以点击图中的红框。

以画条形图为例。

可以看到,各种风格的图形都可以直接找到绘制的示例代码, 代码可以直接赋值。以红框的为例;

需要加载什么包
,使用什么示例数据
,代码和绘制的图
都有展示和说明;
- 加载所需的包以及数据预处理
# Library
library(ggplot2)
library(dplyr)
# Dataset 1: one value per group
data <- data.frame(
name=c("north","south","south-east","north-west","south-west","north-east","west","east"),
val=sample(seq(1,10), 8 )
)
# Dataset 2: several values per group (natively provided in R)
# mpg
- 图形绘制的代码
# load the library
library(forcats)
# Reorder following the value of another column:
data %>%
mutate(name = fct_reorder(name, val)) %>%
ggplot( aes(x=name, y=val)) +
geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) +
coord_flip() +
xlab("") +
theme_bw()
# Reverse side
data %>%
mutate(name = fct_reorder(name, desc(val))) %>%
ggplot( aes(x=name, y=val)) +
geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) +
coord_flip() +
xlab("") +
theme_bw()
网站完全是免费的,内容和界面也很简洁。
唯一的一个缺点可能是Google
广告吧,也容易解决,建议你安装AdGuard
插件,直接edge
插件商店安装即可。

SAS
SAS
也有类似的这种可以直接找到具体图形绘制代码的地方,那就是 ODS图形设计器
。
SAS ODS图形设计器
打开SAS
后,工具栏中的工具
,点击红框中的ODS图形设计器
即可。

然后就会进入SAS ODS Graphics Designer
窗口,还是以绘制条形图为例
。

直接点击红框,确定
。

可以一路确定
即可。

然后就可以示例数据出图。可以通过点击工具栏的视图
,代码
。

直接复制代码到编辑框,按照你的需求个性化修改相应的参数即可。

proc template;
define statgraph sgdesign;
dynamic _SEX _HEIGHT;
begingraph;
entrytitle halign=center '键入标题...';
entryfootnote halign=left '键入脚注...';
layout lattice / rowdatarange=data columndatarange=data rowgutter=10 columngutter=10;
layout overlay / xaxisopts=( discreteopts=( tickvaluefitpolicy=splitrotate));
barchart category=_SEX response=_HEIGHT / name='bar' stat=mean groupdisplay=Cluster clusterwidth=1.0;
endlayout;
endlayout;
endgraph;
end;
run;
proc sgrender data=SASHELP.CLASS template=sgdesign;
dynamic _SEX="SEX" _HEIGHT="HEIGHT";
run;
Python
和R
一样,Python
的语法学习后,可视化具体图形的学习,也有可以直接复制代码的网站。
The Python Graph Gallery
网站链接如下,The Python Graph Gallery,可以直接点击进入主页。

想找具体的图形的画法,可以点击图中的红框。

以画条形图为例。可以直接找到绘制图形的示例代码。

# Libraries
import numpy as np
import matplotlib.pyplot as plt
# Make a random dataset:
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))
# Create bars
plt.bar(y_pos, height)
# Create names on the x-axis
plt.xticks(y_pos, bars)
# Show graphic
plt.show()
pyecharts
python
还有另一个类似的网站,网站链接如下,pyecharts,直接点击就可以进到主页;

从安装到各种图形的绘制,教程很完善
,并且还是中文界面
。

同样以绘制条形图为例,在左侧基础图表中点击选取即可。可以直接找到绘制图形的示例代码。

下面我只是复制了一段代码,并不完整。
from pyecharts import Bar
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图数据堆叠示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.render()
要讲的就这么多,最后,其实我想感慨一下,我们开源和分享的环境确实不太好,最后的尽头不是带货就是卖课,也很无奈。
只希望,我可以一直坚持下去,分享
,做知识的搬运工
。
以上。
网友评论