-1

I'd like to know HOW I can edit the legend title. It were really hard to control the name displayed in geom_vline, and yet it is wrong since somehow it's change the legend of color scheme.

I'm using ggplot from tidyverse.

Thanks in advance

ggplot(mtcars, aes(x=disp)) +  geom_density()  +   
    geom_vline(aes(xintercept=140, color =  (green = "p" ) ) )   +      
    geom_vline(aes(xintercept=300, color =  (blue  = 's') ) )   +  
    geom_vline(aes(xintercept=250, color =  (red  =  'm'   ) ) )

enter image description here

Aureliano Guedes
  • 767
  • 6
  • 22
  • 2
    Take a look at this: https://stackoverflow.com/questions/14622421/how-to-change-legend-title-in-ggplot?rq=1 – divibisan Aug 22 '18 at 20:56
  • 2
    This is one of the situations where you want to work within the `ggplot` paradigm of using data in long format and assigning visual aesthetics to variables. If you reshape the data to have a column of intercepts and assign colors correspondingly, you'll have a legend that makes more sense. You need to set a scale in order to specify the exact colors – camille Aug 22 '18 at 21:00
  • 2
    I'd recommend [the `aes` section](http://r4ds.had.co.nz/data-visualisation.html#aesthetic-mappings) of R for Data Science as a good primer – camille Aug 22 '18 at 21:02

1 Answers1

0

I solved it in a very annoying way.

I had to create a new dataframe just to control theses parameters.

For me, it has no much sense since I want to fully control in a non hard way the line type, line color, guild's legend and line's legend.
The way that I did not allow me control colors and for a parameter like vline or hline would be nice if it could be build up the plot directly inserting all parameters.

cuts1 <- data.frame(Ref="p", vals=c(140))
cuts2 <- data.frame(Ref="s", vals=c(300))
cuts3 <- data.frame(Ref="m", vals=c(250))
cuts <- rbind(cuts1, cuts2, cuts3)

ggplot(mtcars, aes(x=disp)) +
   geom_density() + xlim(0,3000)  +
   geom_vline(data = cuts , aes(xintercept=vals, color= Ref) )

enter image description here

Aureliano Guedes
  • 767
  • 6
  • 22