Jon*_*oni 4 plot r scatter-plot ggplot2 lattice
我有一个7列(2因子,5位数)的数据框(“数据”)。第一列包含7个不同国家的名称,在接下来的列中,我收集了表征每个国家的不同参数(例如人口,GDP等)的数据。在最后一列中,因子变量指定相应国家/地区所属的大陆。
数据如下所示:
structure(list(Country = structure(c(5L, 4L, 7L, 2L, 1L, 6L,
3L), .Label = c("Brazil", "Chile", "China", "France", "Germany",
"India", "Netherlands"), class = "factor"), GDP = c(0.46, 0.57,
0.75, 0.56, 0.28, 0.88, 1), Population = c(0.18, 0.09, 0.54,
0.01, 0.02, 0.17, 0.84), Birth.rate = c(87.21, 18.34, 63.91,
14.21, 5.38, 51.19, 209.26), Income = c(43.89, 18.23, 63.91,
12.3, 0.1, 14.61, 160.82), Savings = c(43.32, 0.11, 0, 1.91,
5.29, 36.58, 50.38), Continent = structure(c(2L, 2L, 2L, 3L,
3L, 1L, 1L), .Label = c("Asia", "Europe", "South America"), class = "factor")), .Names = c("Country",
"GDP", "Population", "Birth.rate", "Income", "Savings", "Continent"
), class = "data.frame", row.names = c(NA, -7L))
Run Code Online (Sandbox Code Playgroud)
我需要某种循环函数,该函数将每列相对于彼此绘制(例如,散布图),以便最后每列(除第一列和最后一列,即两个因子变量之外)都针对所有其他列绘制,但每一列在单个绘图图中(并非所有绘图都在一个绘图中)。最好将所有这些图保存到本地计算机上的某个文件夹中。
如果已经根据相互绘制的相应两列标记了x和y轴,那将也很好。此外,在图中显示每个国家名称的每个点旁边都有一个标签会很方便。最后,根据三个不同的洲,为国家的点设置三种不同的颜色会很好。
到目前为止,我只有一段类似的代码
for (i in seq(1,length(data),1)) {
plot(data[,i], ylab=names(data[i]), xlab="Country",
text(i, labels=Country, pos=4, cex =.5))
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它仅针对第一列(“国家/地区”)绘制每一列,这最终并不是我想要的。
你有什么想法我能做到吗?谢谢!
您可以pairs()直接从R 使用。请注意,它dt代表您的数据集。
pairs(dt)
Run Code Online (Sandbox Code Playgroud)
dt <- structure(list(Country = structure(c(5L, 4L, 7L, 2L, 1L, 6L,
3L), .Label = c("Brazil", "Chile", "China", "France", "Germany",
"India", "Netherlands"), class = "factor"), GDP = c(0.46, 0.57,
0.75, 0.56, 0.28, 0.88, 1), Population = c(0.18, 0.09, 0.54,
0.01, 0.02, 0.17, 0.84), Birth.rate = c(87.21, 18.34, 63.91,
14.21, 5.38, 51.19, 209.26), Income = c(43.89, 18.23, 63.91,
12.3, 0.1, 14.61, 160.82), Savings = c(43.32, 0.11, 0, 1.91,
5.29, 36.58, 50.38), Continent = structure(c(2L, 2L, 2L, 3L,
3L, 1L, 1L), .Label = c("Asia", "Europe", "South America"), class = "factor")), .Names = c("Country",
"GDP", "Population", "Birth.rate", "Income", "Savings", "Continent"
), class = "data.frame", row.names = c(NA, -7L))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3867 次 |
| 最近记录: |