【问题标题】:How to draw ggplot with the data from the frame?如何使用框架中的数据绘制 ggplot?
【发布时间】:2021-03-23 18:30:36
【问题描述】:

我收集了由 2 种均值估计组成的数据。我想画ggplot(点与线相连),在哪里

  • 在 y 轴上有平均值
  • 存在迭代(从 1 到 10)(“iteracje”)
  • 有 2 行代表 2 种颜色,我会使用不同的颜色

我是这样开始的,但我不知道为什么当我把迭代放在 x 轴上时,平均值只有一个点。下面是数据框的外观和我的代码。谢谢你的帮助!

> frame
mu_1     mu_2 iteracje
1  0.9865904 7.005866        1
2  0.9865904 7.005866        2
3  0.9865904 7.005866        3
4  0.9865904 7.005866        4
5  0.9865904 7.005866        5
6  0.9865904 7.005866        6
7  0.9865904 7.005866        7
8  0.9865904 7.005866        8
9  0.9865904 7.005866        9
10 0.9865904 7.005866       10

 frame = data.frame(mu_1, mu_2,iteracje=1:10)
 ggplot(frame,aes(iteracje, mu_1))+geom_point(aes(mu_2))

【问题讨论】:

    标签: r ggplot2 statistics scatter-plot ggplotly


    【解决方案1】:

    试试这个。关键是将数据重新整形为长然后绘制:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    #Code
    frame %>% pivot_longer(-iteracje) %>%
      ggplot(aes(x=factor(iteracje),y=value,color=name,group=name))+
      geom_point()+
      geom_line()
    

    输出:

    使用的一些数据:

    #Data
    frame <- structure(list(mu_1 = c(0.9865904, 0.9865904, 0.9865904, 0.9865904, 
    0.9865904, 0.9865904, 0.9865904, 0.9865904, 0.9865904, 0.9865904
    ), mu_2 = c(7.005866, 7.005866, 7.005866, 7.005866, 7.005866, 
    7.005866, 7.005866, 7.005866, 7.005866, 7.005866), iteracje = 1:10), class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10"))
    

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 2019-10-22
      • 2016-04-17
      • 2020-01-02
      • 2022-10-12
      • 2018-11-24
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多