如何在R中使用ggplot2呈现多个图作为两组图的成员[关闭]

问题描述 投票:-2回答:2

假设我有这个数据框:

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"))
r ggplot2
2个回答
-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"))

enter image description here


0
投票

我正在寻找的答案是:

ggplot(dataframe, aes(test,value, colour = disease)) +
geom_line(aes(group = as.factor(patient)))
© www.soinside.com 2019 - 2024. All rights reserved.