简体   繁体   English

调整不同 label 区域的多重填充(颜色)

[英]Adjust the multiple fills(color) of different label regions

0 0

Forgive my stupid to disturb you again.原谅我的笨蛋再次打扰你。

@teunbrand answered my question yesterday and I used it in my real data but it doesn't work. @teunbrand 昨天回答了我的问题,我在真实数据中使用了它,但它不起作用。

Here is my question in stackoverfow:Can I adjust the fill(color) of different label regions when using ggh4x package这是我在stackoverfow中的问题:使用ggh4x package时,我可以调整不同label区域的填充(颜色)吗

And @ teunbrand created a function: assign_strip_colours <- function(gt, index, colours){…}而@teunbrand 创建了一个 function:assign_strip_colours <- function(gt, index, colours){…}

I don't know where is wrong with my real data and code.我不知道我的真实数据和代码哪里出了问题。 There are 42 regions need to be filled with different colors.有42个区域需要填充不同的colors。

gt <- assign_strip_colours(gt, 1:42,rainbow(42)) Warning message: In gt$grobs[is_strips] <- strips : 被替换的项目不是替换值长度的倍数(The item being replaced is not a multiple of the length of the replacement value. ) ?

If there is sth need to be adjust in assign_strip_colours <- function(gt, index, colours){…}?如果需要在 assign_strip_colours <- function(gt, index, colours){...} 中进行调整?

Forgive me I'm really new to ggplotGrob.请原谅我对 ggplotGrob 真的很陌生。 I need your help.Thanks.我需要你的帮助。谢谢。

sample data and code:示例数据和代码:

structure(list(Name = 1:71, Disease = 72:142, Organ = c("A", 

"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"), fill = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a" ), mean =..., row.names = c(NA, 71L), class = "data.frame") “A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A” "、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、 “A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A” "、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、"A"、 “A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A”、“A” ", "A", "A", "A", "A", "A", "A", "A"), 填充 = c("a", "a", "a", "a" ,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“ “一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一” ,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“ “一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一”、“一” ,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“一”,“ a", "a", "a", "a", "a"), 平均 =..., row.names = c(NA, 71L), class = "data.frame")

p1<-ggplot(data = data, aes(Name,mean, label = Name, fill=Organ)) +
  geom_bar(position="dodge2", stat="identity",width = 0.85,color="black") +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.95), width = .2) +
  #  scale_alpha_manual(values = datamean_sd$Alpha) +
  #  scale_color_manual(name = "Organ", values = c("A"="#f15a24", "B"="#00FF00","C"="#7570B3","D"="#FF00FF","E"="#FFFF33","F"="#00F5FF","G"="#666666","H"="#7FC97F","I"="#BEAED4","J"="#A6D854"))+
  #  guides(
  #    colour = guide_legend(title.position = "right")
  # )+
  facet_nested(.~Organ+Disease, scales = "free_x", space = "free_x",switch = "x")+
  ##  facet_wrap(strip.position="bottom") +
  labs(title = "123", x = NULL, y = "value") +
  rotate_x_text(angle = 45)+
  scale_fill_manual(name = "Organ",values = unique(datamean_sd$Organ_fill))
p1
####
gt <- ggplotGrob(p1)
###############
assign_strip_colours <- function(gt, index, colours) {
  if (length(index) != length(colours))
    stop()
  
  # Decide which strips to recolour, here: the first 3
  is_strips <- which(startsWith(gt$layout$name, "strip-b"))[index]
  # Extract strips
  strips <- gt$grobs[is_strips]
  # Loop over strips
  strips <- mapply(function(strip, colour) {
    # Find actual strip
    is_strip <- strip$layout$name == "strip"
    grob <- strip$grobs[is_strip][[1]]
    # Find rectangle
    is_rect <- which(vapply(grob$children, inherits, logical(1), "rect"))
    # Change colour
    grob$children[[is_rect]]$gp$fill <- colour
    # Put back into strip
    strip$grobs[is_strip][[1]] <- grob
    return(strip)
  }, strip = strips, colour = colours)
  # Put strips back into gtable
  gt$grobs[is_strips] <- strips
  return(gt)
}

gt <- assign_strip_colours(gt, 1:42,rainbow(42))
grid::grid.newpage(); grid::grid.draw(gt)

My bad, I think there should have been a SIMPLIFY = FALSE at the mapply() function which I forgot earlier.我的错,我认为mapply() function 应该有一个SIMPLIFY = FALSE我之前忘记了。


gt <- ggplotGrob(p1)

assign_strip_colours <- function(gt, index, colours) {
  if (length(index) != length(colours))
    stop()
  
  # Decide which strips to recolour, here: the first 3
  is_strips <- which(startsWith(gt$layout$name, "strip-b"))[index]
  # Extract strips
  
  strips <- gt$grobs[is_strips]
  # Loop over strips
  strips <- mapply(function(strip, colour) {

    # Find actual strip
    is_strip <- strip$layout$name == "strip"
    grob <- strip$grobs[is_strip][[1]]
    # Find rectangle
    is_rect <- which(vapply(grob$children, inherits, logical(1), "rect"))
    # Change colour
    grob$children[[is_rect]]$gp$fill <- colour
    # Put back into strip
    strip$grobs[is_strip][[1]] <- grob
    return(strip)
  }, strip = strips, colour = colours, SIMPLIFY = FALSE)
  # Put strips back into gtable
  gt$grobs[is_strips] <- strips
  return(gt)
}

gt <- assign_strip_colours(gt, 1:42,rainbow(42))
grid::grid.newpage(); grid::grid.draw(gt)

Created on 2021-04-11 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 4 月 11 日创建

Data / plot construction:数据 / plot 结构:

library(ggplot2)
library(ggh4x)

data <- [Censored upon request]

p1<-ggplot(data = data, aes(Name,mean, label = Name, fill=Organ)) +
  geom_bar(position="dodge2", stat="identity",width = 0.85,color="black") +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.95), width = .2) +
  facet_nested(.~Organ+Disease, scales = "free_x", space = "free_x",switch = "x")+
  theme_classic() +
  theme(legend.position = "bottom",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5),
        plot.margin = unit(c(5, 10, 20, 7), "mm"),
        strip.background = element_rect(colour="black", fill="white"),
        strip.text.x = element_text(size = 6, angle=0),
        axis.text.x=element_text(size=8),
        strip.placement = "outside"
  ) +
  labs(title = "123", x = NULL, y = "value")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM