ggplot – highlight a single data set out of many using unique formating Format only a single dataset out of several

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật