0
votes

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

enter image description here

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.

1
The problem I have is that the two (out of eight) lines are plotted with greater thickness and grouped according to size = Highlight. However, this latter legend has been switched off (using guides(size = FALSE)) in order to avoid a clutter of redundant legends on the plot. So, I want to thicken just two lines in the legend associated with linetype = Fa.IVIVC. The response you link to applies a thicker line by means of the command guides(colour = guide_legend(override.aes = list(size=3))). How do you modify that to be conditional to the two lines I am seeking to identify?please help

1 Answers

4
votes

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

enter image description here