ggplot lineplot用于多行数据-ggplot等效于matplot

时间:2018-11-21 18:18:16

标签: r matplotlib ggplot2

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

.data-icon {
    font-size: 45px;
    color: black;
    opacity: 0.5;
    display: block;
    width: 100%;
    text-align: center;
}

我已成功使用以下代码在matplot中创建所需的图:

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

哪个生成: [REE图] [1]

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

[坏处] [1]

有人能帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

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

产量 enter image description here