[英]Plot two matrices into one with ggplot
我用 matplot() 將 2 個矩陣繪制成一張圖,但我沒有得到 ggplot 等效項。 以下是矩陣:
矩陣 A:足球比賽中的得分嘗試
John Mike Luc
2010 20 30 25
2011 13 22 18
2012 10 20 14
矩陣 B:游戲數量
John Mike Luc
2010 10 15 12
2011 5 8 7
2012 2 8 3
以下代碼完美運行
a <- Score_Attempts
b <- Num_Of_Games
matplot(a, b, type = "l", xlab = "Score attempt", ylab = "Number of game")
legend("bottomright", legend = colnames(a), col = seq_len(a), pch = 1, cex = 0.7)
ggplot 等效項似乎有些不對勁。 幫我把這個也弄好,謝謝!
data <- data.frame(ScoreAttempt = as.vector(a),
NumOfGame = as.vector(b))
ggplot(data, mapping = aes(x = ScoreAttempt, y = NumOfGame ))
labs(x="Score attempt", y= "Number of game") +
geom_line(size = 2)
首先,由於缺少+
符號,您的labs()
和geom_line()
沒有連接到ggplot()
。
試圖復制您的代碼如下
a <- matrix( c(20,30,25,13,22,18,10,20,14), ncol=3, byrow=TRUE )
dimnames(a) <- list( 2010:2012, c("John","Mike","Luc") )
b <- matrix( c(10,15,12,5,8,7,2,8,3), ncol=3, byrow=TRUE )
dimnames(b) <- list( 2010:2012, c("John","Mike","Luc") )
matplot(a, b, type = "l", xlab = "Score attempt", ylab = "Number of game")
legend("bottomright", legend = colnames(a), col = seq_len(a), pch = 1, cex = 0.7)
創建這個
假設上圖是正確的,你需要一些額外的數據來通過ggplot2
重新創建它
data <- data.frame( Individual=rep(c("John","Mike","Luc"),each=3),
Year=rep(2010:2012,3),
ScoreAttempt = as.vector(a),
NumOfGame = as.vector(b) )
ggplot(data, mapping = aes(x = ScoreAttempt, y = NumOfGame, color=Individual, group=Individual ) ) +
labs(x="Score attempt", y= "Number of game") +
geom_line(size = 2)
得到這個
現在你只需要微調所有的縮放來得到你想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.