尽管这个问题已经“解决”了很多次,但事实证明总是存在另一个问题。
没有打印功能,它可以正常运行,但是得到以下信息:
Error in .subset2(x, i) : recursive indexing failed at level 2
我要说的是它不喜欢在两层迭代中创建图形吗?将方法更改为“ qplot(whatever:whatever)”存在完全相同的问题。
它旨在为我正在查看的每对变量打印一个图形。他们太多了,无法容纳单张图片,例如pairs函数,我需要能够在轴上看到实际的变量名称。
load("Transport_Survey.RData")
variables <- select(Transport, "InfOfReceievingWeather", "InfOfReceievingTraffic", "InfOfSeeingTraffic", "InfWeather.Ice", "InfWeather.Rain", "InfWeather.Wind", "InfWeather.Storm", "InfWeather.Snow", "InfWeather.Cold", "InfWeather.Warm", "InfWeather.DarkMorn", "InfWeather.DarkEve", "HomeParking", "WorkParking", "Disability", "Age", "CommuteFlexibility", "Gender", "PassionReduceCongest")
varnames <- list("InfOfReceivingWeather", "InfOfReceivingTraffic", "InfOfSeeingTraffic", "InfWeather.Ice", "InfWeather.Rain", "InfWeather.Wind", "InfWeather.Storm", "InfWeather.Snow", "InfWeather.Cold", "InfWeather.Warm", "InfWeather.DarkMorn", "InfWeather.DarkEve", "HomeParking", "WorkParking", "Disability", "Age", "CommuteFlexibility", "Gender", "PassionReduceCongest")
counterx = 1
countery = 1
for (a in variables) {
for (b in variables) {
print(ggplot(variables, mapping=aes(x=variables[[a]], y=variables[[b]],
xlab=varnames[counterx], ylab=varnames[countery]))+
geom_point())
countery = countery+1
counterx = counterx+1
}
}
#variables2 <- select(Transport, one_of(InfOfReceivingWeather, InfOfReceivingTraffic, InfOfSeeingTraffic, InfWeather.Ice, InfWeather.Rain, InfWeather.Wind, InfWeather.Storm, InfWeather.Snow, InfWeather.Cold, InfWeather.Warm, InfWeather.DarkMorn, InfWeather.DarkEve, HomeParking, WorkParking, Disability, Age, CommuteFlexibility, Gender, PassionReduceCongest))
这是一个供参考的小型数据框,是从我使用的列中采样的:
structure(list(InfOfReceievingWeather = c(1, 1, 1, 1, 4), InfOfReceievingTraffic = c(1,
1, 1, 1, 4), InfOfSeeingTraffic = c(1, 1, 1, 1, 4), InfWeather.Ice = c(3,
1, 3, 5, 5), InfWeather.Rain = c(1, 1, 2, 2, 4), InfWeather.Wind = c(1,
1, 2, 2, 4), InfWeather.Storm = c(1, 1, 1, 2, 5), InfWeather.Snow = c(1,
1, 2, 5, 5), InfWeather.Cold = c(1, 1, 1, 2, 5), InfWeather.Warm = c(1,
1, 1, 1, 3), InfWeather.DarkMorn = c(1, 1, 1, 1, 1), InfWeather.DarkEve = c(1,
1, 1, 1, 1), HomeParking = c(1, 1, 3, 1, 1), WorkParking = c(1,
4, 4, 5, 4), Disability = c(1, 1, 1, 1, 1), Age = c(19, 45, 35,
40, 58), CommuteFlexibility = c(2, 1, 5, 1, 2), Gender = c(2,
2, 2, 2, 1), PassionReduceCongest = c(0, 0, 2, 0, 2)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
答案 0 :(得分:1)
您在分配b
和a
时出错。基本上,当在b
中定义variables
和variables
时,它们成为aes
列中包含的值的向量。因此,在您的variables[[a]]
映射中,当您调用a
时,基本上就是在写(对于variables
中variables[[c(1, 1, 1, 1, 4)]]
的第一次迭代):
variables[["InfOfReceievingWeather"]]
,而不是for (a in variables) {
for (b in variables) {
print(ggplot(variables, mapping=aes(x=a, y=b)) ...
。因此,它不起作用。
要解决此问题,您必须选择以下一项:
for (a in 1:ncol(variables)) {
for (b in 1:ncol(variables)) {
print(ggplot(variables, mapping=aes(x=variables[[a]], y=variables[[b]])) ...
或
a
尽管第一个选项似乎更简单,但我宁愿选择第二个选项,因为它可以循环使用b
和variables
作为列指示符来提取xlab
的同名ylab
和for (a in 1:ncol(variables)) {
for (b in 1:ncol(variables)) {
print(ggplot(variables, mapping=aes(x=variables[[a]], y=variables[[b]])) +
xlab(colnames(variables)[a])+
ylab(colnames(variables)[b])+
geom_point())
}
}
。
最后,编写这样的内容应该可以:
ff = function(x, margin=c("all", "line", "column", "cell", 0, 1, 2)){
if(!is.numeric(margin)){
if(is.null(margin)) {
margin=0:2 #defaulting
} else {
marginopts = list(all = 0:2,
line = 1,
column = 2,
cell = 0)
margin <- unname(unlist(marginopts[margin]))
}
}
margin
}
ff(NULL, margin=1:2) # [1] 1 2
ff(NULL, margin="all") # [1] 0 1 2
ff(NULL, margin="cell") # [1] 0
ff(NULL, margin=c("column", "cell")) # [1] 2 0
ff(NULL, margin=NULL) # [1] 0 1 2
它回答了您的问题吗?