【问题标题】:Plot multiple sets of points in R在 R 中绘制多组点
【发布时间】:2010-12-24 21:58:07
【问题描述】:

我想要绘制多组 xy 对。我希望每组 xy 对通过一条线连接。换句话说,我们的目标是拥有多个实验实例,每个实例都由绘制在一个图上的一条线来近似。另外,我将如何为线条着色?

plot 函数可以满足我的要求,但需要一组 xy 对: plot(x, y, ...)

这个函数可以做多组还是有另一个函数呢?

【问题讨论】:

    标签: r statistics plot


    【解决方案1】:

    要使用普通的绘图命令执行此操作,我通常会创建一个绘图,然后使用 lines() 函数添加更多行。

    否则你可以使用 lattice 或 ggplot2。以下是一些数据:

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

    您可以使用 lattice 中的xyplot()

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

    或者geom_line()在ggplot2中:

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

    这是另一个例子,包括每对的点(来自this post on the learnr blog):

    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")
    

    【讨论】:

    • 我收到一个错误:错误:找不到函数“dotplot”我需要 CRAN 提供的东西才能使用它吗?
    • 好的,我现在看到库调用了。感谢您的回答!
    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多