MC2020
MC2020

Reputation: 87

Add customized legends to geom_line() in R

d_probe <- soread()
ggplot(d_probe) +
  geom_line(aes(x = identity, y = effect)) +
  geom_line(aes(x = identity, y = ULCI), linetype = "dotted") + 
  geom_line(aes(x = identity, y = LLCI), linetype = "dotted")

d_probe:

identity    effect        se         t         p      LLCI      ULCI
     2.0000   -0.8046    0.2984   -2.6965    0.0081   -1.3960   -0.2132
     2.1400   -0.7471    0.2768   -2.6993    0.0081   -1.2956   -0.1985
     2.2800   -0.6895    0.2554   -2.6997    0.0080   -1.1957   -0.1833
     2.4200   -0.6319    0.2344   -2.6959    0.0081   -1.0965   -0.1674
     2.5600   -0.5744    0.2139   -2.6855    0.0084   -0.9983   -0.1505
     2.7000   -0.5168    0.1940   -2.6643    0.0089   -0.9013   -0.1324
     2.8400   -0.4593    0.1749   -2.6257    0.0099   -0.8059   -0.1126
     2.9800   -0.4017    0.1570   -2.5590    0.0119   -0.7128   -0.0906
     3.1200   -0.3441    0.1406   -2.4475    0.0160   -0.6228   -0.0655
     3.2600   -0.2866    0.1264   -2.2668    0.0254   -0.5372   -0.0360
     3.4000   -0.2290    0.1152   -1.9875    0.0494   -0.4574   -0.0006
     3.4023   -0.2281    0.1151   -1.9820    0.0500   -0.4562    0.0000
     3.5400   -0.1715    0.1080   -1.5882    0.1151   -0.3854    0.0425
     3.6800   -0.1139    0.1054   -1.0804    0.2824   -0.3229    0.0951
     3.8200   -0.0563    0.1080   -0.5218    0.6028   -0.2703    0.1577
     3.9600    0.0012    0.1153    0.0105    0.9916   -0.2272    0.2296
     4.1000    0.0588    0.1265    0.4648    0.6430   -0.1919    0.3094
     4.2400    0.1163    0.1406    0.8272    0.4099   -0.1624    0.3951
     4.3800    0.1739    0.1570    1.1075    0.2705   -0.1373    0.4851
     4.5200    0.2315    0.1750    1.3230    0.1886   -0.1153    0.5782
     4.6600    0.2890    0.1940    1.4896    0.1392   -0.0955    0.6736
     4.8000    0.3466    0.2139    1.6201    0.1081   -0.0774    0.7706

How can I add customized legends to the three lines so that readers could know their meanings? Sample legends could be: "this is line 1," "this is line 2," and "this is line 3." Thank you!

Upvotes: 1

Views: 45

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

First, the basic plot. The legend will have the variables' names automatically, since effect, LLCI and ULCI are now under the long format variable name, mapped to linetype.

library(dplyr)
library(tidyr)
library(ggplot2)

g <- d_probe %>%
  pivot_longer(cols = c(effect, LLCI, ULCI)) %>%
  ggplot(aes(x = identity, y = value, linetype = name)) +
  geom_line()

Now, customize the line types.

lty <- c("solid", "dotted", "dotted")
g + scale_linetype_manual(values = lty)

enter image description here

To change the legend, use argument labels, plot omited.

labs <- c("this is line 1", "this is line 2", "this is line 3")
g + scale_linetype_manual(labels = labs, values = lty)

Edit

In order to make the space between the legend lines and its text, use a theme with legend.spacing set. In this case It should be legend.spacing.x. And to left align the text will also make the spacing equal for all text labels.

g + scale_linetype_manual(values = lty) +
  theme(
    legend.spacing.x = unit(0, "cm"),
    legend.text.align = 0
  )

Upvotes: 1

Related Questions