How can I change the thickness of a specific line and/or add shape in a multi-line plot?
How can I change the thickness of a specific line and/or add shape in a multi-line plot?
I have a data called molten.data below. I have this code which plots the lines I want, but I need to change the thickness of one specific line (G11F:G11M) to make it look thicker compared to other lines or preferably add shape to the data point in that line. How can we do it?
molten.data
G11F:G11M
code I have:
ggplot(molten.data, aes(variable, value,group= key.related,colour=key.related)) +
geom_line() +
geom_point()
data:
molten.data<- structure(list(key.related = c("G11F:G11F", "G11F:G11F", "G11F:G11F",
"G11F:G11M", "G11F:G11F", "G11F:G11M", "G11F:AOGC-02-0079", "G11F:G11F",
"G11F:G11M"), variable = structure(c(1L, 2L, 3L, 3L, 4L, 4L,
4L, 5L, 5L), .Label = c("IBS_2_samples", "IBS_4_samples", "IBS_8_samples",
"IBS_16_samples", "IBS_32_samples"), class = "factor"), value = c(0.533,
1.01, 1.11, 0.132, 1.22, 0.353, 0.0658, 1.33, 0.534)), .Names = c("key.related",
"variable", "value"), row.names = c(1L, 82L, 163L, 168L, 244L,
249L, 260L, 325L, 330L), class = "data.frame")
2 Answers
2
You can set up a column to note whether an observation should be highlighted, and map that to size, shape, linetype, etc.
You can do that with an ifelse:
ifelse
library(tidyverse)
molten.data %>%
mutate(hilite = ifelse(key.related == "G11F:G11M", 2, 1))
#> key.related variable value hilite
#> 1 G11F:G11F IBS_2_samples 0.5330 1
#> 2 G11F:G11F IBS_4_samples 1.0100 1
#> 3 G11F:G11F IBS_8_samples 1.1100 1
#> 4 G11F:G11M IBS_8_samples 0.1320 2
#> 5 G11F:G11F IBS_16_samples 1.2200 1
#> 6 G11F:G11M IBS_16_samples 0.3530 2
#> 7 G11F:AOGC-02-0079 IBS_16_samples 0.0658 1
#> 8 G11F:G11F IBS_32_samples 1.3300 1
#> 9 G11F:G11M IBS_32_samples 0.5340 2
My preference is to do it with factors. forcats::fct_other lets you pick levels to keep, and labels everything else as "Other". Imagine you have several keys you want to keep with their original names, and a whole lot of keys you want to lump into an "Other" category—it becomes pretty useful as the data gets more complicated.
forcats::fct_other
Then I pipe it into ggplot, using hilite to designate the highlighting. One way is with size; you can adjust the sizes to make it more dramatic, or get whatever sizes are appropriate:
ggplot
hilite
molten.data %>%
mutate(hilite = as.factor(key.related) %>% fct_other(keep = c("G11F:G11M"), other_level = "Other keys")) %>%
ggplot(aes(x = variable, y = value, color = key.related, group = key.related, size = hilite)) +
geom_line() +
geom_point(size = 2) +
scale_size_manual(values = c("G11F:G11M" = 1.5, "Other keys" = 0.5), guide = F)

Or keeping size uniform but changing shapes. Here I set up the shape values so the non-highlighted points get a normal circle point and the highlighted points get triangles.
molten.data %>%
mutate(hilite = as.factor(key.related) %>% fct_other(keep = c("G11F:G11M"), other_level = "Other keys")) %>%
ggplot(aes(x = variable, y = value, color = key.related, group = key.related)) +
geom_line() +
geom_point(aes(shape = hilite), size = 2) +
scale_shape_manual(values = c("G11F:G11M" = 17, "Other keys" = 16), guide = F)

Created on 2018-07-01 by the reprex package (v0.2.0).
Based on what you need you can keep or remove size or linetype as shown below -
l <- ifelse(molten.data$key.related == 'G11F:G11M', 1, 2)
ltyp <- ifelse(molten.data$key.related == 'G11F:G11M', 'longdash', '“dotted”')
ggplot(data=molten.data, aes(x=variable, y=value, group= key.related, colour=key.related)) +
geom_line(aes( linetype = ltyp , size = l )) +
geom_point(aes( size = l ))

What is
l? Also, why do you include linetype in the geom_point call?– markus
Jul 1 at 7:35
l
linetype
geom_point
@MAPK black legend are the size.
– Vikash Kumar
Jul 2 at 13:13
Sorry
l was supposed to be size and by copy-paste, I pasted ltyp also in geom_point. Thank you @markus for pointing out.– Vikash Kumar
Jul 2 at 13:13
l
size
ltyp
geom_point
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks Vikash, but what are those black box/legend?
– MAPK
Jul 1 at 4:00