admin管理员组

文章数量:1122846

It is quite ambarassing but I cannot get it to work, having performed countless such plots in the past. The dataframe reads

   ID TIME ENDPOINT TRT    SEX AGE   WT
1   1    0     65.6   A   Male  33 85.0
2   1   24     64.9   A   Male  33 85.0
3   1  168     67.6   A   Male  33 85.0
4   1  336     58.1   A   Male  33 85.0
5   1  672     69.4   A   Male  33 85.0
6   1  840     58.4   A   Male  33 85.0
7   2    0     74.3   A Female  34 77.5
8   2   24     67.9   A Female  34 77.5
9   2  168     57.9   A Female  34 77.5
10  2  336     63.4   A Female  34 77.5
11  2  672     54.4   A Female  34 77.5
12  2  840     70.5   A Female  34 77.5
13 11    0     73.0   B   Male  33 73.9
14 11   24     90.9   B   Male  33 73.9
15 11  168     83.6   B   Male  33 73.9
16 11  336     79.9   B   Male  33 73.9
17 11  672     79.6   B   Male  33 73.9
18 11  840     84.4   B   Male  33 73.9

> str(DF)
'data.frame':   18 obs. of  7 variables:
 $ ID      : int  1 1 1 1 1 1 2 2 2 2 ...
 $ TIME    : int  0 24 168 336 672 840 0 24 168 336 ...
 $ ENDPOINT: num  65.6 64.9 67.6 58.1 69.4 58.4 74.3 67.9 57.9 63.4 ...
 $ TRT     : chr  "A" "A" "A" "A" ...
 $ SEX     : chr  "Male" "Male" "Male" "Male" ...
 $ AGE     : int  33 33 33 33 33 33 34 34 34 34 ...
 $ WT      : num  85 85 85 85 85 85 77.5 77.5 77.5 77.5 ...

the code read

 DF <- read.csv(paste0(fileName,'.csv'), header = T)

p = ggplot(data = DF, aes(x=TIME, y=ENDPOINT, group=factor(ID), colour=factor(ID))) +
  theme_grey(base_size = 16) +
  geom_line(linewidth = 2)
print(p)

and produces correct plot

but when using TRT as colour variable

p = ggplot(data = DF, aes(x=TIME, y=ENDPOINT, group=factor(TRT), colour=factor(TRT))) +
  theme_grey(base_size = 16) +
  geom_line(linewidth = 2)
print(p)

is produces

What do I do wrong? UPDATE: Stefan has provided the solution

本文标签: rbasic multiple subject time series ggplot fails using factorsStack Overflow