联合线路&酒吧geoms:如何生成适当的传说? [英] Combined line & bar geoms: How to generate proper legend?

查看:151
本文介绍了联合线路&酒吧geoms:如何生成适当的传说?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

d2 的图例看起来不错;对于 d1 ,我想只显示白色/透明背景的水平线。

  df = data.frame(
Date = c(2012-11-30,2012-12-03,2012-12-04),
d1 = c(9,5,11),
d2 = c(4,6,3)

ggplot(df,aes(Date))+
geom_bar(aes (y = d2,color =d2),stat =identity,fill =red)+
geom_line(aes(y = d1,group = 1,color =d1))+
scale_colour_manual(,values = c(d1=blue,d2=red))


$ b 我添加了 aes(fill =d2)位于 geom_bar()中并被移除 fill =red。然后我为线条和酒吧添加了单独的比例。然后在 theme()中,我从图例条目中删除了灰色背景。

为确保在 scale_colour_manual()中的d2之前显示图例中的d1,引号(更长的名字)。 $ b

为了保持图例密钥在一行中, legend.box =horizo​​ntal添加到 theme()

  ggplot(df,aes(Date))+ 
geom_bar (aes(y = d2,fill =d2),stat =identity)+
geom_line(aes(y = d1,group = 1,color =d1))+
scale_colour_manual (,values = c(d1=blue,d2=red))+
scale_fill_manual(,values =red)+
theme(legend。 key = element_blank(),
legend.title = element_blank(),
legend.box =horizo​​ntal)


The legend for d2 looks fine; for d1, I would like to show just the hoizontal line against a white/transparent backgounnd.

df = data.frame(
  Date = c("2012-11-30", "2012-12-03", "2012-12-04"),
  d1 = c(9, 5, 11),
  d2 = c(4, 6, 3)
)
ggplot(df, aes(Date)) + 
  geom_bar(aes(y = d2, color = "d2"), stat="identity", fill = "red") +
  geom_line(aes(y = d1, group = 1, color = "d1"))  +
  scale_colour_manual("", values=c("d1" = "blue", "d2" = "red")) 

解决方案

It is not an elegant solution but at least it gives some result.

I added aes(fill="d2") in geom_bar() and removed fill="red". Then I added separate scales for line and for bars. Then in theme() I removed grey background from legend entry.

To ensure that d1 in legend is shown before d2 in scale_colour_manual(" ") there should be extra space between quotes ("longer" name).

To keep legend keys in one line, legend.box="horizontal" added to theme()

  ggplot(df, aes(Date)) + 
    geom_bar(aes(y = d2,fill="d2"), stat="identity") +
    geom_line(aes(y = d1, group = 1, color = "d1")) +
    scale_colour_manual(" ", values=c("d1" = "blue", "d2" = "red"))+
    scale_fill_manual("",values="red")+
    theme(legend.key=element_blank(),
          legend.title=element_blank(),
          legend.box="horizontal")

这篇关于联合线路&酒吧geoms:如何生成适当的传说?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆