Note that there are some explanatory texts on larger screens.

plurals
  1. POCustomizing legend text and colors in ggplot2
    primarykey
    data
    text
    <p>I am trying to get a plot (using ggplot) that will plot <code>geom_point</code> and <code>geom_line</code> where different variables are plotted in different colors (according to <code>scale_colour_manual(value = color)</code> where <code>color</code> is a custom array of colors) in addition to two horizontal black lines (<code>geom_hline</code>).</p> <p>My trouble arises when I attempt to get the legend text for the horizontal lines customized. It appears that I can have only one of the two:</p> <ol> <li>the color of the horizontal line as black but the legend text for this line is incorrect</li> <li>the legend text for this line is correct but the color of the horizontal line is determined by the aforementioned <code>scale_colour_manual</code> color.</li> </ol> <p></p> <pre><code>plot &lt;- ggplot(data, aes(x = factor(Month), y = avgLoss, colour = type, order = -as.numeric(type))) + geom_line() + geom_point() meanplus2sd &lt;- mean(data$avgLoss) + 2*sd(data$avgLoss) plot &lt;- plot + geom_hline(aes(yintercept = meanplus2sd), colour = "black") </code></pre> <p>produces black line that says "black" in the legend</p> <pre><code>plot &lt;- plot + geom_hline(aes(yintercept = meanplus2sd, colour = "Mean + 2 Stdev.")) </code></pre> <p>produces a line that is the next color in my defined <code>scale_colour_manual</code> array, but the legend text is <code>"Mean + 2 Stdev."</code></p> <p>Any help in getting both custom color and legend text for a horizontal line in addition to the standard <code>geom_point + geom_line</code> plotting would be excellent. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload