2015-10-13 35 views
0

我想为我的情节中显示的hline添加一个标签。我可以在图表上显示水平线,但我不能更改标题,并将其显示为空白。这是数据:hline传说不起作用ggplot2

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)) 

而这是代码:

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

谢谢。

+2

你应该尝试提供一个** **最小[重复的例子(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r - 可重复生成的示例)(不需要从某处下载数据文件的示例)。删除与此问题无关的所有代码。 – MrFlick

回答

1

我相信,如果您取消注释show_guide = TRUE,虚线可能实际上显示在图例中,但您无法看到它。例如,如果您有16位色彩质量,则会发生这种情况。如果是这种情况,一旦你保存了阴谋,你就能看到图例中的那一行。我保存为PNG。

不幸的是,一旦你可以看到你的线条,在size图例中也会出现虚线。您需要通过linetype = 0override.aes中删除。

您可以在guide_legend内删除图例的标题fill。您必须通过fill审美才能做到这一点,因为这是您用于制作剧情中水平线的图例。

这是您的剧情代码的缩小版本,只是为了展示我添加的作品。

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