在遵循 Stefan 对这篇文章的非常有用的回答(他使用的地方)之后ggnewscale::new_scale()
,我现在遇到了以下问题:
“如何将自定义图例排列成ggnewscale
多个垂直列?”
就像通常使用 ggplot2 中的命令完成的那样guides(scale_shape_manual=guide_legend(ncol=2))
。
最小可重现示例:
# fictional data in the scheme of /sf/ask/4676314121/
mutated <- list()
for(i in 1:10) {
mutated[[i]] <- data.frame(Biological.Replicate = rep(1,4),
Reagent.Conc = c(10000, 2500, 625, 156.3),
Reagent = rep(1,8),
Cell.type = rep(LETTERS[i],4),
Mean.Viable.Cells.1 = rep(runif(n = 10, min = 0, max = 1),4))
}
mutated <- do.call(rbind.data.frame, mutated)
Run Code Online (Sandbox Code Playgroud)
用户“stefan”回答后修改后的代码如下所示:
# from /sf/answers/4676602661/
library(ggplot2)
library(ggnewscale)
library(dplyr)
library(magrittr)
mutated <- mutated %>%
mutate(Cell.type = as.factor(Cell.type),
Reagent = …
Run Code Online (Sandbox Code Playgroud) 我使用此代码生成的图给出了这个图
问题是我无法匹配图中出现的顺序
我的代码
p <- df %>%
ggplot(aes(name, perc)) +
geom_col(data = ~ filter(.x, name == "FAB") %>% rename(FAB = value), mapping = aes(fill = FAB)) +
#scale_fill_manual(values = cols)+
new_scale_fill() +
geom_col(data = ~ filter(.x, name == "Sex") %>% rename(Sex = value), mapping = aes(fill = Sex)) +
new_scale_fill() +
geom_col(data = ~ filter(.x, name == "Age") %>% rename(Age = value), mapping = aes(fill = Age)) +
new_scale_fill() +
geom_col(data = ~ filter(.x, name == "BM_percentage") %>% rename(BM_percentage = …
Run Code Online (Sandbox Code Playgroud) 我需要能够检测传递给函数的 ggplot 是否已经添加了scale_linetype_manual
一个,以便我知道是否使用并向其ggnewscale
添加另一个。new_scale("linetype")