![](/img/trans.png)
[英]How can I make a scatter plot of an r data frame where some cells have complex numbers?
[英]I have imported the data in R, how to make a scatter plot?
我是R
新手,並按如下方式導入我的數據集(點表示剩余數據):
> num.csv <- read.csv("c:/num.csv", header=T)
> print(num.csv)
X.Y
1 22500;482
2 25842;1
3 27221;1
4 32757;1
5 40152;1
. .
. .
. .
如何為此數據制作散點圖?
謝謝。
首先,數據需要在單獨的列中。 雖然文件標記為“csv”,但您似乎使用分號分隔而不是逗號。 重新格式化文件或嘗試:
num.csv <- read.csv("c:/num.csv", header=T, sep=";")
然后,您可以使用R中的各種繪圖包中的一個來制作繪圖。 例如:
install.packages("ggplot2"); #ggplot2 is not part of the standard install...
library(ggplot2);
qplot(X, Y, data=num.csv);
我沒有測試過上面的內容,這取決於你的數據框是如何從read.csv出來的。
@ patrickmdmnet的答案是要走的路,但我有點好奇,只是想嘗試一個程序化的解決方案。 我對R strplit()功能如何工作感到很好奇:
# Test matrix
tmp.mtrx <- matrix(c("1;2", "3;4", "5;6", "7;8"), ncol=1)
# The split
tmp.split <- strsplit(tmp.mtrx, ";")
# Put it all together into a new matrix
new_matrix <- matrix(tmp.split[[1]], ncol=2)
for(i in 2:length(tmp.split)){
new_matrix <- rbind(new_matrix, tmp.split[[i]])
}
# Do the plot originally asked for
plot(new_matrix[,1], new_matrix[,2])
@ Chl - 我正在尋找unlist函數,它使得解決方案更好,沒有循環雖然因為我已經編程了很多我經常發現如果它沒有太大的影響我的代碼更可讀更好性能。 這是Chl在一個稍微復雜的矩陣中的解決方案:
# Test matrix
tmp.mtrx <- matrix(c("1;2", 55, "3;4", 75, "5;6", 85, "7;8", 88), ncol=2)
# The split
tmp.split <- strsplit(tmp.mtrx, ";")
# A vector with all the values, length = (ncol(tmp.mtrx) + 1)*nrow(tmp.mtrx)
tmp.data_vector <- unlist(tmp.split)
# Put it all together into a new matrix
new_matrix <- matrix(tmp.data_vector, ncol=(ncol(tmp.mtrx)+1), byrow=TRUE)
# Do the plot originally asked for
plot(new_matrix[,1], new_matrix[,2])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.