r - 如何在R中绘制不同列的数据
问题描述
我有以下数据框:
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")
但我得到以下信息:
我该如何解决?
解决方案
我继续使您的数据可重现。我不确定我的答案是否正是您想要的,但从您的数据来看,这对我来说似乎是最明显的。
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")
推荐阅读
- javascript - 如何编写可重用和模块化的 Redux 选择器?
- mysql - 如何通过聚合和标记来编写a?
- python - 如何在 Selenium for Python 上由 .click() 打开的新选项卡上使用 driver.current_url
- php - 无法在 html 表单中获取 id 值
- odata - 更正 UI5 中 Edm.Int16 数据类型的 filterOperator
- javascript - 错误:必须使用解构状态分配
- python - sklearn predict() 函数对于逐行预测来说太慢了
- c# - NReco.ImageGenerator 不显示图像和样式
- sql-server - SQL Server 中是否有类似预处理器指令的东西,尤其是#DEFINE?
- python-3.x - 如何重新启动硒网络驱动程序?(Python)