【问题标题】:ggplot lineplot for multiple rows of data - ggplot equivalent for matplot多行数据的 ggplot 线图 - 与 matplot 等效的 ggplot
【发布时间】:2018-11-21 18:18:15
【问题描述】:

我正在尝试为每个具有 24 个测量值的样本创建一个带有一条线的图(即要绘制的数据是行而不是列)。我的数据示例如下所示:

structure(c("23.96000", "25.92000", "20.13000", "20.39000", "13.88000", 
            "14.97000", "11.56000", "12.75000", " 8.86000", "10.33000", " 8.96000", 
            " 9.87000", " 7.540000", " 8.160000", " 6.670000", " 7.430000", 
            " 7.060000", " 7.040000", " 6.250000", " 7.200000", " 6.400000", 
            " 6.380000", " 6.70000", " 6.05000", " 5.590000", " 6.310000", 
            " 6.000000", " 5.770000"), .Dim = c(2L, 14L), .Dimnames = list(
              NULL, c("La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy", 
                      "Ho", "Er", "Tm", "Yb", "Lu")))

我已经使用以下代码成功地在 matplot 中创建了我想要的情节:

m <- as.matrix(data)
REE <- c('La','Ce','Pr','Nd','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu')

m2 <- m[,11:24]

#Plotting with matplot

matplot(t(m2), type = "l", log="y", xaxt ="n",ylab="C/C_Chondrite",ylim=c(1,100))
axis(1, at=1:length(REE), labels=REE)

这会产生: [REE图][1]

我已经尝试过此示例中描述的方法:ggplot equivalent for matplot 仅使用 geom_point() 来测试功能, 但是我目前正在得到这样的情节:

[坏情节][1]

有人可以帮忙吗?

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow!请提供您的datadput(data) 以创建MCVE。谢谢!
  • ggplot 在输入长数据时功能强大,但在输入宽数据时会很笨拙。我建议您在转换它的 ggplot 之前添加一个步骤,例如tidyr::gather(element, value, La:Lu)...
  • 嗨@jay.sf 我已经改变了这个 - 谢谢你的提示!
  • @jay.sf 我刚看到 - 谢谢!

标签: r matplotlib ggplot2


【解决方案1】:

您可能需要转置数据,与链接示例相比,您的示例中的数据有所不同。

data <- as.data.frame(t(data))  # transpose your data here with `t()`
data$id <- 1:nrow(data) 
library(reshape2)
plot_data <- melt(data,id.var="id")
library(ggplot2)
ggplot(plot_data, aes(x=id, y=value, group=variable, colour=variable)) +
  geom_point() +
  geom_line(aes(lty=variable))

产量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多