0

I am trying to add a label for a hline that I show on my plot. I can display the hline on the graph but I can not alter the title and it shows as a blank space. This is the data:

library (ggplot2)
library(ggthemes)
library(scales)
library(grid)

toy<-data.frame(naics_3=c('sector 35','sector 34','sector 33',
                         'sector 32','sector 31','sector 30'),
               contrb_08_14=c(24.91,29.91,13.54,4.4,14.03,-0.37),
               real_exports14=c(6787,4399,3351,2690,1956,1407))

And this is the code:

p<-ggplot(toy, aes(x = naics_3, y =contrb_08_14)) +
 geom_point(data=toy, aes(size =(real_exports14)), 
            colour="#1947A3",
            alpha=0.9)+
  scale_size(breaks = c(50,200,800,3200,6000),
             range = c(5,30),
             labels = c('50 million',
                        '100 million',
                        '800 million',
                        '3 billion',
                        '6 billion'))+
  geom_hline(aes(yintercept = mean(contrb_08_14),
                 fill='average',
                 linetype='dashed'), 
             color = "#00004C", 
             linetype='dashed',
             data=bubble,
             #show_guide = T,
             alpha=0.4)+
  scale_linetype(name = "Ave")+
  guides(linetype = guide_legend(override.aes = list(colour = "red",
                                                     fill='purple')))+
  guides(size=guide_legend(override.aes = list(size = c(4,
                                                        8,
                                                        15,
                                                        18,
                                                        24)),
                           title = "Exports 2014",
                           title.hjust = 0.5,
                           keywidth=0.3))+
  coord_flip()+
  theme(plot.margin=unit(c(0.0,0.0,0.0,0.0),"mm"))+
  theme_classic()+
  scale_y_continuous(limits=c(-0.05, 0.35),
                     breaks=c(-0.05, 0, 0.05, 0.1, 0.15,
                              0.2, 0.25, 0.3, 0.35),
                     labels = percent)+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #plot.background = element_rect(fill = '#F2F2F2'),
        #legend.background = element_rect(fill="#F2F2F2"),
        panel.background = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(color = 'black'))+
  theme(axis.text.x=element_text(size = 9),
        axis.text.y=element_text(size = 7))+
  theme(legend.key = element_rect(colour = "white",
                                  fill = "white",
                                  size=0.5),
        legend.key.size= unit(0.1,"cm"),
        legend.text= element_text(size = 8),
        legend.title= element_text(size = 8.5))+
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank())
print(p)

This is the plot produced (with entire dataset), it only displays the title 'average' which I can not modify. I would like to show the title (with a different name) and show the same dashed line it is displayed on the plot in the legend keyThis is the plot

Thanks.

asado23
  • 366
  • 1
  • 7
  • 20
  • 2
    You should attempt to provide a **minimal** [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (one that doesn't require downloading a data file from somewhere). Remove all code not relevant to this problem. – MrFlick Oct 13 '15 at 19:05

1 Answers1

1

I believe, if you uncomment show_guide = TRUE, that the dashed line may actually be showing in the legend but you just can't see it. This can happen, for example, if you have 16 bit color quality. If this is the case, once you save the plot you'll be able to see the line is there in the legend. I saved as a PNG.

Unfortunately, once you can see the lines you'll notice have dashed lines in your size legend, as well. You will want to remove those via linetype = 0 in override.aes.

You can remove the title for your legend within guide_legend for fill. You have to do this via the fill aesthetic because that's what you used to make the legend for the horizontal line in your plot.

Here's a scaled down version of your plot code, just to show the pieces I'm adding.

ggplot(toy, aes(x = naics_3, y =contrb_08_14)) +
    geom_point(aes(size = real_exports14), 
                         colour = "#1947A3", alpha=0.9) +
    geom_hline(aes(yintercept = mean(contrb_08_14), fill = "average"), 
                         color = "#00004C", linetype = "dashed",
                         show_guide = TRUE, alpha = 0.4) +
    guides(fill = guide_legend(title = NULL),
           size = guide_legend(override.aes = list(size = c(4, 8, 15, 18, 24),
                                                   linetype = 0),
                                title = "Exports 2014", title.hjust = 0.5,
                                keywidth=0.3)) +
    theme_classic() +
    coord_flip()

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118