Search code examples
rggplot2plotlegendsegment

Plot several segments with a partially common legend


Say that I have the following data frame:

line_df <- data.frame(x_ini = c(0.55,2.55,1.55,5.55,7.55),
x_end = c(1.45,3.45,2.45,6.45,8.45),
y = c(-0.5,-0.5,-1.5,-2.5,-2.5),
category = c("red","red","blue","green","green"))

I want to plot an horizontal segment for each row of the dataframe. The line segments would be defined like this: (x_ini[i],y[i]) and (x_end[i],y[i]) and the color of segments by line_df$category.

I have tried with geom_segment, segments, abline and lines but I can't manage to set the legend properly and plotting the segments with the same instructions because I have other data I want to include on the same plot.

Any suggestion on how could I do this? Happy to modify the data frame to other more suitable structure but I want to avoid calling the same instruction several times for each one of the segments.

Thanks!

EDIT

The other data I want to include is below.

previous_df <- data.frame(letter=c("x","y","y","z","x","p"),number=c(9,3,2,1,5,7),labeldf=c(1,15,12,15,1,12))

ggplot(previous_df,aes(labeldf,number,fill=letter)) +
    geom_bar(stat="identity",position='dodge')

So the problem is that I can't add another ggplot layer to it, and if I add directly the geom_segment (mentioned in the answer by Nova) I get errors regarding the fill option.


Solution

  • I think if you add a field for "row" you can do it - is this what you mean?

    line_df$row <- seq_len(nrow(line_df))
    
    ggplot(line_df) +
      geom_segment(aes(x = x_ini, xend = x_end, y = y, yend = y, 
                       group = row, color = category), size = 2) +
      scale_color_manual(values = c("lightblue", "seagreen", "firebrick"))
    

    enter image description here

    If you want to add data from another dataframe, you could do it like this:

    ggplot(previous_df) +
      geom_bar(aes(labeldf, number, fill=letter), stat="identity",position='dodge') +
      geom_segment(data = line_df, aes(x = x_ini, xend = x_end, y = y, yend = y, 
                                       group = row, color = category), size = 2) +
      scale_color_manual(values = c("lightblue", "seagreen", "firebrick"))
    

    enter image description here