简体   繁体   中英

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 . 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). 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().

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. So, I really want the linewidths of the lines where the variable "Highlight" = TRUE to show up in the legend 'Fa.IVIVC'. How can this be done?

Thank you.

You can define even more manually the line widths in 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).

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")
        ) 

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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