There are similar questions on the exchange but I do not think they apply to what I am trying to do here.
I have a point and line plot that has eight datasets. I would like to highlight just one and let the SW autocolor the rest. This is important as I may have instances where I have many more than eight. I do not want to have to manually set colors and just set the one I want to a color and style I specifcy. Is there a quick way to accomplish that?
In the picture below I would like to make CR5 black and dashed as an example.
data = data.frame(A = c("CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7"
)
,TIME = c(0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18
),
VAL = c(0,0.2062,0.3171,0.4032,0.5071,0.5931,0.7325,0.8381,1.0332,1.0945,1.0958,1.0958,1.0893,0,0.1192,0.1822,0.2314,0.2969,0.3583,0.4605,0.5571,0.7956,0.9577,1.0322,1.042,1.0413,0,0.1934,0.3049,0.3936,0.4999,0.5915,0.7453,0.8774,1.0571,1.0694,1.0657,1.0627,1.0658,0,0.0954,0.1427,0.1846,0.2418,0.2972,0.4113,0.5353,0.8212,0.9717,1.0038,1.0002,1.004,0,0.1095,0.1491,0.1875,0.2372,0.278,0.3608,0.4382,0.6466,0.807,0.8972,0.9325,0.9476,0,0.1025,0.1499,0.1884,0.239,0.2848,0.3689,0.4538,0.6884,0.8525,0.947,0.9714,0.9743,0,0.1213,0.1741,0.2158,0.2758,0.3251,0.4297,0.5286,0.785,0.9463,1.0264,1.044,1.0541
))
ggplot (data) +
geom_point( aes(x = TIME, y = VAL, group = A, color = A )) +
geom_line(aes(x = TIME, y = VAL, group = A, color = A )) +
theme_classic()
2
Does something like this help?
#packages
library(ggplot2)
library(data.table)
library(RColorBrewer)
#create a data.table of desired shapes and colors
plot_layout <- data.table(Species = unique(iris$Species))
plot_layout[, col := brewer.pal(n = nrow(plot_layout), name = 'Set2')] #see RColorBrewer::display.brewer.all()
plot_layout[, linetype := "solid"]
#adjust for the one factor you want to highlight
plot_layout[Species == "setosa", col := "black"]
plot_layout[Species == "setosa", linetype := "dashed"]
#create individual vectors
col_vector <- plot_layout$col
names(col_vector) <- plot_layout$Species
linetype_vector <- plot_layout$linetype
names(linetype_vector) <- plot_layout$Species
#visualise
ggplot(iris,aes(x = Petal.Length, y = Sepal.Width, col = Species, linetype = Species)) +
geom_point() +
geom_line() +
theme_classic() +
scale_color_manual(values = col_vector) +
scale_linetype_manual(values = linetype_vector)
There is probably a shorter solution. Also, check which color palette fits best for you depending on the amount of unique groups you have. The Set2
palette of RColorBrewer, for example, contains 8 unique colors.
3
there is a solution using gghighlight
which requires creating another column in the data
dataframe and then plotting:
# load libs
library("ggplot2")
library("gghighlight")
# create data frame
data <- data.frame(A = c("CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR1","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR2","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","CR3","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","Target","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR5","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR6","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7","CR7") ,TIME = c(0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18,0,0.33,0.67,1,1.5,2,3,4,7,10,13,16,18), VAL = c(0,0.2062,0.3171,0.4032,0.5071,0.5931,0.7325,0.8381,1.0332,1.0945,1.0958,1.0958,1.0893,0,0.1192,0.1822,0.2314,0.2969,0.3583,0.4605,0.5571,0.7956,0.9577,1.0322,1.042,1.0413,0,0.1934,0.3049,0.3936,0.4999,0.5915,0.7453,0.8774,1.0571,1.0694,1.0657,1.0627,1.0658,0,0.0954,0.1427,0.1846,0.2418,0.2972,0.4113,0.5353,0.8212,0.9717,1.0038,1.0002,1.004,0,0.1095,0.1491,0.1875,0.2372,0.278,0.3608,0.4382,0.6466,0.807,0.8972,0.9325,0.9476,0,0.1025,0.1499,0.1884,0.239,0.2848,0.3689,0.4538,0.6884,0.8525,0.947,0.9714,0.9743,0,0.1213,0.1741,0.2158,0.2758,0.3251,0.4297,0.5286,0.785,0.9463,1.0264,1.044,1.0541))
# either use the A column for highlighting
ggplot (data, aes(x=TIME, y=VAL, color=A)) +
geom_point() +
# this is where you specify how you want the highlighted line to be
geom_line(linewidth=1.5, color='cornflowerblue') +
gghighlight(A == "CR1") +
theme_classic()
# or add a column for the highlighting for a
# group that should be highlighted
data$highlight <- ifelse(data$A=="CR1", 1, 0)
# then use this new col for highlighting
ggplot (data, aes(x=TIME, y=VAL, color=A)) +
geom_point() +
# this is where you specify how you want the highlighted line to be
geom_line(linewidth=1.5, color='cornflowerblue') +
gghighlight(highlight == 1) +
theme_classic()
however, this method returns a warning and i think it is because highlight
is not called within the ggplot’s aes
but i am not sure. worth giving it a shot.
2