1

这里有两个数据集:

dd1 <- structure(list(Test = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Test A", 
"Test B", "Test C", "Test D", "Test E"), class = "factor"), Batch = structure(c(1L, 
2L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), 
    Time = c(36, 36, 36, 36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 
    18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 
    6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 
    36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 
    18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 
    6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 36, 3, 6, 12, 18, 24, 
    36, 3, 6, 12, 18, 24, 36), Result2 = c(0.015, 0.015, 0.02, 
    0.02, 6.5, 6.6, 6.5, 6.5, 6.5, 6.5, 6.5, 6.6, 6.5, 6.5, 6.5, 
    6.4, 6.5, 6.6, 6.4, 6.5, 6.6, 6.5, 6.5, 6.6, 6.5, 6.5, 6.6, 
    6.5, 0.73, 0.72, 0.72, 0.73, 0.74, 0.73, 0.73, 0.72, 0.72, 
    0.76, 0.74, 0.73, 0.68, 0.65, 0.66, 0.65, 0.68, 0.67, 0.69, 
    0.66, 0.67, 0.66, 0.67, 0.67, 99, 96, 98, 101, 98, 97, 95, 
    94, 98, 99, 100, 98, 97, 91, 101, 100, 100, 98, 96, 92, 100, 
    100, 98, 98, 99, 99, 99, 100, 98, 97, 99, 99, 99, 100, 98, 
    97, 99, 98, 99, 99, 98, 98, 99, 98, 99, 99, 97, 96), Censored = structure(c(TRUE, 
    TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
    ), .Dim = c(100L, 1L))), .Names = c("Test", "Batch", "Time", 
"Result2", "Censored"), row.names = c(147L, 335L, 523L, 711L, 
70L, 85L, 100L, 117L, 132L, 152L, 258L, 273L, 288L, 305L, 320L, 
340L, 446L, 461L, 476L, 493L, 508L, 528L, 634L, 649L, 664L, 681L, 
696L, 716L, 67L, 82L, 97L, 114L, 129L, 149L, 255L, 270L, 285L, 
302L, 317L, 337L, 443L, 458L, 473L, 490L, 505L, 525L, 631L, 646L, 
661L, 678L, 693L, 713L, 78L, 93L, 110L, 125L, 142L, 162L, 266L, 
281L, 298L, 313L, 330L, 350L, 454L, 469L, 486L, 501L, 518L, 538L, 
642L, 657L, 674L, 689L, 706L, 726L, 66L, 81L, 96L, 113L, 128L, 
148L, 254L, 269L, 284L, 301L, 316L, 336L, 442L, 457L, 472L, 489L, 
504L, 524L, 630L, 645L, 660L, 677L, 692L, 712L), class = "data.frame")
dd2 <- structure(list(Test = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Test B", "Test C", 
"Test D", "Test E"), class = "factor"), Batch = structure(c(1L, 
1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 
1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("A", 
"B", "C", "D"), class = "factor"), Result2 = c(6.5, 6.6, 6.5, 
6.6, 6.6, 6.6, 6.5, 6.6, 0.76, 0.74, 0.76, 0.75, 0.7, 0.68, 0.69, 
0.68, 100, 103, 100, 102, 99, 101, 101, 100, 99, 98, 99, 98, 
97, 98, 97, 98), Timepoint = c("1 week,4°C", "1 week,37°C", "1 week,4°C", 
"1 week,37°C", "1 week,4°C", "1 week,37°C", "1 week,4°C", "1 week,37°C", 
"1 week,4°C", "1 week,37°C", "1 week,4°C", "1 week,37°C", "1 week,4°C", 
"1 week,37°C", "1 week,4°C", "1 week,37°C", "1 week,4°C", "1 week,37°C", 
"1 week,4°C", "1 week,37°C", "1 week,4°C", "1 week,37°C", "1 week,4°C", 
"1 week,37°C", "1 week,4°C", "1 week,37°C", "1 week,4°C", "1 week,37°C", 
"1 week,4°C", "1 week,37°C", "1 week,4°C", "1 week,37°C")), .Names = c("Test", 
"Batch", "Result2", "Timepoint"), row.names = c(40L, 55L, 228L, 
243L, 416L, 431L, 604L, 619L, 37L, 52L, 225L, 240L, 413L, 428L, 
601L, 616L, 48L, 63L, 236L, 251L, 424L, 439L, 612L, 627L, 36L, 
51L, 224L, 239L, 412L, 427L, 600L, 615L), class = "data.frame")

“批处理”和“测试”列中存在常见级别:

> str(dd1)
'data.frame':   100 obs. of  5 variables:
 $ Test    : Factor w/ 5 levels "Test A","Test B",..: 1 1 1 1 2 2 2 2 2 2 ...
 $ Batch   : Factor w/ 4 levels "A","B","C","D": 1 2 3 4 1 1 1 1 1 1 ...
 $ Time    : num  36 36 36 36 3 6 12 18 24 36 ...
 $ Result2 : num  0.015 0.015 0.02 0.02 6.5 6.6 6.5 6.5 6.5 6.5 ...
 $ Censored: logi [1:100, 1] TRUE TRUE TRUE TRUE FALSE FALSE ...
> str(dd2)
'data.frame':   32 obs. of  4 variables:
 $ Test     : Factor w/ 4 levels "Test B","Test C",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ Batch    : Factor w/ 4 levels "A","B","C","D": 1 1 2 2 3 3 4 4 1 1 ...
 $ Result2  : num  6.5 6.6 6.5 6.6 6.6 6.6 6.5 6.6 0.76 0.74 ...
 $ Timepoint: chr  "1 week,4°C" "1 week,37°C" "1 week,4°C" "1 week,37°C" ...
> head(dd1)
      Test Batch Time Result2 Censored
147 Test A     A   36   0.015     TRUE
335 Test A     B   36   0.015     TRUE
523 Test A     C   36   0.020     TRUE
711 Test A     D   36   0.020     TRUE
70  Test B     A    3   6.500    FALSE
85  Test B     A    6   6.600    FALSE
> head(dd2)
      Test Batch Result2   Timepoint
40  Test B     A     6.5  1 week,4°C
55  Test B     A     6.6 1 week,37°C
228 Test B     B     6.5  1 week,4°C
243 Test B     B     6.6 1 week,37°C
416 Test B     C     6.6  1 week,4°C
431 Test B     C     6.6 1 week,37°C

第一张图是我想要的第一件事,很好:

gg <- ggplot() +
  geom_point(aes(x=Time, y=Result2, size=Censored), data=dd1) + 
  scale_x_continuous(breaks=unique(dd1$Time)) + 
  scale_size_discrete("title 1") + 
  facet_grid(Test ~ Batch, scales="free")
gg

在此处输入图像描述

但是当我添加图表的第二部分时,基于第二个数据集,第一个图例被修改(一些线通过点出现):

gg + geom_hline(aes(yintercept=Result2, colour=Timepoint), data=dd2,  
                linetype="dashed", show_guide=TRUE) + 
  scale_colour_discrete("title 2")

在此处输入图像描述

我想要这两个图例,但我不希望在添加第二个时像这样修改第一个。请问怎么办?

4

1 回答 1

2

您可以使用函数guides()并在此函数内部使用参数size设置图例。linetype=0override.aes=

+guides(size= guide_legend(override.aes = list(linetype = 0)))
于 2013-09-27T14:53:16.303 回答