ggplot - 使用独特的格式突出显示多个数据集中的单个数据集仅格式化多个数据集中的单个数据集

问题描述 投票:0回答:1

交流中也有类似的问题,但我认为它们不适用于我在这里尝试做的事情。

我有一个包含八个数据集的点线图。我只想突出显示其中一个,然后让软件自动着色其余部分。这很重要,因为我可能会遇到超过八个的情况。我不想手动设置颜色,只需将我想要的颜色设置为我指定的颜色和样式即可。有没有快速的方法来实现这一点?

在下图中,我想将 CR5 设为黑色和虚线作为示例。


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()

enter image description here

r ggplot2 plot
1个回答
0
投票

这样的东西有帮助吗?

#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)

image

可能有一个更短的解决方案。另外,根据您拥有的独特组的数量,检查哪种调色板最适合您。例如,RColorBrewer 的

Set2
调色板包含 8 种独特的颜色。

© www.soinside.com 2019 - 2024. All rights reserved.