下图显示了控制比例尺颜色手册时的2个图例 [英] plot below showing 2 legends when controling scale color manual

查看:44
本文介绍了下图显示了控制比例尺颜色手册时的2个图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情节在下面,有2个图例出现.我有一个问题:

Hi I have the plot below and there are 2 legends showing up. I have a question:

目前,我添加的图例有6个条目"A值",广告值","au值","b","bD值"和"bU值".我真的想让图例只显示4个条目

Currently the legend I added has 6 entries "A Value", "Ad value", "au value", "b", "bD Value" and "bU value". I really want to the legend to show only 4 entries

  • "A",并且在图例中,它应该是一条蓝色实线,就像已经是
  • "bd& bu",在图例中,这应该是蓝色的DASHED行...不确定如何获取此
  • "A",在图例中,它应该是一条红色实线,就像它已经是
  • "ad& au",并且在图例中,这应该是一条红色的DASHED行...不确定如何实现此操作

有什么想法吗?

d =  data.frame (title =        c(  rep(c("aU","A","ad"),2), rep(   c("bU","b","bD"),2 ) )  ,
               time = c(1,1,1,2,2,2,1,1,1,2,2,2)   ,
               value = c(10,8,4,9,7,3,5,3,1,4,2,0))

d
 ggplot(data=d , aes(x=time, y=value, 
                                group=title, 
                                colour = title,
                                linetype =title))+
     geom_line()  + geom_point() +


       scale_colour_manual( name = "Metric",  

                                  values = c( 
                                  A = "red", 
                                  ad = "red",
                                  aU = "red",
                                  b = "blue",
                                  bU ="blue",
                                  bD= "blue"),

                                  labels = c( 
                                  A = "A value", 
                                  ad = "Ad value", 
                                  aU = "au value",
                                  b = "b",
                                  bU ="bU vlaue",
                                  bD= "dD Value")
                            )+

    scale_linetype_manual(name = "Metric",
                                 values =c(
                                  A = "solid", 
                                  ad = "dashed", 
                                  aU = "dashed",
                                  b = "solid",
                                  bU ="dashed",
                                  bD= "dashed"),
                              labels = c( 
                                  A = "A value", 
                                  ad = "Ad value", 
                                  aU = "au value",
                                  b = "b",
                                  bU ="bU vlaue",
                                  bD= "dD Value")
                          )

推荐答案

第二个图例显示是因为您仅在两个指南之一中更改了标签和标题.如果您更改另一个以使其匹配(或者更好的是,更改 title 列以匹配所需的标签),则颜色和线型将一起显示在一个图例中.

The second legend is showing up because you changed the labels and title in just one of the two guides. If you change the other to match it (or better yet, change the title column to match the labels you want), the color and linetype will show up together in one legend.

或者,如果分组匹配(即,如果"U"在"A"和"B"中含义相同),则可以在其中一个上设置颜色,在另一个上设置线型(出现成为您当前正在做的事情).像这样:

Alternatively, if the groupings match up (i.e., if the "U" means the same thing in "A" and "B"), you could set the color on one of those and the linetype on the other (which appears to be what you are currently doing). Like this:

d2 <-
  tidyr::separate(d
                  , title
                  , c("group","subgroup")
                  , 1
                  , FALSE)

ggplot(data=d2
       , aes(x=time, y=value, 
             group=title, 
             colour = group,
             linetype =subgroup))+
  geom_line()  + geom_point()

然后,如果您仍然需要,可以手动设置颜色和线型.

You could then manually set colors and linetypes if you still wanted.

或者,对于您当前的方法:

Or, for your current approach:

ggplot(data=d , aes(x=time, y=value, 
                    group=title, 
                    colour = title,
                    linetype =title))+
  geom_line()  + geom_point() +


  scale_colour_manual( 
                       values = c( 
                         A = "red", 
                         ad = "red",
                         aU = "red",
                         b = "blue",
                         bU ="blue",
                         bD= "blue")
  )+

  scale_linetype_manual(values =c(
    A = "solid", 
    ad = "dashed", 
    aU = "dashed",
    b = "solid",
    bU ="dashed",
    bD= "dashed")
  )

要仅设置两种线型,可以使用:

To set just two line types, you can use:

d2$forLabel <- ifelse(d2$subgroup == "", "Value", "Limit")


ggplot(data=d2
       , aes(x=time, y=value, 
             group=title, 
             colour = group,
             linetype =forLabel))+
  geom_line()  + geom_point() +
  scale_linetype_manual(values = c(Limit = "dashed"
                                   , Value = "solid"))

对于所有内容,我建议您使用它而不是单独的标签(特别是在以后添加更多组的情况下)

I'd recommend this instead of separate labels for everything (in particular, in case you add more groups later)

作为另一种选择,如果要显示间隔,您会考虑使用功能区而不是两行吗?

As another alternative, if you are displaying intervals, would you instead consider using a ribbon, instead of two lines?

d3 <-
  d2 %>%
  group_by(group,time) %>%
  summarise(min = min(value), max=max(value)
            , value = value[forLabel=="Value"])

d3 <-
  d2 %>%
  group_by(group,time) %>%
  summarise(min = min(value), max=max(value)
            , value = value[forLabel=="Value"])


ggplot(data=d3
       , aes(x=time, y=value, 
             color = group))+
  geom_line()  + geom_point() +
  geom_ribbon(aes(ymin = min, ymax = max
                  , fill = group
                  , color = NULL)
              , alpha = 0.2
              , color = NA) +
  theme_minimal()

这篇关于下图显示了控制比例尺颜色手册时的2个图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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