【问题标题】:Plot difference in values of multiple variables in a dataframe绘制数据框中多个变量值的差异
【发布时间】:2019-11-04 01:45:01
【问题描述】:

我有数据框 DF 显示不同学生在两个测试中的表现,test1test2

DF <- data.frame(student = factor(c("S1", "S2", "S3", "S4", "S5")), 
                 test1 = factor(c(90, 77, 45, 67, 80)),
                 test2 = factor(c(70, 75, 55, 64, 80)))

我想随着时间的推移绘制学生的表现,即在一个看起来像这样的图中跨越两个测试,但是用student而不是series和测试而不是time(所以(test1和test2 `,而不是 x 轴上的数字 2 和 4):

我该怎么做?

【问题讨论】:

    标签: r dataframe ggplot2


    【解决方案1】:

    比较分数随时间变化的一种方法是将数据转换为长格式并并排创建条形图。

    library(ggplot2)
    
    tidyr::pivot_longer(DF, cols = -student) %>%
       ggplot() + aes(x = student, y = as.numeric(as.character(value)), fill = name) +
       geom_bar(stat = "identity", width = 0.4, position = "dodge")
    

    也许你正在寻找这个

    tidyr::pivot_longer(DF, cols = -student) %>%
      ggplot() + aes(x = name, y = as.numeric(as.character(value)), 
                group = student, color = student) +
      geom_line()
    

    【讨论】:

    • 谢谢。这样做的目的是显示个别学生的表现如何变化,但要理解测试之间更广泛的趋势,这是我想要展示的,在这里比在我原来的问题中显示的情节更难。
    • @KaC 这显示了student performance over time,即问题中提到的不同测试。这不是你想要的吗?
    • 我想要的是类似于原始问题中的情节。
    • @KaC 更新了答案。你能检查一下这是不是你想要的吗?
    • 这正是我想要的,@ronak-shah。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2022-07-05
    相关资源
    最近更新 更多