我正在嘗試將圖例添加到幾個 geom_point。雖然,因為我有三個 geom_points 標簽只出現一個變數(“結果”)。
除了“結果”變數之外,我還想顯示兩個菱形的標簽:藍色菱形(“TStartTime”)和綠色(“指標”)。

# Code for numbers reproduction
df <- data.frame(subjectID = factor(1:10, 10:1),
stage = rep(c("treated"), times = c(10)),
endTime = c(6, 8, 3, 5, 10, 14, 2, 12, 6, 6),
Outcome = rep(c("healthy", "disability", "healthy", "disability", NA, NA, NA, NA, "healthy", "disability"), 1),
TStartTime=c(1.0, 1.5, 0.3, 0.9, NA, NA, NA, NA, NA, NA),
TEndTime=c(6.0, 7.0, 1.2, 1.4, NA, NA, NA, NA, NA, NA),
TimeZero=c(0,0,0,0,0,0,0,0,0,0),
ind=rep(c(!0, !0, !0, !0, !0), times = c(2, 2, 2, 2, 2)),
Garea=c(1.0, 1.5, 0.3, 0.9, 2, 2, NA, NA, NA, NA),
indicator=c(NA, NA, NA, NA, 4, 1, 5, 2, NA, NA))
# Code for the plot
gg <- ggplot(df, aes(subjectID, endTime))
scale_fill_manual(values = c("khaki", "orange"))
geom_col(aes(fill = factor(stage)))
geom_point(data=df, aes(subjectID, TStartTime), colour = c("blue"), fill =alpha(c("#FAFAFA"), 0.2), shape=18, size=4)
coord_flip() # blue diamond
geom_point(data=df, aes(subjectID, indicator), colour = c("green"), shape=18, size=4)
coord_flip() # green diamond for indicator
geom_point(aes(colour = Outcome, shape = Outcome), size = 4)
coord_flip()
scale_colour_manual(values = c('purple','gray'), na.translate=FALSE)
scale_y_continuous(limits = c(-0.2, 15), breaks = 0:15)
labs(labels= "",
x = "ID ",
fill = "Status",
y = "Days",
title = "Plot")
theme_classic()
theme(plot.title = element_text(hjust = 0.5),
plot.caption = element_text(size = 7, hjust = 0))
uj5u.com熱心網友回復:
您基本上是在為相同的美學尋找第二個尺度。ggnewscale 是你的朋友。代碼中的許多其他注釋。特別是,您已經多次呼叫 coord_flip,這不是必需的,甚至可能很危險。我會完全避免 coord_flip (請參閱我在代碼中的評論如何做到這一點)。
除了所有這些技術方面 - 你的可視化對我來說似乎并不理想,而且相當混亂。我想知道是否可能沒有更直觀的方式來呈現您的各種變數 - 也許考慮方面。下面給個建議。
library(tidyverse)
library(ggnewscale)
df <- data.frame(
subjectID = factor(1:10, 10:1),
stage = rep(c("treated"), times = c(10)),
endTime = c(6, 8, 3, 5, 10, 14, 2, 12, 6, 6),
Outcome = rep(c("healthy", "disability", "healthy", "disability", NA, NA, NA, NA, "healthy", "disability"), 1),
TStartTime = c(1.0, 1.5, 0.3, 0.9, NA, NA, NA, NA, NA, NA),
TEndTime = c(6.0, 7.0, 1.2, 1.4, NA, NA, NA, NA, NA, NA),
TimeZero = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
ind = rep(c(!0, !0, !0, !0, !0), times = c(2, 2, 2, 2, 2)),
Garea = c(1.0, 1.5, 0.3, 0.9, 2, 2, NA, NA, NA, NA),
indicator = c(NA, NA, NA, NA, 4, 1, 5, 2, NA, NA)
)
# pivot longer so you can combine tstarttime and indicator into one legend easily
df %>%
pivot_longer(cols = c(TStartTime, indicator)) %>%
# remove all the coord_flip calls (you only need one, if not none!)
ggplot()
scale_fill_manual(values = c("khaki", "orange"))
# just change the x/y aesthetic in geom_col
# geom_col would add all values together, so you need to use the un-pivoted data
geom_col(data = df, mapping = aes(y = subjectID, x = endTime, fill = factor(stage)))
# now you only need one geom_point for the new scale, but use the variable in aes()
geom_point(aes(y = subjectID, x = value, colour = name), shape = 18, size = 4)
scale_color_manual(values = c("blue", "green"))
# now add a new scale for the same aesthetic (color)
new_scale_color()
geom_point(aes(y = subjectID, x = endTime, colour = Outcome, shape = Outcome), size = 4)
## removing na.translate = FALSE avoids the duplicate legend for outcome
scale_colour_manual(values = c("purple", "gray"))
#> Warning: Removed 12 rows containing missing values (geom_point).
#> Warning: Removed 8 rows containing missing values (geom_point).

可視化更少的維度/變數有時會更好。這里有一個建議,如何避免雙重比例以獲得相同的美感,并使用你的顏色可能更令人信服。我覺得使用條形可能也不理想,但這實際上取決于變數“indicator/ttimestart”是什么以及它與結束時間的關系。一個好的目標是顯示這兩個變數之間的關系。
df %>%
pivot_longer(cols = c(TStartTime, indicator)) %>%
ggplot()
## all of them are treated, so I am using Outcome as fill variable
# this removes the need for second geom-point and second scale
geom_col(data = df, mapping = aes(y = subjectID, x = endTime, fill = Outcome))
scale_fill_manual(values = c("purple", "gray"))
geom_point(aes(y = subjectID, x = value, colour = name), shape = 18, size = 4)
scale_color_manual(values = c("blue", "green"))
## if you have untreated people, show them in a new facet, e.g., add
facet_grid(~stage)
#> Warning: Removed 12 rows containing missing values (geom_point).

由reprex 包于 2022-05-05 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471594.html
