【发布时间】:2017-10-25 21:48:07
【问题描述】:
我有一个来自世界银行的数据集,其中包含一些连续变量和分类变量。
> head(nationsCombImputed)
iso3c iso2c country year.x life_expect population birth_rate neonat_mortal_rate region
1 ABW AW Aruba 2014 75.45 103441 10.1 2.4 Latin America & Caribbean
2 AFG AF Afghanistan 2014 60.37 31627506 34.2 36.1 South Asia
3 AGO AO Angola 2014 52.27 24227524 45.5 49.6 Sub-Saharan Africa
4 ALB AL Albania 2014 77.83 2893654 13.4 6.5 Europe & Central Asia
5 AND AD Andorra 2014 70.07 72786 20.9 1.5 Europe & Central Asia
6 ARE AE United Arab Emirates 2014 77.37 9086139 10.8 3.6 Middle East & North Africa
income gdp_percap.x log_pop
1 High income 47008.83 5.014693
2 Low income 1942.48 7.500065
3 Lower middle income 7327.38 7.384309
4 Upper middle income 11307.55 6.461447
5 High income 30482.64 4.862048
6 High income 67239.00 6.958379
我希望使用 ggpairs 在散点图中绘制一些连续变量(life_expect、birth_rate、neonat_mortal_rate、gdp_percap.x),但我想使用数据中的区域分类变量为它们着色。我尝试了许多不同的方法,但如果不包括分类变量,我就无法为连续变量着色。
ggpairs(nationsCombImputed[,c(2,5,7,8,9,11)],
title="Scatterplot of Variables",
mapping = ggplot2::aes(color = region),
labeller = "iso2c")
但我得到了这个错误
stop_if_high_cardinality 中的错误(数据、列、 cardinality_threshold) :列 'iso2c' 的级别 (211) 多于 允许的阈值 (15)。请删除该列或增加 “cardinality_threshold”参数。增加 cardinality_threshold 可能会产生较长的处理时间
最终,我想要一个 4x4 散点图,由区域着色的连续变量和数据点标签使用第 2 列中的 iso2c 代码。
这在 ggpairs 中可行吗?
嗯,是的,这是可能的!根据@Robin Gertenbach 的建议,我在代码中添加了 columns 参数,效果很好,请参见下文。
ggpairs(nationsCombImputed,
title="Scatterplot of Variables",
columns = c(5,7,8,11),
mapping=ggplot2::aes(colour = region))
我仍然希望使用 iso2c 列将数据点标签添加到散点图中,但我正在为此苦苦挣扎,任何指针都将不胜感激。
【问题讨论】:
-
ggpairs 有一个
columns参数,将子集向量移到那里,你就会得到你想要的 -
@RobinGertenbach,感谢您的指导。我得到了着色方面的工作,但仍然难以处理散点图中的标记点。关于如何使用数据中的 iso2c 列标记散点图中的点有什么建议吗?我已根据您的建议更新了 OP。