8
Tôi có khung dữ liệu như thế này:Làm thế nào để tạo biểu đồ phân tán 4x4 sử dụng GGPLOT Facet
library(ggplot2)
d.405 <- data.frame(abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),type="405")
d.409 <- data.frame(abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),abs(rnorm(30)),type="409")
all <- rbind(d.405,d.409)
colnames(all) <- c("401","402","403","404","type");
all
Những gì tôi muốn làm là để tạo ra một âm mưu 4x4 sử dụng GGPLOT khía cạnh. mà nó trông như thế này:
cách tốt nhất để đi về nó là gì?
Tôi đang mắc kẹt với đoạn mã sau:
library("reshape2");
library(plyr)
allM <- melt(all, id.vars = "type")
allList <- split(allM$value, interaction(allM$type, allM$variable))
allComb <- unlist(lapply(c(1, 3), function(x) lapply(c(2 ,4), function(y) do.call(cbind,allList[c(x,y)]))), recursive=FALSE)
allNew <- do.call(rbind,
lapply(allComb, function(x) {
tmp <- as.data.frame(x)
tmp <- (within(tmp, {xval<-names(tmp)[1];
yval<-names(tmp)[2]}))
names(tmp)[1:2] <- c("405","409")
tmp}))
head(allNew)
p <- ggplot(allNew, aes(x = 405, y = 409)) + geom_smooth(method = "lm") + geom_point() + facet_grid(yval ~ xval)
# Calculate correlation for each group
cors <- ddply(allNew, .(yval, xval), summarise, cor = round(cor(405, 409), 2))
p + geom_text(data=cors, aes(label=paste("r=", cor,sep="")), x=0.5, y=0.5)
p
Tôi giả sử bạn có một thiếu 'tất cả <- rbind (d.405, d.409) '? – alexwhan
Đúng. Đã cập nhật. Cảm ơn. – neversaint