假设我有此数据框:
Patient Test1 Test2 Test3 Disease
1 10 12 10 no
2 12 2 13 yes
3 15 15 18 yes
4 8 9 10 no
5 7 8 7 no
现在,我希望创建此数据的图。显示患者1,4和5的行应全部为蓝色,其余应为红色。
这是我当时想要的最佳答案:
library(reshape2)
library(ggplot2)
mydata <- read.delim("test.txt")
m_mydata <- melt(mydata,id=c("Patient","Disease"))
ggplot(m_mydata, aes(x = variable, y = value, group = Patient, colour = Disease)) +
geom_line() + scale_color_manual(values=c("blue", "red"))
答案 0 :(得分:0)
我正在寻找的答案是:
ggplot(dataframe, aes(test,value, colour = disease)) +
geom_line(aes(group = as.factor(patient)))
答案 1 :(得分:-2)
首先,您需要将数据从短格式转换为长格式:
library(reshape2)
library(tidyverse)
df %>%
melt(id.vars = c("Patient", "Disease")) %>%
rename(Tests = variable,
Values = value)
Patient Disease Tests Values
1 1 0 Test1 10
2 2 1 Test1 12
3 3 1 Test1 15
4 4 0 Test1 8
5 5 0 Test1 7
6 1 0 Test2 12
7 2 1 Test2 12
8 3 1 Test2 15
9 4 0 Test2 9
在此步骤之后,您可以对其进行绘制,其中X轴用Test1,Test2,...,Test1000和Y轴表示,并带有相应的值。此外,它按患者分组:
df %>%
melt(id.vars = c("Patient", "Disease")) %>%
rename(Tests = variable,
Values = value) %>%
ggplot(aes(x = Tests, y = Values, color = as.factor(Disease))) +
geom_line(aes(group = as.factor(Patient))) + scale_color_manual(values=c("#000000", "#ff000c"))