前两期分别介绍了R-ggplot2 基础散点图R-ggplot2 基础图表绘制-散点图和 Python-seaborn基础散点图Python-seaborn 基础图表绘制-散点图 的绘制方法,较为系统的介绍了绘图的基础语法,也为一些绘图基础不是很好的小伙伴提供了参考方法,基础的讲过了,接下里我们将示例应用了啊(也是这个系列推文的流程啊:基础 示例演示),只为让你更好的掌握绘图知识点。本期的推文就使用R-ggplot2进行一个较为经典的图表仿制,也是自己一直想制作的图表。主要涉及的知识点如下:
- geom_smooth()绘制拟合线
- ggrepel::geom_text_repel()绘制不重叠文本
ggplot2 ggrepel 图表再现
这期的推文绘图示例我们使用的是经济学人经典的一张图表,如下:
接下来我们就使用ggplot2 ggrepel 包进行再现,首先,我们预览下本期数据(数据都已经进过处理,也就是简单的数据替换和选择,Python的pandas包和 R的dplyr等包都可以进行处理,这里不做介绍)(部分):
已经整理成符合ggplot2绘图的数据要求,接下俩就是使用ggplot2进行绘制,具体代码如下:
代码语言:javascript复制ggplot(plot_data, aes(x = CPI, y = HDI, color = Region))
geom_point(size = 2.5, shape = 21, fill = "white", stroke = 1)
这里就得到了大致的效果图,观察参考图表,我们可以发现有些国家的名字是显示出来的,这里我们进行单独列出来:
代码语言:javascript复制country_show <- c("Russia", "Venezuela", "Iraq", "Myanmar", "Sudan",
"Afghanistan", "Congo", "Greece", "Argentina", "Brazil",
"India", "Italy", "China", "South Africa", "Spain",
"Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",
"US", "Germany", "Britain", "Barbados", "Norway", "Japan",
"New Zealand", "Singapore")
接下里,我们直接贴出绘图代码,如下:
代码语言:javascript复制ggplot(data = plot_data,aes(x=CPI,y = HDI,colour=Region))
geom_point(shape=21,size=2.5,fill="white",stroke=1.2)
geom_smooth(method = "lm",formula = y ~ log(x),se = FALSE,color = "red",size=.8,fullrange = TRUE,
aes(fill = "R^2=56%"))
geom_text_repel(aes(label = Country), color = "grey20",segment.color = "grey30",
data = subset(plot_data,Country %in% country_show),
point.padding = unit(0.03, 'npc'),
force = 1,
nudge_y = 0.015,
nudge_x = 0.0,
)
scale_colour_manual(name = NULL,values = c('#01344A','#228DBD','#6DBBD8','#1B6E64','#D24131','#621107'),
labels = c("OECD","Americas","Asia & nOceania","Central & nEastern Europe",
"Middle East & nnorth Africa", "Sub-Saharan nAfrica"))
scale_x_continuous(limits = c(1, 10), breaks = c(1:10))
scale_y_continuous(limits = c(0.2,1), breaks = c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0))
labs(
x = "Corruption Perceptions Index, 2011 (10=least corrupt)",
y = "Human Development Index, 2011 (1=best)",
title = "Corruption and human development",
subtitle = "Base charts: Scatter Exercise",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>")
theme_bw()
#设置图例
guides(colour = guide_legend(nrow = 1),
fill = guide_legend(title = ""))
theme(
legend.position = "top",
legend.justification = 0,
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
plot.title = element_text(size = 14,face = "bold",hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.title = element_text(face = 'italic', size = 8),
plot.caption = element_markdown(size = 8, hjust = 1),
panel.border = element_blank(),
axis.line.x = element_line(color = "black"),
axis.ticks.y = element_blank(),
axis.ticks.length=unit(-0.1, "cm"),
axis.text.x = element_text(margin=unit(c(0.5,0.5,0,0), "cm"))
)
可视化效果如下:
- geom_smooth()绘制拟合线
geom_smooth(method = "lm",formula = y ~ log(x),se = FALSE,color = "red",size=.8,fullrange = TRUE,
aes(fill = "R^2=56%"))
这里se=FALSE 去除拟合范围,fullrange = TRUE 是拟合线包括数据全范围,method = "lm",formula = y ~ log(x) 则表示对数线性拟合。
- geom_text_repel()添加文本
由于原图文字与散点之间的链接线设置较为合理,这里我们可以使用geom_segment()单独进行位置设置并连线,这里我们就不使用此方法,直接使用geom_text_repel()进行绘制即可(当然最终效果也不如原图):
代码语言:javascript复制 geom_text_repel(aes(label = Country), color = "grey20",segment.color = "grey30",
data = subset(plot_data,Country %in% country_show),
point.padding = unit(0.03, 'npc'),
force = 1,
nudge_y = 0.015,
nudge_x = 0.0,
)
- theme(主题)设置 这里的主题则是根据原图进行刻度、网格、背景等元素的添加,详细代码如下:
theme(
legend.position = "top",
legend.justification = 0,
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
plot.title = element_text(size = 14,face = "bold",hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.title = element_text(face = 'italic', size = 8),
plot.caption = element_markdown(size = 8, hjust = 1),
panel.border = element_blank(),
axis.line.x = element_line(color = "black"),
axis.ticks.y = element_blank(),
axis.ticks.length=unit(-0.1, "cm"),
axis.text.x = element_text(margin=unit(c(0.5,0.5,0,0), "cm"))
)
(这里虽然进行了图表一定程度上的再现,但还是存在部分细节之处没有很好的仿制,这里我么只学习绘图技巧啊)
总结
本期推文我们进行了散点示例图的绘制,主要目的还是进行图表技巧的练习,希望可以给大家提供些绘图灵感。