簡體   English   中英

在R中繪制多組點

[英]Plot multiple sets of points in R

我想要繪制多組xy對。 我希望每組xy對都通過一條線連接起來。 換句話說,目標是使多個實驗實例各自通過在一個圖上繪制的線來近似。 另外,我將如何對線條進行不同的着色?

繪圖函數做我想要的,但是接受一組xy對: plot(x, y, ...)

這個功能可以采取多套或是否有其他功能?

要使用普通的plot命令執行此操作,我通常會創建一個繪圖,然后使用lines()函數添加更多行。

否則你可以使用lattice或ggplot2。 這是一些數據:

df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)

你可以使用xyplot()

library(lattice)
xyplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)

geom_line()

library(ggplot2)
ggplot(melt(df, id.vars="x"), aes(x, value, colour = variable,
        group = variable)) + geom_line() + theme_bw()

這是另一個例子,包括每對的點數(來自學習者博客上的這篇文章 ):

library(lattice)
dotplot(VADeaths, type = "o", auto.key = list(lines = TRUE,
     space = "right"), main = "Death Rates in Virginia - 1940",
     xlab = "Rate (per 1000)")

使用ggplot2的相同情節:

library(ggplot2)
p <- ggplot(melt(VADeaths), aes(value, X1, colour = X2,
             group = X2))
p + geom_point() + geom_line() + xlab("Rate (per 1000)") +
         ylab("") + opts(title = "Death Rates in Virginia - 1940")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM