如何设置排序数据进行绘图?
How to set a sorted data to plot?
我正在尝试创建两个数据的散点图,但我不知道如何将我的排序结果指定给散点图。
程序是这样的:
- 阅读“data_to_be_chosen.csv”
- 阅读“data_to_be_plotted.csv”
- 对“data_to_be_chosen.csv”进行排序以找到两个最上面的值(及其名称)
- 在“data_to_be_plotted.csv”
中找到对应的names/columns
- 显示两者的散点图
我在第 5 步遇到问题。
假设 Column C
和 Column A
具有两个最高值。
如果我手动将数据设置到绘图中,它将是:
plot(plotted$C, plotted$A)
但是,我希望它根据排序顺序自动完成。
我认为以下代码可以工作:
plot(plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1])],
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2])])
但是,这给了我一个错误:
Error in stripchart.default(x1, ...) : invalid plotting method
我也试过这些,但它们也不起作用:
plot(names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
plot(colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
有什么方法可以将排序后的结果设置为这个图吗?
我没有更多的想法。
我的R版本是4.1.2(最新版)。
这是我的数据:
data_to_be_chosen.csv
A,B,C
2.044281,0.757232,2.188617
data_to_be_plotted.csv
A,B,C
0.34503,-0.38781,-0.3506
0.351566,-0.3901,-0.35244
0.351817,-0.39144,-0.35435
0.351222,-0.39138,-0.35394
0.351222,-0.39113,-0.35366
0.350753,-0.39088,-0.35291
0.350628,-0.39041,-0.3531
0.349127,-0.3881,-0.3511
0.346125,-0.38675,-0.34969
0.346594,-0.38719,-0.34963
这是我的代码:
plotted <- read.csv("data_to_be_plotted.csv")
chosen <- read.csv("data_to_be_chosen.csv")
chosen_list <- list(chosen=chosen)
sort(chosen_list$chosen[1,], decreasing=TRUE)[1:2]
names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1:2])
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1:2])]
# Correlation can be calculated with the above data frame
cor(plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1])],
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2])])
# What I want is this plot ... except manually specifying C or A
plot(plotted$C, plotted$A)
# The above data frame can NOT be used to plot / Issues "invalid plotting method"
plot(plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1])],
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2])])
# I also tried, but no luck:
plot(names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
plot(colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
您可以使用 unlist
将 single-row “data_to_be_chosen” df 强制转换为命名向量;然后排序,得到前两个名字,并用它们索引到“data_to_be_plotted”:
chosen_vec <- unlist(chosen)
plot(plotted[names(sort(chosen_vec, decreasing = TRUE)[1:2])])
问题只是对一行进行子集化会得到一个 "data.frame"
对象而不是一个 "numeric"
向量,这可能是您所期望的。您可以使用 class()
.
检查
class(chosen_list$chosen[1,])
# [1] "data.frame"
解决办法就是unlist
了。
class(unlist(chosen_list$chosen[1,]))
# [1] "numeric"
下面使用创建对象而不是重复代码。
(x <- sort(unlist(chosen_list$chosen[1,]), decreasing=TRUE)[1:2])
# C A
# 2.188617 2.044281
(nx <- names(x))
# [1] "C" "A"
(p_df <- plotted[nx])
# C A
# 1 -0.35060 0.345030
# 2 -0.35244 0.351566
# 3 -0.35435 0.351817
# 4 -0.35394 0.351222
# 5 -0.35366 0.351222
# 6 -0.35291 0.350753
# 7 -0.35310 0.350628
# 8 -0.35110 0.349127
# 9 -0.34969 0.346125
# 10 -0.34963 0.346594
要将数据框的两个向量的cor
关系作为单个值,我们可能需要[, j]
,因为数据框有两个维度。
cor(p_df[, 1], p_df[, 2])
# [1] -0.9029339
检查(同上):
class(cor(p_df[1], p_df[2]))
# [1] "matrix" "array"
class(cor(p_df[, 1], p_df[, 2]))
# [1] "numeric"
我建议您更新有关 indexing using brackets 的知识。
或者只获取相关矩阵
cor(p_df)
# C A
# C 1.0000000 -0.9029339
# A -0.9029339 1.0000000
最后使用其中之一:
plot(plotted[, nx[1]], plotted[, nx[2]])
plot(p_df[1:2])
plot(p_df) ## works in this special case, because we only have two columns
数据:
plotted <- structure(list(A = c(0.34503, 0.351566, 0.351817, 0.351222, 0.351222,
0.350753, 0.350628, 0.349127, 0.346125, 0.346594), B = c(-0.38781,
-0.3901, -0.39144, -0.39138, -0.39113, -0.39088, -0.39041, -0.3881,
-0.38675, -0.38719), C = c(-0.3506, -0.35244, -0.35435, -0.35394,
-0.35366, -0.35291, -0.3531, -0.3511, -0.34969, -0.34963)), class = "data.frame", row.names = c(NA,
-10L))
chosen <- structure(list(A = 2.044281, B = 0.757232, C = 2.188617), class = "data.frame", row.names = c(NA,
-1L))
chosen_list <- list(chosen=chosen)
我正在尝试创建两个数据的散点图,但我不知道如何将我的排序结果指定给散点图。
程序是这样的:
- 阅读“data_to_be_chosen.csv”
- 阅读“data_to_be_plotted.csv”
- 对“data_to_be_chosen.csv”进行排序以找到两个最上面的值(及其名称)
- 在“data_to_be_plotted.csv” 中找到对应的names/columns
- 显示两者的散点图
我在第 5 步遇到问题。
假设 Column C
和 Column A
具有两个最高值。
如果我手动将数据设置到绘图中,它将是:
plot(plotted$C, plotted$A)
但是,我希望它根据排序顺序自动完成。
我认为以下代码可以工作:
plot(plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1])],
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2])])
但是,这给了我一个错误:
Error in stripchart.default(x1, ...) : invalid plotting method
我也试过这些,但它们也不起作用:
plot(names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
plot(colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
有什么方法可以将排序后的结果设置为这个图吗?
我没有更多的想法。
我的R版本是4.1.2(最新版)。
这是我的数据:
data_to_be_chosen.csv
A,B,C
2.044281,0.757232,2.188617
data_to_be_plotted.csv
A,B,C
0.34503,-0.38781,-0.3506
0.351566,-0.3901,-0.35244
0.351817,-0.39144,-0.35435
0.351222,-0.39138,-0.35394
0.351222,-0.39113,-0.35366
0.350753,-0.39088,-0.35291
0.350628,-0.39041,-0.3531
0.349127,-0.3881,-0.3511
0.346125,-0.38675,-0.34969
0.346594,-0.38719,-0.34963
这是我的代码:
plotted <- read.csv("data_to_be_plotted.csv")
chosen <- read.csv("data_to_be_chosen.csv")
chosen_list <- list(chosen=chosen)
sort(chosen_list$chosen[1,], decreasing=TRUE)[1:2]
names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1:2])
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1:2])]
# Correlation can be calculated with the above data frame
cor(plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1])],
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2])])
# What I want is this plot ... except manually specifying C or A
plot(plotted$C, plotted$A)
# The above data frame can NOT be used to plot / Issues "invalid plotting method"
plot(plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1])],
plotted[names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2])])
# I also tried, but no luck:
plot(names(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
names(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
plot(colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[1]),
colnames(sort(chosen_list$chosen[1,], decreasing=TRUE)[2]))
您可以使用 unlist
将 single-row “data_to_be_chosen” df 强制转换为命名向量;然后排序,得到前两个名字,并用它们索引到“data_to_be_plotted”:
chosen_vec <- unlist(chosen)
plot(plotted[names(sort(chosen_vec, decreasing = TRUE)[1:2])])
问题只是对一行进行子集化会得到一个 "data.frame"
对象而不是一个 "numeric"
向量,这可能是您所期望的。您可以使用 class()
.
class(chosen_list$chosen[1,])
# [1] "data.frame"
解决办法就是unlist
了。
class(unlist(chosen_list$chosen[1,]))
# [1] "numeric"
下面使用创建对象而不是重复代码。
(x <- sort(unlist(chosen_list$chosen[1,]), decreasing=TRUE)[1:2])
# C A
# 2.188617 2.044281
(nx <- names(x))
# [1] "C" "A"
(p_df <- plotted[nx])
# C A
# 1 -0.35060 0.345030
# 2 -0.35244 0.351566
# 3 -0.35435 0.351817
# 4 -0.35394 0.351222
# 5 -0.35366 0.351222
# 6 -0.35291 0.350753
# 7 -0.35310 0.350628
# 8 -0.35110 0.349127
# 9 -0.34969 0.346125
# 10 -0.34963 0.346594
要将数据框的两个向量的cor
关系作为单个值,我们可能需要[, j]
,因为数据框有两个维度。
cor(p_df[, 1], p_df[, 2])
# [1] -0.9029339
检查(同上):
class(cor(p_df[1], p_df[2]))
# [1] "matrix" "array"
class(cor(p_df[, 1], p_df[, 2]))
# [1] "numeric"
我建议您更新有关 indexing using brackets 的知识。
或者只获取相关矩阵
cor(p_df)
# C A
# C 1.0000000 -0.9029339
# A -0.9029339 1.0000000
最后使用其中之一:
plot(plotted[, nx[1]], plotted[, nx[2]])
plot(p_df[1:2])
plot(p_df) ## works in this special case, because we only have two columns
数据:
plotted <- structure(list(A = c(0.34503, 0.351566, 0.351817, 0.351222, 0.351222,
0.350753, 0.350628, 0.349127, 0.346125, 0.346594), B = c(-0.38781,
-0.3901, -0.39144, -0.39138, -0.39113, -0.39088, -0.39041, -0.3881,
-0.38675, -0.38719), C = c(-0.3506, -0.35244, -0.35435, -0.35394,
-0.35366, -0.35291, -0.3531, -0.3511, -0.34969, -0.34963)), class = "data.frame", row.names = c(NA,
-10L))
chosen <- structure(list(A = 2.044281, B = 0.757232, C = 2.188617), class = "data.frame", row.names = c(NA,
-1L))
chosen_list <- list(chosen=chosen)