【问题标题】:How to plot data of different columns in R如何在R中绘制不同列的数据
【发布时间】:2019-07-08 15:04:24
【问题描述】:

我有以下数据框:

Class   Step    1   2   3
TestMe  1       5   10  6
TestMe  2       7   11  5
TestMe  3       9   13  9
TestMe  4       11  15  10
TestMe  5       13  18  4
TestMe  6       15  20  3
TestMe  7       17  23  8
TestMe  8       19  25  11
TestMe  9       21  27  13
TestMe  10      23  30  7

我想使用 R 生成一个图,这样

图中的每一行代表一个运行(即三个运行)。我尝试了以下方法:

dataset <- dataset %>% melt(id.vars = c("Class"))

dataset <- transform(dataset, value = as.numeric(value))

YaxisTitle <- "Fitness"

pp2 <- dataset %>% ggplot(aes(x=variable, y=value, group=Class, colour=Class)) +
  geom_line() +
  scale_x_discrete(breaks = seq(0, 10, 1)) +
  labs(x = as.character(dataset$Class), y = YaxisTitle) +
  theme(text = element_text(size=10),legend.position="none")

但我得到以下信息:

我该如何解决这个问题?

【问题讨论】:

  • 您能否使用dput(df) 共享您的数据,以便人们可以复制错误并提供可能的解决方案
  • 你的melt 函数不能正常工作,使用这个:dataset &lt;- dataset %&gt;% gather("key", "value", -Step, -Class); ggplot(dataset, aes(x=Step, y=value, color=key)) + geom_line()

标签: r ggplot2 reshape


【解决方案1】:

我继续让您的数据可重现。我不确定我的答案是否正是您想要的,但从您的数据来看,这对我来说似乎是最明显的。

library(tidyverse)

dataset <- tribble(
  ~Class,   ~Step,  ~"1", ~"2", ~"3",
  "TestMe",  1,       5,   10,  6,
  "TestMe",  2,       7,   11,  5,
  "TestMe",  3,       9,   13,  9,
  "TestMe",  4,       11,  15,  10,
  "TestMe",  5,       13,  18,  4,
  "TestMe",  6,       15,  20,  3,
  "TestMe",  7,       17,  23,  8,
  "TestMe",  8,       19,  25,  11,
  "TestMe",  9,       21,  27,  13,
  "TestMe",  10,      23,  30,  7,
)

YaxisTitle <- "Fitness"

dataset <- dataset %>%
  gather("1", "2", "3", key = "variable", value = "value") 

  ggplot(dataset, aes(x=Step, y=value, group=variable, colour=variable)) +
  geom_line() +
  scale_x_discrete(breaks = seq(0, 10, 1)) +
  labs(x = as.character(dataset$Class), y = YaxisTitle) +
  theme(text = element_text(size=10),legend.position="none")

【讨论】:

    【解决方案2】:

    您的melt 有问题。请参阅以下示例以重新塑造数据框和绘图。

    library(tidyverse)
    
    dataset <- read.table(text = "Class   Step    1   2   3
    TestMe  1       5   10  6
    TestMe  2       7   11  5
    TestMe  3       9   13  9
    TestMe  4       11  15  10
    TestMe  5       13  18  4
    TestMe  6       15  20  3
    TestMe  7       17  23  8
    TestMe  8       19  25  11
    TestMe  9       21  27  13
    TestMe  10      23  30  7",
                          header = TRUE, stringsAsFactors = FALSE)
    
    
    dataset <- dataset %>% 
      gather(Variable, Value, starts_with("X"))
    
    YaxisTitle <- "Fitness"
    
    dataset %>% 
      ggplot(aes(x = Step, y = Value, group = Variable, colour = Variable)) +
      geom_line() +
      scale_x_discrete(breaks = seq(0, 10, 1)) +
      scale_color_manual(values = c("X1" = "Blue", "X2" = "Red", "X3" = "Gold")) +
      labs(x = as.character(dataset$Class), y = YaxisTitle) +
      theme_minimal() +
      theme(text = element_text(size=10),legend.position="none") 
    

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 2014-09-08
      • 2019-08-05
      相关资源
      最近更新 更多