简体   繁体   中英

ggplot2 with points and lines with different legends

The following code

p <- ggplot()+
  geom_point(aes(v3,v2,color=factor(v1)),myData)+
  scale_colour_manual(values = gradient,name="Property A")+ 
  xlab("X Values")+
  ylab("Y Values")+
  theme(legend.position="bottom")

produce this plot with points separated by a property A 结果图 I want to connect the points sharing another property B. It should look something like this:

所需结果

I already tried adding

geom_line(aes(v3,v2,color=factor(v4)),myData)

But with this approach the line and the points will be recognized as a single object which causes a problem with the legend.

Edit: The dput() of myData

structure(list(v1 = c(1L, 1L, 1L, 2L, 2L, 2L), v2 = c(8348.97159577052, 
7681.30540986381, 6826.40361652663, 10795.9750463179, 10765.5654460646, 
9444.74166746195), v3 = c(0.349130695948487, 0.338453160875985, 
0.319725370654182, 0.621362929529391, 0.619078094211563, 0.616495725056279
), v4 = c(0.995, 0.95, 0.9, 1, 0.995, 0.99)), .Names = c("v1", 
"v2", "v3", "v4"), row.names = c(NA, 6L), class = "data.frame")

With the comment from @kitman0804 you can also add a linetype = factor(v4) to the aes()

p <- ggplot()+
  geom_point(aes(v3,v2,color=factor(v1)),myData)+
  scale_colour_manual(values = c(1,2,3,4,5,6),name="Property A")+  
  scale_linetype_discrete(name = "Property B") + 
  xlab("X Values")+
  ylab("Y Values")+
  theme(legend.position="bottom")
p + geom_line(aes(v3, v2, group=factor(v4), linetype = factor(v4)), myData)

在此处输入图片说明

UPDATE: Add annotations instead different line types

At first, you create a data.frame which contains all min (or max) values using this function. You can also adjust the x and y position of the values with the adjustx and adjusty parameters.

xyForAnnotation <- function(data, xColumn, yColumn, groupColumn, 
                            min = TRUE, max = FALSE, 
                            adjustx = 0.05, adjusty = 100) {
  xs <- c()
  ys <- c()
  is <- c()
  for (i in unique(data[ , c(as.character(groupColumn))])){
    tmp <- data[data[, c(groupColumn)] == i,]
    if (min){
      wm <- which.min(tmp[, c(xColumn)])
      tmpx <- tmp[wm, c(xColumn)]
      tmpy <- tmp[wm, c(yColumn)]
    }
    if (max){
      wm <- which.max(tmp[, c(xColumn)])
      tmpx <- data[wm, c(yColumn)]
      tmpy <- data[wm, c(yColumn)]
    }

    xs <- c(xs, tmpx)
    ys <- c(ys, tmpy)
    is <- c(is, i)
  }
  df <- data.frame(lab = is, x = xs, y = ys)
  df$x <- df$x + adjustx
  df$y <- df$y + adjusty
  df
}

df <- xyForAnnotation(myData, "v3", "v2", "v4")

p <- ggplot()+
  geom_point(aes(v3,v2,color=factor(v1)),myData)+
  scale_colour_manual(values = c(1,2,3,4,5,6, 7, 8, 9),name="Property A")+  
  scale_linetype_discrete(guide = F) + 
  xlab("X Values")+
  ylab("Y Values")+
  theme(legend.position="bottom") + 
  geom_line(aes(v3, v2, group=factor(v4)), myData)
p + annotate("text", label = as.character(df$lab), x = df$x, y = df$y)

The plot then looks like this:

在此处输入图片说明

As mentioned above, you can adjust the position of the annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM