[英]Change colour of a specific diamond on a forest plot using ggplot2
我正在嘗試使用 ggplot 在我的森林 plot 上突出顯示變量的一個特定鑽石,但我找不到如何包含它。 我正在嘗試更改"Periodontitis (Yes)"
的顏色。
dataset_unstratified$variables <- factor(dataset_unstratified$variables
, levels = c("Intercept (Skeletal Muscle Mass Index)","Periodontitis (Yes)", "Sex(Men)", "Age", "Body-Mass Index", "Smoker", "Former Smoker",
"Diabetes","Prediabetes","Education (Low)","Education (Medium)","Daily energy intake",
"Daily protein intake", "Vitamin D2 and D3", "Bone Mineral Density"))
dataset_unstratified <- dataset_unstratified[order(dataset_unstratified$variables), ]
plot_width <- 100
wrapper <- function(x) {
paste(strwrap(x, width = plot_width), collapse = "\n")
}
plot1 <- ggplot(dataset_unstratified, aes(y=variables, x=coefficients)) +
geom_point(shape = 18, size = 5) +
theme(axis.title.y = element_blank()) +
xlab("Beta-coefficients with 95% Confidence Intervals") +
ylab("Model variables") + theme(axis.title.y = element_blank()) +
labs(title = wrapper("a) SMMI and periodontitis"))+
geom_errorbarh(aes(xmin = `2.5 %`, xmax = `97.5 %`), height = 0.25) +
geom_vline(xintercept = 0, color = "blue", linetype = "dashed", cex = 1, alpha = 0.5)
您的代碼目前不可重現,但您應該能夠通過創建無聊灰色 colors 的調色板,然后根據特定級別的 a 將“突出顯示”顏色(例如,紅色)注入該調色板來實現您想要的 output因素(例如, hp
)。 基於這篇文章。
library(tidyverse)
library(RColorBrewer)
data(mtcars)
# make a long dataframe
df <- mtcars %>%
rownames_to_column(var = "rowname") %>%
pivot_longer(cols = -rowname) %>%
mutate(name = as.factor(name))
ref <- "hp" # specify the name of the level you want to
varlength <- length(levels(df$name)) # specify the variable that goes on the final y-axis
myColors <- colorRampPalette(brewer.pal(8, "Greys"))(varlength) # get a bunch of gray colors
names(myColors) <- levels(df$name)
myColors[names(myColors)==ref] <- "red" # specify your "highlight" color
colScale <- scale_color_manual(name = "grp", values = myColors) # turn into a ggplot object
ggplot(df, aes(y = name, x = value, color = name)) +
geom_point(shape = 18, size = 5) + colScale
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.