hline 图例不工作 ggplot2

hline legend not working ggplot2

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

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)

这是生成的图(包含整个数据集),它只显示我无法修改的标题 'average'。我想显示标题(使用不同的名称)并显示与图例键

中的情节相同的虚线

谢谢。

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

不幸的是,一旦您看到线条,您会注意到 size 图例中也有虚线。您需要通过 override.aes 中的 linetype = 0 删除它们。

对于 fill,您可以在 guide_legend 内删除图例的标题。您必须通过 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()