I have a problem with customizing a linetype legend in ggplot. I use the following code to generate a plot having mapped the linetype aesthetics to the sample.
ggplot(
mflp_data_hilltop,
aes(
x = `Water Content [Vol%]` / 100, y = cum_mflp,
col = species, linetype = sample
)
) +
geom_line(lwd = 0.7) +
labs(
y = expression(paste("MFLP ", "[", "cm"^"2", " d"^"-1", "]")),
x = expression(paste(italic(theta), " [", "cm"^"3", "cm"^"-3", "]"))
) +
scale_linetype_manual(
values = c("solid", "dashed"),
guide = guide_legend(title = "test", nrow = 2)
) +
scale_color_manual(
values = c("#d7191c", "#fdae61", "#abdda4", "black", "#2b83ba")
) +
theme(
legend.position = c(0.4, 0.95),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.margin = margin(6, 6, 6, 6),
panel.background = element_rect(fill = "white", colour = "black"),
legend.key = element_rect(fill = "white", colour = "white"),
legend.title = element_blank(),
legend.text = element_text(size = 12),
text = element_text(size = 30),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm")
) -> p6
p6
Everything works fine, except the to legend are not aligned nicely. In Order to alter this i tried to use the guides()
object like this
guides(linetype = guide_legend(theme(legend.title.position = c(0.7, 0.5)))) +
It doesn’t really do anything but also no error is thrown.
What I would like to achieve is to have precise control over the linetype legend without affecting the color-legend.
Any ideas are welcome!!!
I have tried different variations of the commands. E.g. adding the guides command directly to scale_linetype_manual(). I searched the net, but didn’t come across a similar case.
Here is some example data:
structure(list(sample = c("HT_C", "HT_C", "HT_C", "HT_C", "HT_C",
"HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_Si", "HT_Si", "HT_Si",
"HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si",
"HT_Si", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C",
"HT_C", "HT_C", "HT_C", "HT_C", "HT_Si", "HT_Si", "HT_Si", "HT_Si",
"HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si",
"HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C",
"HT_C", "HT_C", "HT_C", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si",
"HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_C",
"HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C",
"HT_C", "HT_C", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si",
"HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_C",
"HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C", "HT_C",
"HT_C", "HT_C", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si",
"HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si", "HT_Si"), `Water Content [Vol%]` = c(32.52,
32, 31.46, 30.93, 30.39, 29.86, 29.33, 28.81, 28.3, 27.81, 34.68,
34.25, 33.83, 33.41, 33, 32.59, 32.19, 31.79, 31.4, 31.02, 30.65,
32.52, 32, 31.46, 30.93, 30.39, 29.86, 29.33, 28.81, 28.3, 27.81,
27.32, 34.68, 34.25, 33.83, 33.41, 33, 32.59, 32.19, 31.79, 31.4,
31.02, 30.65, 32.52, 32, 31.46, 30.93, 30.39, 29.86, 29.33, 28.81,
28.3, 27.81, 27.32, 34.68, 34.25, 33.83, 33.41, 33, 32.59, 32.19,
31.79, 31.4, 31.02, 30.65, 32.52, 32, 31.46, 30.93, 30.39, 29.86,
29.33, 28.81, 28.3, 27.81, 27.32, 34.68, 34.25, 33.83, 33.41,
33, 32.59, 32.19, 31.79, 31.4, 31.02, 30.65, 32.52, 32, 31.46,
30.93, 30.39, 29.86, 29.33, 28.81, 28.3, 27.81, 27.32, 34.68,
34.25, 33.83, 33.41, 33, 32.59, 32.19, 31.79, 31.4, 31.02, 30.65
), cum_mflp = c(10.9212336553344, 10.6678784106565, 10.414408270956,
10.1667365193374, 9.9190970839598, 9.67701900460981, 9.43492067153276,
9.19826440644535, 8.96701760672606, 8.74105088093947, 8.52561192938719,
8.33342232225852, 8.14552231183456, 7.95764407508846, 7.77406642549309,
7.59043142309206, 7.41096144938016, 7.23552573640284, 7.06410011550287,
6.89268682461839, 6.72512630234199, 10.0846390627921, 9.83128381811418,
9.57781367841366, 9.33014192679502, 9.08250249141745, 8.84042441206747,
8.59832607899042, 8.36166981390301, 8.13042301418372, 7.90445628839712,
7.68356850079023, 7.85464525845609, 7.66245565132742, 7.47455564090345,
7.28667740415736, 7.10309975456199, 6.91946475216095, 6.73999477844905,
6.56455906547173, 6.39313344457176, 6.22172015368728, 6.05415963141088,
10.9697399949587, 10.7163847502808, 10.4629146105803, 10.2152428589617,
9.96760342358409, 9.7255253442341, 9.48342701115705, 9.24677074606964,
9.01552394635035, 8.78955722056376, 8.56866943295686, 8.56503936246123,
8.37284975533257, 8.1849497449086, 7.9970715081625, 7.81349385856713,
7.6298588561661, 7.4503888824542, 7.27495316947688, 7.10352754857691,
6.93211425769243, 6.76455373541603, 10.7331789862037, 10.4798237415259,
10.2263536018253, 9.9786818502067, 9.73104241482913, 9.48896433547915,
9.2468660024021, 9.01020973731468, 8.7789629375954, 8.5529962118088,
8.3321084242019, 8.37275535561064, 8.18056574848198, 7.99266573805801,
7.80478750131191, 7.62120985171654, 7.43757484931551, 7.25810487560361,
7.08266916262629, 6.91124354172632, 6.73983025084184, 6.57226972856544,
11.3592767354311, 11.1059214907532, 10.8524513510527, 10.6047795994341,
10.3571401640565, 10.1150620847065, 9.87296375162948, 9.63630748654207,
9.40506068682278, 9.17909396103618, 8.95820617342929, 8.88166671228432,
8.68947710515565, 8.50157709473168, 8.31369885798559, 8.13012120839022,
7.94648620598918, 7.76701623227729, 7.59158051929996, 7.42015489839999,
7.24874160751551, 7.08118108523911), species = c("sunflower",
"sunflower", "sunflower", "sunflower", "sunflower", "sunflower",
"sunflower", "sunflower", "sunflower", "sunflower", "sunflower",
"sunflower", "sunflower", "sunflower", "sunflower", "sunflower",
"sunflower", "sunflower", "sunflower", "sunflower", "sunflower",
"barley", "barley", "barley", "barley", "barley", "barley", "barley",
"barley", "barley", "barley", "barley", "barley", "barley", "barley",
"barley", "barley", "barley", "barley", "barley", "barley", "barley",
"barley", "wheat", "wheat", "wheat", "wheat", "wheat", "wheat",
"wheat", "wheat", "wheat", "wheat", "wheat", "wheat", "wheat",
"wheat", "wheat", "wheat", "wheat", "wheat", "wheat", "wheat",
"wheat", "wheat", "potato", "potato", "potato", "potato", "potato",
"potato", "potato", "potato", "potato", "potato", "potato", "potato",
"potato", "potato", "potato", "potato", "potato", "potato", "potato",
"potato", "potato", "potato", "lolium", "lolium", "lolium", "lolium",
"lolium", "lolium", "lolium", "lolium", "lolium", "lolium", "lolium",
"lolium", "lolium", "lolium", "lolium", "lolium", "lolium", "lolium",
"lolium", "lolium", "lolium", "lolium")), row.names = c(NA, -109L
), class = c("tbl_df", "tbl", "data.frame"))
3