Ggplot: multiple lines for one color / class

I am trying to build multiple lines as part of a single "class" in ggplot2. I can build something like the following:

image with 5 classes

But my problem is that I want to show n1, n2 and n3 as one class (gray thin lines for each with the same name in the legend).

My problem is that if I create a new factor in the data frame to group them, I end up drawing an additional connection from the end of n1 (top right) to the beginning of n2 (bottom left), which adds a diagonal in my graph:

graph with one class

This is closer to what I want, but has an extra diagonal line. If that matters, I compute them using ROCR, then extract the x and y points from the resulting object performanceto create these rows.

+5
source share
2 answers

Here is one way, but I don’t think you will like it very much:

d1 <- data.frame(x = 1:10,
                 y = rep(1:2,each = 5),
                 grp = factor(rep(letters[1:2],each = 5),levels = letters[1:3]))
d1 <- rbind(d1,data.frame(x = 1:2,y = c(NA,NA),grp = c('c','c')))

d2 <- data.frame(x = 1:15,
                 y = rep(3:5,each = 5),
                 grp = rep(1:3,each = 5))   

ggplot() + 
    geom_line(data = d1,aes(x = x,y = y,group = grp,colour = grp)) + 
    geom_line(data = d2,aes(x = x,y = y,group = grp),colour = "blue")

enter image description here

Please note that your solution will not work with other types of data. It just happens that each of the three lines that you want to combine into one and the same category in the legend starts and ends basically with the same spot. If this were not the case, you would end up with unwanted trunk lines.

The above method will work more generally, but, as you can see, is no less inconvenient. If anything is more uncomfortable.

, , . , ggplot2, , (, ) .

+1

rbind 'd ROCR performance data.frame . , , . .

n1 <- getPerformance(test1)
n2 <- getPerformance(test2)
n3 <- getPerformance(test3)

,

  type x           y
1   n1 0 0.000000000
2   n1 0 0.003448276
3   n1 0 0.006896552
4   n1 0 0.010344828
5   n1 0 0.013793103
6   n1 0 0.017241379
...
26565   n1 0.9999619 1
26566   n1 1.0000000 1

, n1 n2, n1 (1,1) n2 (0,0) - .

() , , .

n2_rev <- n2[nrow(n2):1,]
ns <- rbind(n1,n2_rev,n3)

, , , :

desired graph

, , . ggplot2, , lines() R?

0

All Articles