set.seed(3)
mydat <- data.frame(ref = rnorm(5), mars = rnorm(5), saturn = rnorm(5), time = c(0, 0.5, 1, 1.5, 2))
> mydat
ref mars saturn time
1 0.9006247 0.70551551 0.7865069 0.0
2 0.8517704 1.30035799 -0.3104631 0.5
3 0.7277152 0.03825201 1.6988848 1.0
4 0.7365021 -0.97928377 -0.7945937 1.5
5 -0.3521296 0.79376123 0.3484377 2.0
我有一個名為 的資料集mydat,我想在其中用對角線繪制以下圖ref。然后對于marsand saturn,我想用不同的顏色和符號來繪制它們。我可以plot很好地做到這一點。
plot(mydat$ref, mydat$ref, type = "l",
xlab = "Reference", ylab = "Actual measurement", ylim = c(-1, 2))
points(mydat$ref, mydat$mars, pch = 15, col = "blue")
points(mydat$ref, mydat$saturn, pch = 18, col = "green")

我想把它畫在ggplot. 但是,我嘗試了以下但無法獲得相同的情節:
library(ggplot2)
library(reshape2)
melt_mydat <- reshape2::melt(mydat, id.vars = "time")
ggplot(data = melt_mydat, mapping = aes(x = value, y = value, color = as.factor(variable), group = as.factor(variable))) geom_point()
uj5u.com熱心網友回復:
使用時您只需要保留ref它自己的列melt(即指定為其中之一id.vars)。然后我們可以將它的值用于圖中的 x 軸:
set.seed(3)
mydat <- data.frame(ref = rnorm(5), mars = rnorm(5), saturn = rnorm(5), time = c(0, 0.5, 1, 1.5, 2))
library(ggplot2)
library(reshape2)
melt_mydat <- reshape2::melt(mydat, id.vars = c("time","ref"))
ggplot(data = melt_mydat, aes(x = ref, y = value, color = variable))
geom_point()
geom_abline(slope = 1)
scale_color_manual(values = c("blue", "green"))
labs(x = "Reference", y = "Actual measurement")
uj5u.com熱心網友回復:
也許你想要這樣的東西:
library(ggplot2)
ggplot(mydat, aes(x = ref))
geom_point(aes(y = mars), color = "blue")
geom_point(aes(y = saturn), color = "green")
geom_line(aes(y = ref))
labs(x = "Reference", y = "Actual measurement")
theme_minimal()
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466723.html
下一篇:散點線組合圖
