使用两个变量的R散射图循环
我正在尝试做一个for循环,以自动生成约50个散点图,比较两组数据。这是一个质量控制分析,所以我看了两次(重复)分析的地球化学值。所以我有一个53个元素(周期表元素)的列表,标记为Al1,Ag1,Au1。。。。。还有一份53人的名单,标签是Al2、Ag2……等等 我已经成功地让我的循环用于生成只需要一个变量的图形,x轴是固定的,如下所示使用两个变量的R散射图循环,r,loops,R,Loops,我正在尝试做一个for循环,以自动生成约50个散点图,比较两组数据。这是一个质量控制分析,所以我看了两次(重复)分析的地球化学值。所以我有一个53个元素(周期表元素)的列表,标记为Al1,Ag1,Au1。。。。。还有一份53人的名单,标签是Al2、Ag2……等等 我已经成功地让我的循环用于生成只需要一个变量的图形,x轴是固定的,如下所示 for(i in colNames){ plt <- ggplot(YGS_Dupes, mapping = aes_string(x=Dup_Num
for(i in colNames){
plt <- ggplot(YGS_Dupes, mapping = aes_string(x=Dup_Num, y = i)) +
geom_bar() + theme_calc() + ggtitle(paste(i, "Duplicate Comparison", sep=" -
"))
print(plt)
ggsave(paste0(i,".png"))
Sys.sleep(2)
}
看起来是这样的:
铁与铁散射图
所以我做了一组类似的colNames组,如下所示:
colNames_scatter_dup <- names(YGS_Dup_Scatter)[4:56]
colNames_scatter_dup2 <- names(YGS_Dup_Scatter)[57:109]
试试这个:
For (i in 1:length(colNames_scatter_dup)){
print(ggplot(YGS_Dup_Scatter, mapping = aes(x = YGS_Dup_Scatter[,names(YGS_Dup_Scatter) %in% colNames_scatter_dup[i]], y = YGS_Dup_Scatter[,names(YGS_Dup_Scatter) %in% colNames_scatter_dup2[i]]))+geom_point())
}
考虑Map
(包装到mapply
)这是一个迭代函数,用于在等长列表之间以元素方式运行,并将输出保存到列表中。这样做,您可以避免使用嵌套的for
循环方法所看到的无关循环
# EXTRACT NEEDED NAMES
samples1 <- names(YGS_Dupes)[grep("1$", names(YGS_Dupes))][-1] # -1 TO REMOVE Dupe_Code.1
samples2 <- names(YGS_Dupes)[grep("2$", names(YGS_Dupes))]
# SET UP LOOPING FUNCTION
plot_fct <- function(s1, s2) {
s_title <- gsub("1", "", s1)
p <- ggplot(YGS_Dupes, aes_string(x=s1, y=s2)) + geom_point(color="#0072B2") +
ggtitle(paste(s_title, "Duplicate Comparison", sep=" - ")) +
theme(plot.title = element_text(hjust = 0.5), legend.position="top",
axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))
ggsave(paste0(s_title,".png"))
return(p)
}
# BUILD LIST LOOPING ELEMENTWISE
plot_list2 <- Map(plot_fct, samples1, samples2)
# OUTPUT PLOTS BY NAME
plot_list2$Ag_ppb1
plot_list2$As_ppm1
plot_list2$Au_ppb1
#提取所需名称
样本1好的,谢谢你的帮助,与同事讨论你的答案让我到达了我需要的地方
最终结果如下:
YGS_Dup_Scatter = read.csv(file.choose(), header=TRUE, sep=",")
colNames_scatter_dup <- names(YGS_Dup_Scatter)[4:56]
colNames_scatter_dup2 <- names(YGS_Dup_Scatter)[57:109]
for (j in 1:length(colNames_scatter_dup)) {
plt <- ggplot(YGS_Dup_Scatter, mapping = aes_string(x=colNames_scatter_dup[j], y =colNames_scatter_dup2[j])) +
geom_point() + theme_calc() + ggtitle(paste(colNames_scatter_dup[j], "Duplicate Comparison", sep=" - ")) + theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 16), axis.text.x = element_text(face = "bold", size = "14"), axis.text.y = element_text(face = "bold", size = "12"), plot.margin = margin(10, 30, 2, 2), axis.title.y=element_text(face = "bold", size = "14"), plot.background = element_rect(fill = "lightskyblue2"))
print(plt)
ggsave(paste0(i,".png"))
Sys.sleep(2)
}
YGS\u Dup\u Scatter=read.csv(file.choose(),header=TRUE,sep=“,”)
colNames_scatter_dup作为使用for循环绘制散点图的一般解决方案,您可以使用以下流程
步骤1:创建打印功能
在下面的代码中,我显式地提供了dataframe和x轴目标变量。对于y轴上的变量,我在函数中传递列号,以便稍后运行For循环
sct_plot_function <- function(dataset = car.c2.num, target_x = car.c2.num$price, target_y_num){
ggplot(dataset, aes(x = target_x, y = car.c2.num[,target_y_num])) +
geom_point() +
geom_smooth(level = 0.95) +
theme_bw() +
labs(title = paste("Scatter plot of Price Vs ", colnames(car.c2.num)[target_y_num]), y = colnames(car.c2.num)[target_y_num], x = "Price") +
theme(plot.title = element_text(hjust = 0.5))
}
您可以将其用作重新定义多个x轴和y轴的基本结构。
如果要分别在x轴和y轴上绘制所有变量组合,可以进一步使用嵌套for循环
示例图像:
汽车数据集UCI的价格与压缩比散点图
在寻求帮助时,您应该提供一个简单的示例输入和所需输出,用于测试和验证可能的解决方案。取出与问题没有直接关系的部分代码(例如theme()
stuff)。我删除了所有的主题信息,但似乎无法使dput()正常工作。是否可以将csv附加到问题中?或者它的一小部分?您只需要复制并粘贴运行dput(YGS\u Dup\u Scatter)
@Avery\u Tellig的输出:这些可能很有用&感谢您花时间制作,这绝对是可视化比较数据的一种有趣方式。不幸的是,关键的要求是使图形成为相互之间的直接散点图。然而,我能够让它与大约99%的额外图形一起工作,如编辑中所示。所以现在的麻烦是让它只制作我想要的图形。感谢使用Map
在两个列表之间进行元素级迭代来查看更新。我几乎发布了一个by
版本,但为了避免混淆,没有发布。
# EXTRACT NEEDED NAMES
samples1 <- names(YGS_Dupes)[grep("1$", names(YGS_Dupes))][-1] # -1 TO REMOVE Dupe_Code.1
samples2 <- names(YGS_Dupes)[grep("2$", names(YGS_Dupes))]
# SET UP LOOPING FUNCTION
plot_fct <- function(s1, s2) {
s_title <- gsub("1", "", s1)
p <- ggplot(YGS_Dupes, aes_string(x=s1, y=s2)) + geom_point(color="#0072B2") +
ggtitle(paste(s_title, "Duplicate Comparison", sep=" - ")) +
theme(plot.title = element_text(hjust = 0.5), legend.position="top",
axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))
ggsave(paste0(s_title,".png"))
return(p)
}
# BUILD LIST LOOPING ELEMENTWISE
plot_list2 <- Map(plot_fct, samples1, samples2)
# OUTPUT PLOTS BY NAME
plot_list2$Ag_ppb1
plot_list2$As_ppm1
plot_list2$Au_ppb1
YGS_Dup_Scatter = read.csv(file.choose(), header=TRUE, sep=",")
colNames_scatter_dup <- names(YGS_Dup_Scatter)[4:56]
colNames_scatter_dup2 <- names(YGS_Dup_Scatter)[57:109]
for (j in 1:length(colNames_scatter_dup)) {
plt <- ggplot(YGS_Dup_Scatter, mapping = aes_string(x=colNames_scatter_dup[j], y =colNames_scatter_dup2[j])) +
geom_point() + theme_calc() + ggtitle(paste(colNames_scatter_dup[j], "Duplicate Comparison", sep=" - ")) + theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 16), axis.text.x = element_text(face = "bold", size = "14"), axis.text.y = element_text(face = "bold", size = "12"), plot.margin = margin(10, 30, 2, 2), axis.title.y=element_text(face = "bold", size = "14"), plot.background = element_rect(fill = "lightskyblue2"))
print(plt)
ggsave(paste0(i,".png"))
Sys.sleep(2)
}
sct_plot_function <- function(dataset = car.c2.num, target_x = car.c2.num$price, target_y_num){
ggplot(dataset, aes(x = target_x, y = car.c2.num[,target_y_num])) +
geom_point() +
geom_smooth(level = 0.95) +
theme_bw() +
labs(title = paste("Scatter plot of Price Vs ", colnames(car.c2.num)[target_y_num]), y = colnames(car.c2.num)[target_y_num], x = "Price") +
theme(plot.title = element_text(hjust = 0.5))
}
for(i in 1:(dim(car.c2.num)[2] - 1) ){
plot(sct_plot_function(target_y_num = i))
}