使用ggplot2仅显示一个图例中的特定线条和区域

时间:2016-12-11 21:40:15

标签: r plot ggplot2 legend

我对某个情节有疑问。考虑我有以下data.frame:

data <- data.frame(a = c(44, 58, 60, 63, 65, 66, 67, 68, 69, 70), 
               b = c(38, 52, 55, 57, 59, 60, 61, 62, 63, 65),
               c = c(51, 64, 66, 68, 70, 71, 71, 73, 74, 75),
               d = c(44, 53, 54, 55, 56, 56, 58, 59, 59, 60),
               e = c(44, 57, 59, 60, 62, 63, 64, 65, 66, 67),
               x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
rownames(data) = c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')

我想用三种不同的线型绘制三个多边形(x,a),(x,d),(x,e)&#39; solid&#39;&#39; dotdash&#39;并且&#39; dash&#39;。它们都应该出现在图例中。此外,我想用线型&#39;点缀&#39;来绘制多边形(x,b),(x,c)。和它们之间的颜色是红色的。只应在图例中显示红色区域,而不是虚线本身。为此,我编写了以下代码行:

data_no_legend = data[,c(2,3,6)]
data_legend = data[,c(1,4,5,6)]

data_melt_legend = melt(data_legend, id = "x")
data_melt_no_legend = melt(data_no_legend, id = "x")

p <- ggplot()
p <- p + geom_ribbon(data = data, aes(x = x, ymin = b, ymax = c,fill = 'legend'), alpha = 0.5, show.legend = TRUE, inherit.aes = FALSE)
p <- p + scale_fill_manual(labels = c("legend"), values = c('red'))
p <- p + geom_line(data = data_melt_legend, aes(x = x, y = value,    size = variable, linetype= variable), color = 'gray', show.legend = TRUE,  inherit.aes=FALSE)
p <- p + scale_linetype_manual(labels = c("legend 1","legend 2", "legend 3"),values = c("solid","dashed", "dotdash"))
p <- p + scale_size_manual(labels = c("legend 1","legend 2", "legend 3"),values = c(1,0.5,0.5))
p <- p + geom_line(data = data_melt_no_legend, aes(x = x, y = value,  group= variable), color= 'gray', linetype = 'dotted', show.legend = FALSE,  inherit.aes=FALSE)
p <- p + geom_point(data=data, aes(x = x, y = a), color = 'gray',  size = 1.5,inherit.aes=FALSE)

不幸的是,我有两个不同的传说。一个用于线路,一个用于区域。谁能帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,这需要做一些,但我认为这有效。我正在生成一个名为&#34; Legend&#34;应该显示你想要在图例中显示的内容(因此,你可能想要更新它)。请注意,我使用gather中的tidyverse而不是melt,并使用mutate中的dplyr生成此数据格式。

诀窍在于,您需要设置一个线型,其中包含您希望从geom_line获取线条的值。我将大小设置为0以避免双线打印。然后,添加线条并设置要打印的美学效果。最后一招是使用override.aes更改行后面的填充颜色(实质上是为geom_ribbon组添加)并禁止填充的单独图例(如果使用show.legend = FALSE ,它不会让你为linetype传奇设置填充。

gatheredData <-
  data %>%
  gather(variable, value, -x) %>%
  mutate(Legend = ifelse(variable %in% c("b","c")
                         , "bc", variable)
         , Legend = factor(Legend
                           , levels = c("a", "d", "e", "bc")))

ggplot() +
  geom_ribbon(aes(x = x
                  , ymax = c
                  , ymin = b
                  , fill = "bc"
                  , linetype = factor("bc"
                                      , levels = levels(gatheredData$Legend))
                  )
              , data = data
              , size = 0) +
  geom_line(aes(x = x
                , y = value
                , linetype = Legend
                , group = variable)
            , gatheredData) +
  scale_linetype_manual(values = c(a ='solid'
                                   , d = 'dotdash'
                                   , e = 'dashed'
                                   , bc = 'dotted')
                        , name = "Legend"
                        , breaks = levels(gatheredData$Legend)) +
  scale_fill_manual(values = c(bc = 'red')
                    , drop = FALSE) +
  guides(
    linetype = guide_legend(override.aes = list(fill = c("gray", "gray", "gray", "red")))
    ,    fill = "none"
    )

给出

enter image description here

请注意,我使用图例级别来设置图例条目的顺序。由于某些原因,它真的很挑剔,并要求我明确设置breaks =