我有一个非常简单的问题。我希望当前红色的框为蓝色,而蓝色的框为红色。我希望传奇保持现在的样子。我怎样才能最好地实现这一目标?
labels1 <- c("Male sex", "Male sex", "Age", "Age", "Body mass index", "Body mass index" , "SBP", "SBP", "Black", "Black", "Asian", "Asian", "Deprivation", "Deprivation")
labels1 <- factor(labels1, levels = labels1)
Joint <- c("Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee")
Joint <- factor(Joint, levels = Joint)
#rr <- c(1.02, 0.79, 2.18, 2.45, 2.93, 1.70, 0.98, 0.98, 0.98, 0.42, 1.32, 0.26, 0.99, 0.99)
#rrlow <- c(0.98, 0.76, 2.11, 2.36, 2.83, 1.62, 0.97, 0.97, 0.83, 0.31, 1.14, 0.19, 0.98, 0.98)
#rrhigh <- c(1.06, 0.83, 2.25, 2.54, 3.03, 1.78, 0.99, 1.00, 1.17, 0.57, 1.52, 0.37, 1.00, 1.00)
rr <- c(0.79, 1.02, 2.45, 2.18, 1.70, 2.93, 0.98, 0.98, 0.42, 0.98, 0.26, 1.32, 0.99, 0.99)
rrlow <- c(0.76, 0.98, 2.36, 2.11, 1.62, 2.83, 0.97, 0.97, 0.31, 0.83, 0.19, 1.14, 0.98, 0.98)
rrhigh <- c(0.83, 1.06, 2.54, 2.25, 1.78, 3.03, 1.00, 0.99, 0.57, 1.17, 0.37, 1.52, 1.00, 1.00)
x <- c(2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13)
rr <- rr[x]
rrlow <- rrlow[x]
rrhigh <- rrhigh[x]
forestdf <- data.frame(labels1, rr, rrhigh, rrlow, Joint)
#fplottable <- data.frame(labels1, figure1table[c(-16,-17), "rrfull"])
#fplottable <- data.frame(labels1, figure1table[c(-16,-17), "rrfull"])
#colors
dotCOLS = c("red3", "dodgerblue4")
barCOLS = c("red3", "dodgerblue4")
forestdf$color <- c(rep(c("white", "gray95"), 7))
p <- ggplot(forestdf, aes(x=rr, y=labels1, xmin=rrlow, xmax=rrhigh))+
geom_pointrange(shape=22, position = position_dodge(width = 0.75), aes(col = Joint, fill = Joint))+
geom_vline(xintercept = 1, linetype=3)+
#scale_colour_identity() +
xlab("Adjusted Hazard Ratio with 95% Confidence Interval")+theme_bw()+scale_y_discrete(limits=rev(labels1))+
scale_x_log10(limits = c(0.125, 4), breaks = c(0.125, 0.25, 0.5, 1, 2, 4), labels=c("0.125", "0.25", "0.5", "1", "2", "4"), expand = c(0,0))+
theme(axis.title.y=element_blank(), axis.text=element_text(size=11, color = "black"))+
theme(plot.margin = margin(30, 5, 10, 12)) +
scale_fill_manual(values = rev(dotCOLS))+scale_color_manual(values = rev(barCOLS))
p
答案 0 :(得分:0)
在您的 scale_fill_manual
和 scale_color_manual
中,您都在颠倒您指定的颜色值的顺序。如果您将 rev
放在值周围,颜色将切换。这是您想要达到的目标吗?
**labels1 <- c("Male sex", "Male sex", "Age", "Age", "Body mass index", "Body mass index" , "SBP", "SBP", "Black", "Black", "Asian", "Asian", "Deprivation", "Deprivation")
labels1 <- factor(labels1, levels = labels1)
Joint <- c("Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee", "Hip", "Knee")
Joint <- factor(Joint, levels = Joint)
#rr <- c(1.02, 0.79, 2.18, 2.45, 2.93, 1.70, 0.98, 0.98, 0.98, 0.42, 1.32, 0.26, 0.99, 0.99)
#rrlow <- c(0.98, 0.76, 2.11, 2.36, 2.83, 1.62, 0.97, 0.97, 0.83, 0.31, 1.14, 0.19, 0.98, 0.98)
#rrhigh <- c(1.06, 0.83, 2.25, 2.54, 3.03, 1.78, 0.99, 1.00, 1.17, 0.57, 1.52, 0.37, 1.00, 1.00)
rr <- c(0.79, 1.02, 2.45, 2.18, 1.70, 2.93, 0.98, 0.98, 0.42, 0.98, 0.26, 1.32, 0.99, 0.99)
rrlow <- c(0.76, 0.98, 2.36, 2.11, 1.62, 2.83, 0.97, 0.97, 0.31, 0.83, 0.19, 1.14, 0.98, 0.98)
rrhigh <- c(0.83, 1.06, 2.54, 2.25, 1.78, 3.03, 1.00, 0.99, 0.57, 1.17, 0.37, 1.52, 1.00, 1.00)
x <- c(2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13)
rr <- rr[x]
rrlow <- rrlow[x]
rrhigh <- rrhigh[x]
forestdf <- data.frame(labels1, rr, rrhigh, rrlow, Joint)
#fplottable <- data.frame(labels1, figure1table[c(-16,-17), "rrfull"])
#fplottable <- data.frame(labels1, figure1table[c(-16,-17), "rrfull"])
#colors
dotCOLS = c("red3", "dodgerblue4")
barCOLS = c("red3", "dodgerblue4")
forestdf$color <- c(rep(c("white", "gray95"), 7))
p <- ggplot(forestdf, aes(x=rr, y=labels1, xmin=rrlow, xmax=rrhigh))+
geom_pointrange(shape=22, position = position_dodge(width = 0.75), aes(col = Joint, fill = Joint))+
geom_vline(xintercept = 1, linetype=3)+
#scale_colour_identity() +
xlab("Adjusted Hazard Ratio with 95% Confidence Interval")+theme_bw()+scale_y_discrete(limits=rev(labels1))+
scale_x_log10(limits = c(0.125, 4), breaks = c(0.125, 0.25, 0.5, 1, 2, 4), labels=c("0.125", "0.25", "0.5", "1", "2", "4"), expand = c(0,0))+
theme(axis.title.y=element_blank(), axis.text=element_text(size=11, color = "black"))+
theme(plot.margin = margin(30, 5, 10, 12)) +
scale_fill_manual(values = dotCOLS) +
scale_color_manual(values = barCOLS)
p