简体   繁体   English

使用ggplot2将图例中的线宽与图中的线宽匹配

[英]Matching line width in legend to line width in plot using ggplot2

This question is a follow-up to a previous question I posted to stackoverflow which has already been addressed . 这个问题是我发布到stackoverflow先前问题的后续问题,该问题已经解决 I wish to thank once again the respondent who took time to address the previous question. 我要再次感谢被访者花时间解决上一个问题。 For the sake of conciseness, I will not reproduce the details of the previous question, so please refer to the hyperlink to view the data set in order to reproduce the problem. 为了简洁起见,我不会重现上一个问题的详细信息,因此请参考超链接以查看数据集以重现该问题。

In summary, I have a plot involving three sets of points grouped by color according to a column "Formulation", and eight non-colored lines grouped using linetype and size, the latter being mapped to two different grouping variables ("Fa.IVIVC" and "Highlight" respectively). 总而言之,我有一个图,其中涉及根据“公式”列按颜色分组的三组点,以及使用线型和大小分组的八条非彩色线,后者分别映射到两个不同的分组变量(“ Fa.IVIVC”和“突出显示”)。 The command used is reproduced below followed by the resulting plot. 下面复制了所使用的命令,随后是结果图。

> ggplot() +
+   geom_point(data = df, aes(
+     x = invitro,
+     y = invivo,
+     colour = factor(Form, labels = c("Fast", "Medium", "Slow"))
+   )) +
+   geom_line(
+     data = line_data,
+     aes(x = invitro, y = Fabs, linetype = `Fa.IVIVC`, size = Highlight)
+   ) +
+   labs(title = "Plot", colour = "Formulation") +
+   scale_x_continuous(limits = c(0, 100)) +
+   scale_y_continuous(limits = c(0, 100)) +
+   guides(size = FALSE) +
+   scale_size_manual(values = c("TRUE" = 2, "FALSE" = 0.5)) +
+   theme(
+     panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
+     panel.background = element_blank(), axis.line = element_line(colour = "black")
+   )

在此处输入图片说明

Note that the above command acts on a data frame which was augmented to the original one I posted using dput(). 请注意,以上命令作用于一个数据框,该数据框已扩充为我使用dput()发布的原始数据框。

The whole purpose of the previous post was to find out how to highlight two lines in an otherwise busy plot. 上一篇文章的全部目的是找出如何在繁忙的情节中突出显示两条线。 However, the clarity of this objective is diminished unless the legend also shows the associated lines in the specified thicknesses. 但是,除非图例也以指定的厚度显示关联的线条,否则此物镜的清晰度会降低。 I do not want to clutter the plot with too many legends, and currently, the legends 'Formulation' and 'Fa.IVIVC' appear to be sufficient. 我不想在图例中添加太多图例,目前,图例“ Formulation”和“ Fa.IVIVC”似乎已足够。 So, I really want the linewidths of the lines where the variable "Highlight" = TRUE to show up in the legend 'Fa.IVIVC'. 因此,我确实希望在图例“ Fa.IVIVC”中显示变量“ Highlight” = TRUE的行的线宽。 How can this be done? 如何才能做到这一点?

Thank you. 谢谢。

You can define even more manually the line widths in scale_size_manual ... 您甚至可以在scale_size_manual手动定义线宽...
I think it is better also to slightly modify the legend key to remove the gray background (to make it match you theme) and to increase the width of the key with the legend.key.width argument in theme (otherwise you the legend of the thick dotted line show only one dot). 我认为最好稍微修改图例键以删除灰色背景(使其与您的主题匹配),并在theme使用legend.key.width参数增加键的宽度(否则,您将粗虚线仅显示一个点)。

ggplot() +
    geom_point(data = df, aes(
        x = invitro,
        y = invivo,
        colour = factor(Form, labels = c("Fast", "Medium", "Slow"))
        )) +
    geom_line(
        data = line_data,
        aes(x = invitro, y = Fabs, linetype = `Fa.IVIVC`, size = `Fa.IVIVC`)
        ) +
    labs(title = "Plot", colour = "Formulation") +
    scale_x_continuous(limits = c(0, 100)) +
    scale_y_continuous(limits = c(0, 100)) +
    scale_size_manual(values = c("DWeibull" = 0.5, "Fa = Fd" = 2, 
                                 "Fa = m*Fd + c" = 0.5, "polyx2" = 0.5, "polyx3" = 0.5, 
                                 "powerlaw" = 2, "Sigmoid" = 0.5, "SWeibull" = 0.5, 
                                 "Time scale 1" = 0.5, "Time scale 2" = 0.5)) +
    theme(
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank(), legend.key.width = unit(4,"line")
        ) 

在此处输入图片说明

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

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