1

I have a plot with three ECDFs and three vertical lines, and cannot get the stat_ecdf legend line color to match the actual line color.

Here is the code for just the ECDF plot:

set.seed(124)
allDTI <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))
meanALLDTI <- ddply(allDTI, "type", summarise, values.mean=mean(values, na.rm=TRUE), n)


ggplot(allDTI, aes(x=values, color=type)) + 
  stat_ecdf(size=1, show_guide=T) + 
  xlab(expression('Index Value')) + 
  ylab("Cumulative Density") +
  ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  theme(text = element_text(size=20),
        plot.title = element_text(size=30,face="bold",vjust=1),
        axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
        axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
        legend.position = c(0.85, 0.25),
        legend.text.align = 0,
        legend.box = 'horizontal',
        legend.margin = unit(45.0, 'line'),
        legend.text=element_text(size=28,vjust=0,hjust=0),
        legend.key.height = unit(1.5, 'line'),
        legend.key.width = unit(1.5, 'line'),
        panel.background = element_rect(fill = "white")) + 
  scale_color_manual(values=c('grey10','grey30','grey50'),
                     labels=c(expression(TI[d]),expression(TI[l]),
                              expression(TI[w]))) +
  guides(color = guide_legend(title=NULL))

Here is the plot:

enter image description here

But I can't figure out how to add the vertical lines I want to add in the manner I want to add them:

Code:

   ggplot(allDTI, aes(x=values, color=type)) + 
      stat_ecdf(size=1, show_guide=T) + 
      xlab(expression('Index Value')) + 
      ylab("Cumulative Density") +
      ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
      scale_x_continuous(expand = c(0,0)) +
      scale_y_continuous(expand = c(0,0)) +
      theme(text = element_text(size=20),
            plot.title = element_text(size=30,face="bold",vjust=1),
            axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
            axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
            legend.position = c(0.85, 0.25),
            legend.text.align = 0,
            legend.box = 'horizontal',
            legend.margin = unit(45.0, 'line'),
            legend.text=element_text(size=28,vjust=0,hjust=0),
            legend.key.height = unit(1.5, 'line'),
            legend.key.width = unit(1.5, 'line'),
            panel.background = element_rect(fill = "white")) + 
      scale_color_manual(values=c('grey10','grey30','grey50'),
                         labels=c(expression(TI[d]),expression(TI[l]),
                                  expression(TI[w]))) +
      guides(color = guide_legend(title=NULL)) + 
      geom_vline(data=meanALLDTI, aes(xintercept=values.mean,  colour=type),
                 linetype="dashed", size=1, show_guide=T) + 
      scale_linetype_manual(values=c('grey10','grey30','grey50'),
                         labels=c(expression(bar(TI[d])),expression(bar(TI[l])),
                                  expression(bar(TI[w])))) +
      guides(linetype= guide_legend(title=NULL))

Plot: enter image description here

As an example of what I want, here is how it came out for me with density plots:

enter image description here

traggatmot
  • 1,423
  • 5
  • 26
  • 51
  • For removing the horizontal lines, you can use `panel.grid.major.y = element_blank()` and `panel.grid.minor.y = element_blank()`. For better styling the legend, see [this example](http://stackoverflow.com/a/33237559/2204410) – Jaap Nov 02 '15 at 08:04
  • 1
    Furthermore: it's always good to provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This makes it a lot easier for others to help you. – Jaap Nov 02 '15 at 08:05
  • I'm not talking about the horizontal lines in the data frame. But the horizontal lines in the legend. Also, I've been over that example a dozen times and can't figure out what to do. Also, If I new how to easily fill data frames with random data I would. – traggatmot Nov 02 '15 at 10:32
  • Added code to make it reproducible. – traggatmot Nov 02 '15 at 10:41
  • i was having internet problems and just added the reproducible part. Please check it and see. You're code still doesn't produce the final graph I want, which is demonstrated at the end of my edited post. Obviously, it is a different plot, but it shows the double legend, with legend title, and each plot style have the correct colors and symbols and labels. I really appreciate this help, so please stay with me a little longer. – traggatmot Nov 02 '15 at 11:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93972/discussion-between-traggatmot-and-jaap). – traggatmot Nov 02 '15 at 11:29

1 Answers1

1

After some trial and error, I think it's better to use geom_segment in order to get a nice legend. With:

ggplot(allDTI, aes(x=values, color=type)) + 
  stat_ecdf(size=1) +
  geom_segment(data=meanALLDTI, aes(x=values.mean, xend=values.mean, y=0, yend=1, color=type, linetype = type), size=1) +
  labs(title=expression(TI[d]~'and'~TI[l]~'and'~TI[w]), x="Index Value", y="Cumulative Density") +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  scale_color_manual(values=c("grey10", "grey30", "grey50"),
                     labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
  scale_linetype_manual(values=c(3,2,5),
                        labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
  guides(color = guide_legend(title="color",
                              override.aes = list(color = c("grey10", "grey30", "grey50"),
                                                  size = 2)),
         linetype = guide_legend(title="lty", override.aes = list(linetype = c(3,2,5),
                                                                   size = 0.7))) +
  theme(text = element_text(size=20),
        plot.title = element_text(size=30,face="bold",vjust=1),
        axis.title.x = element_text(size=20,face="bold",vjust=0,hjust=0.5),
        axis.title.y = element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
        legend.position = c(0.85, 0.25),
        legend.box = 'horizontal',
        legend.title = element_blank(),
        legend.text.align = 0,
        legend.text = element_text(size=20,vjust=0,hjust=0),
        legend.key = element_rect(fill=NA),
        legend.key.height = unit(1.5, 'line'),
        legend.key.width = unit(1.5, 'line'),
        panel.background = element_rect(fill = "white"))

you get:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Jaap, can you comment on why it's not easy to set up two different legends the way I was hoping to, considering I have two different types? I got vertical lines and I have CDF's, and one would think I'd be able to set a legend for each, but that doesn't seem possible, right? – traggatmot Nov 02 '15 at 17:28
  • @traggatmot That's because if you use `type` for two different types of legend, the legends get combined into one. – Jaap Nov 03 '15 at 06:31