简体   繁体   English

手动添加图例元素-ggplot2

[英]Adding manually a legend element - ggplot2

With the code below: 使用以下代码:

plt <- ggplot(data) + geom_step(aes(factor(no), var7, color = group_no, group = group_no), size = 1.6)
plt + geom_step(aes(factor(no), var5, color = group_no, group = group_no), linetype = 'dashed', size = .7)

I have generated the following plot. 我生成了以下图。 在此处输入图片说明

Now I would like to modify legend in one of the following way: 现在,我想通过以下方式之一修改图例:

  • add the legend for color and dashed line 添加图例的颜色和虚线
  • add two dashed lines to the ones already visible on the legend and modify their explanation so that they would show for example 'id 1, var5', 'id2, var 5' for dashed lines and 'id1, var7', 'id2, var7' for solid lines (actually, this one is preferable) 在图例中已经可见的虚线上添加两条虚线,并修改其说明,以便例如显示虚线的“ id 1,var5”,“ id2,var 5”和“ id1,var7”,“ id2,var7” '用于实线(实际上,这是首选)

Is it feasible? 可行吗 I have tried with scale_linetype_manual(values = c('var5', 'var7') but it didn't work. 我已经尝试过使用scale_linetype_manual(values = c('var5', 'var7')但是没有用。

The data look like this: 数据如下所示:

data <- structure(list(no = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), group_no = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"), var1 = c(2, 1, 1, 1, 1, 0, 1, 1), var2 = c(0, 0, 1, 1, 1, 
0, 1, 0), var3 = c(2.04466646181187, 0.660598114091747, 1.55142947390572, 
1.53055726052766, 1.33062973767801, 0.521466633696396, 0.383486796026974, 
0.320273289219046), var4 = c(0.786548055557462, 0.933132594335315, 
0.734844331310191, 0.404908113668656, 0.50963171017644, 0.066048513105941, 
0.156065948976073, 0.528480184907794), var5 = c(2, 1, 3, 
2, 4, 2, 5, 3), var6 = c(0, 0, 1, 1, 2, 1, 3, 1), var7 = c(2.04466646181187, 
0.660598114091747, 3.59609593571759, 2.19115537461941, 4.92672567339561, 
2.71262200831581, 5.31021246942258, 3.03289529753485), var8 = c(0.786548055557462, 
0.933132594335315, 1.52139238686765, 1.33804070800397, 2.03102409704409, 
1.40408922110991, 2.18709004602017, 1.93256940601771)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -8L), vars = "group_no", drop = TRUE, .Names = c("no", "group_no", "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8"), indices = list(c(0L, 2L, 4L, 6L), c(1L, 3L, 5L, 7L)), group_sizes = c(4L, 4L), biggest_group_size = 4L, labels = structure(list(group_no = structure(1:2, .Label = c("1", "2"), class = "factor")), class = "data.frame", row.names = c(NA, -2L), vars = "group_no", drop = TRUE, .Names = "group_no"))

If I understood correctly, here is an option for your first request 如果我理解正确,这是您的第一个请求的选项

library(tidyverse)
data %>% 
 gather(variable, value, c(var5, var7)) %>% 
 ggplot() + 
 geom_step(aes(factor(no), 
               value, 
               color = group_no,
               linetype = variable,
               group = interaction(group_no, variable),
               size = variable)) +
 scale_linetype_manual(values = c('var5' = "dashed",
                                  'var7' = "solid")) +
 scale_size_manual(values = c('var5' = 1,
                              'var7' = 2)) +
 guides(size = "none")

Reshape your data from wide to long format and map color to 'group_no' and linetype to 'variable'. 将数据从宽格式更改为长格式,然后将color映射为“ group_no”,将linetype映射为“ variable”。 The interaction is necessary because the group is not defined by a single variable anymore, but by a combination of 'group_no' and 'variables'. interaction是必要的,因为该组不再由单个变量定义,而是由“ group_no”和“ variables”的组合定义。

Change the size of the lines using scale_size_manual after you mapped 'variable' to this aesthetic. 在将“变量”映射到此外观之后,使用scale_size_manual更改线条的size Add + guides(size = "none") such that the different size s of geom_step is not displayed in the legend. 添加+ guides(size = "none") ,以使图例中不会显示不同sizegeom_step

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM