我将首先说使用红色作为颜色来表示一件事,而已经使用颜色来表示另一件事是令人困惑的。
您可以在兴趣点周围画一个红色圆圈。或者添加一个箭头。
正如建议的那样,gghighlight 可能会为您提供一个选项。也可能不会。
不想根据我的个人喜好重新绘制你的整个图... ...我可以建议来自 ggbeeswarm 的 geom_beoswarm() 可以让你的图在理解数据分布方面更清晰。
好的,现在来解决根本问题。当我们没有您的数据样本时总是很棘手
require(ggpolot)
require(tidyverse)
seed(42)
someData <- tibble(
Timepoint = as.factor(rep(seq(0,8),10)),
Concentration = sample(1:100000, 90, replace=F),
Group = rep(seq(0,4), 18 )
) %>%
mutate( Sample = paste0("V",ceiling(row_number()/9)))
someData %>%
mutate(Group = as.factor(Group)) %>%
mutate(Timepoint = fct_recode(Timepoint, `Pre-vaccine` = "0",
"< 3.5 weeks after first" = "1",
"3-6 weeks after first" = "2",
"6-12 weeks after first" ="3",
"> 12 weeks after first" = "4",
"< 3 weeks after second" = "5",
"3-6 weeks after second" = "6",
"6-12 weeks after second" ="7" ,
"> 12 weeks after second" = "8")) -> someData
# You have defined some constants that aren't explained
pal <- c("V1" = "red", "0"= "Purple", "1" = "Blue", "2" = "Green", "3" = "Yellow", "4" = "Black", "5"="Pink")
# I've simply omitted breaks and minor_breaks from your code below
这只是您使用上面示例数据的图表
someData %>%
ggplot(aes(Timepoint, Concentration)) +
geom_jitter(position = position_jitter(width = 0.0001),
aes(fill = ifelse(str_detect(Sample, "V1"), Sample, Group)), # Here is where I specify the colour fill of data points if they match the study ID, if not they are coloured by 'Group'
pch = 21,
size = 2.5) +
scale_y_log10(labels = scales::comma,
limits = c(10,10000000),
#breaks = breaks,
#minor_breaks = minor_breaks
) +
theme_classic()+
labs(title = "Antibody levels",
x = "",
y = "Concentration (AU/ml)") +
annotation_logticks(base = 10, sides = "l") +
scale_fill_manual(values = pal) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.y = element_text(face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"),
legend.position = "none")
您可以简单地在第一行上添加第二个 geom_jitter,代码中最近的一行位于另一行之上,并且只需为您要突出显示的行指定填充即可达到您的要求
someData %>%
ggplot(aes(Timepoint, Concentration)) +
geom_jitter(position = position_jitter(width = 0.0001),
aes(fill = Group),
pch = 21,
size = 2.5) +
geom_jitter(position = position_jitter(width = 0.0001),
aes(fill = ifelse(str_detect(Sample, "V1"), "V1", NA)), # Here is where I specify the colour fill of data points if they match the study ID, if not they are coloured by 'Group'
pch = 21,
size = 2.5) +
scale_y_log10(labels = scales::comma,
limits = c(10,10000000),
#breaks = breaks,
#minor_breaks = minor_breaks
) +
theme_classic()+
labs(title = "Antibody levels",
x = "",
y = "Concentration (AU/ml)") +
annotation_logticks(base = 10, sides = "l") +
scale_fill_manual(values = pal) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.y = element_text(face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"),
legend.position = "none")
在我看来,更好的做法是将填充保留为“组”,但要突出显示关键数据点
# I've added a new palette that will highlight the sample of interest
pal2 <- c("V1" = "red", "V2"= NA, "V3" = NA, "V4" = NA, "V5" = NA,
"V6"=NA, "V7" = NA, "V8" = NA, "V9" = NA, "V10"= NA)
someData %>%
ggplot(aes(Timepoint, Concentration), warn) +
geom_jitter(position = position_jitter(width = 0.0001),
aes(fill = Group),
pch = 21,
size = 2.5) +
# You will get an error warning that some rows have missing values... thats becasue
# you only want to highlight some values
# If you need to - save the plot as an object using -> gg at the end
# and then suppressWarnings(print(gg))
geom_jitter(position = position_jitter(width = 0.0001),
aes( color=Sample, stroke = 1, fill = NA),
pch = 21,
size = 5) +
scale_y_log10(labels = scales::comma,
limits = c(10,10000000),
#breaks = breaks,
#minor_breaks = minor_breaks
) +
theme_classic()+
labs(title = "Antibody levels",
x = "",
y = "Concentration (AU/ml)") +
annotation_logticks(base = 10, sides = "l") +
scale_fill_manual(values = pal) +
scale_color_manual(values = pal2) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.y = element_text(face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"),
legend.position = "none")
为了我自己的放纵
require(ggbeeswarm)
someData %>%
ggplot(aes(Timepoint, Concentration)) +
geom_beeswarm(
cex=1.75,
aes(fill = Group),
pch = 21,
size = 2.5) +
scale_y_log10(labels = scales::comma,
limits = c(10,10000000),
#breaks = breaks,
#minor_breaks = minor_breaks
) +
geom_beeswarm(
cex=1.75,
aes( color=Sample, stroke = 1, fill = NA),
pch = 21,
size = 5
) +
theme_classic()+
labs(title = "Antibody levels",
x = "",
y = "Concentration (AU/ml)") +
annotation_logticks(base = 10, sides = "l") +
scale_fill_manual(values = pal) +
scale_color_manual(values = pal2) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.y = element_text(face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"),
legend.position = "none")