hline传奇不工作ggplot2

时间:2015-10-13 19:02:48

标签: r ggplot2 data-visualization

我正在尝试为我在剧情中显示的hline添加标签。我可以在图表上显示hline,但我无法更改标题,它显示为空格。这是数据:

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)

这是生成的图(使用整个数据集),它只显示标题&#39; average&#39;我无法修改。我想显示标题(使用不同的名称)并显示相同的虚线,它显示在图例键This is the plot

的图表上

感谢。

1 个答案:

答案 0 :(得分:1)

我相信,如果您取消注释show_guide = TRUE,虚线可能实际显示在图例中,但您无法看到它。例如,如果您有16位颜色质量,就会发生这种情况。如果是这种情况,一旦您保存图表,您将能够看到图例中的线条。我保存为PNG。

不幸的是,一旦你看到你注意到的行,你的size图例中就会出现虚线。您需要通过linetype = 0中的override.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