plot will not alternate colors, line types or widths - will only give me first kind even though I use c(kind1, kind2, kind3) ex.

problem

I'm trying to make a step-down survival plot using type = "S". I'm plotting a survival curve for every treatment (8 treatments) and each treatment should have its own style, a combination of colors, line widths, and line types. When I designate a style for each line, every line populates in the first style given. For example, when I say "lty = c(3,3,1,1,3,3,1,1)" what I get is a graph that looks like I typed "lty = 3". Please help! :slight_smile:

data

structure(list(X = c(1L, 2L, 3L, 4L, 5L, 6L, 24L, 25L, 26L, 27L,
28L, 29L, 47L, 48L, 49L, 50L, 51L, 52L, 70L, 71L, 72L, 73L, 74L,
75L, 93L, 94L, 95L, 96L, 97L, 98L, 116L, 117L, 118L, 119L, 120L,
121L, 139L, 140L, 141L, 142L, 143L, 144L, 162L, 163L, 164L, 165L,
166L, 167L), trt = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L,
6L, 6L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("broomlivedrought",
"broomlivewet", "broomsteriledrought", "broomsterilewet", "uninvadedlivedrought",
"uninvadedlivewet", "uninvadedsteriledrought", "uninvadedsterilewet"
), class = "factor"), day = c(0L, 13L, 14L, 15L, 17L, 20L,
0L, 13L, 14L, 15L, 17L, 20L, 0L, 13L, 14L, 15L, 17L, 20L, 0L,
13L, 14L, 15L, 17L, 20L, 0L, 13L, 14L, 15L, 17L, 20L, 0L, 13L,
14L, 15L, 17L, 20L, 0L, 13L, 14L, 15L, 17L, 20L, 0L, 13L, 14L,
15L, 17L, 20L), totalsurv = c(87L, 87L, 84L, 83L, 82L, 79L, 89L,
89L, 89L, 89L, 88L, 86L, 88L, 88L, 88L, 88L, 88L, 88L, 89L, 89L,
89L, 89L, 89L, 89L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L,
86L, 86L, 86L, 89L, 89L, 89L, 89L, 89L, 89L, 90L, 90L, 90L, 90L,
90L, 90L), totalplants = c(87L, 87L, 87L, 87L, 87L, 87L, 89L,
89L, 89L, 89L, 89L, 89L, 88L, 88L, 88L, 88L, 88L, 88L, 89L, 89L,
89L, 89L, 89L, 89L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L,
86L, 86L, 86L, 89L, 89L, 89L, 89L, 89L, 89L, 90L, 90L, 90L, 90L,
90L, 90L), percentsurv = c(100, 100, 96.551724137931, 95.4022988505747,
94.2528735632184, 90.8045977011494, 100, 100, 100, 100, 98.876404494382,
96.6292134831461, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 24L, 25L, 26L, 27L,
28L, 29L, 47L, 48L, 49L, 50L, 51L, 52L, 70L, 71L, 72L, 73L, 74L,
75L, 93L, 94L, 95L, 96L, 97L, 98L, 116L, 117L, 118L, 119L, 120L,
121L, 139L, 140L, 141L, 142L, 143L, 144L, 162L, 163L, 164L, 165L,
166L, 167L), class = "data.frame")

code

plot(dfz$percentsurv ~ dfz$dayssurv,
     col = c(rep("darkgoldenrod1",4),rep("chartreuse4",4)),
     lty = c(3,3,1,1,3,3,1,1),
     lwd = c(2,1,2,1,2,1,2,1),
     type = "S"
     )

Will this work for you?

col = c(rep("darkgoldenrod1",4),rep("chartreuse4",4))
lty = c(3,3,1,1,3,3,1,1)
lwd = c(2,1,2,1,2,1,2,1)
for(i in 1:8) {
  tmp <- dfz[(((i-1)*6)+1):(i*6),]
  if (i == 1) {
    plot(tmp$percentsurv ~ tmp$day,
         col = col[i],
         lty = lty[i],
         lwd = lwd[i],
         type = "S"
    )
  } else {
    lines(tmp$percentsurv ~ tmp$day,
          col = col[i],
          lty = lty[i],
          lwd = lwd[i],
          type = "S"
    )
    
  }
}
1 Like

Thank you! Unfortunately, this didn't work. I'll try to figure out the logic here so I can tweak it around.

Here is what I get with your data. It seems to match the data.

dfz <- structure(list(X = c(1L, 2L, 3L, 4L, 5L, 6L, 24L, 25L, 26L, 27L,
                     28L, 29L, 47L, 48L, 49L, 50L, 51L, 52L, 70L, 71L, 72L, 73L, 74L,
                     75L, 93L, 94L, 95L, 96L, 97L, 98L, 116L, 117L, 118L, 119L, 120L,
                     121L, 139L, 140L, 141L, 142L, 143L, 144L, 162L, 163L, 164L, 165L,
                     166L, 167L), 
                     trt = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
                                       3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L,
                                      5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L,
                                      6L, 6L, 8L, 8L, 8L, 8L, 8L, 8L), 
                                     .Label = c("broomlivedrought","broomlivewet", "broomsteriledrought", "broomsterilewet", "uninvadedlivedrought",
                                                "uninvadedlivewet", "uninvadedsteriledrought", "uninvadedsterilewet"), 
                                     class = "factor"), 
                     day = c(0L, 13L, 14L, 15L, 17L, 20L,0L, 13L, 14L, 15L, 17L, 20L, 0L, 13L, 14L, 15L, 17L, 20L, 0L,
                              13L, 14L, 15L, 17L, 20L, 0L, 13L, 14L, 15L, 17L, 20L, 0L, 13L,
                              14L, 15L, 17L, 20L, 0L, 13L, 14L, 15L, 17L, 20L, 0L, 13L, 14L,
                              15L, 17L, 20L), 
                     totalsurv = c(87L, 87L, 84L, 83L, 82L, 79L, 89L,
                                  89L, 89L, 89L, 88L, 86L, 88L, 88L, 88L, 88L, 88L, 88L, 89L, 89L,
                                  89L, 89L, 89L, 89L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L,
                                  86L, 86L, 86L, 89L, 89L, 89L, 89L, 89L, 89L, 90L, 90L, 90L, 90L,
                                  90L, 90L), 
                     totalplants = c(87L, 87L, 87L, 87L, 87L, 87L, 89L,
                                    89L, 89L, 89L, 89L, 89L, 88L, 88L, 88L, 88L, 88L, 88L, 89L, 89L,
                                    89L, 89L, 89L, 89L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L,
                                    86L, 86L, 86L, 89L, 89L, 89L, 89L, 89L, 89L, 90L, 90L, 90L, 90L,
                                    90L, 90L), 
                     percentsurv = c(100, 100, 96.551724137931, 95.4022988505747,
                                     94.2528735632184, 90.8045977011494, 100, 100, 100, 100, 98.876404494382,
                                     96.6292134831461, 100, 100, 100, 100, 100, 100, 100, 100, 100,
                                     100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
                                     100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
                                     100)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 24L, 25L, 26L, 27L,
                                      28L, 29L, 47L, 48L, 49L, 50L, 51L, 52L, 70L, 71L, 72L, 73L, 74L,
                                      75L, 93L, 94L, 95L, 96L, 97L, 98L, 116L, 117L, 118L, 119L, 120L,
                                      121L, 139L, 140L, 141L, 142L, 143L, 144L, 162L, 163L, 164L, 165L,
                                      166L, 167L), class = "data.frame")



col = c(rep("darkgoldenrod1",4),rep("chartreuse4",4))
lty = c(3,3,1,1,3,3,1,1)
lwd = c(2,1,2,1,2,1,2,1)
for(i in 1:8) {
  tmp <- dfz[(((i-1)*6)+1):(i*6),]
  if (i == 1) {
    plot(tmp$percentsurv ~ tmp$day,
         col = col[i],
         lty = lty[i],
         lwd = lwd[i],
         type = "S"
    )
  } else {
    lines(tmp$percentsurv ~ tmp$day,
          col = col[i],
          lty = lty[i],
          lwd = lwd[i],
          type = "S"
    )
    
  }
}

Created on 2020-12-16 by the reprex package (v0.3.0)

1 Like

It worked beautifully (the example was a subset so I replaced the 6's with the actual # of observations). Thank you so much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.