Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在ggplot的条带背景中制作渐变色?_R_Ggplot2_Gradient - Fatal编程技术网

R 如何在ggplot的条带背景中制作渐变色?

R 如何在ggplot的条带背景中制作渐变色?,r,ggplot2,gradient,R,Ggplot2,Gradient,我尝试在ggplot2()包的strip.background()函数中的填充参数中创建颜色渐变 下面是我想要的图片: 以及我尝试使用的代码: # Charge random data data('mtcars') # Create fake variable in order to create title into coloured box mtcars$tempvar <- "My title" # Run the ggboxplot ggboxplot(mtcars, x =

我尝试在
ggplot2()
包的
strip.background()函数中的填充参数中创建颜色渐变

下面是我想要的图片:

以及我尝试使用的代码:

# Charge random data
data('mtcars')

# Create fake variable in order to create title into coloured box
mtcars$tempvar <- "My title"

# Run the ggboxplot
ggboxplot(mtcars, x = "cyl", y = "qsec", 
          color = "cyl", 
          palette = c("#E7B800", "#FC4E07",  "#00AFBB"),
          ylab = "cyl", xlab = "qsec",
          legend = "none") + 

 facet_grid(. ~ tempvar) +
  theme(strip.background = element_rect(fill="darkgreen"),
        strip.text = element_text(size=15, colour="white") )
#充电随机数据
数据(‘mtcars’)
#创建伪变量,以便在彩色框中创建标题

mtcars$tempvar技术上?这是可以做到的。(无论是垃圾还是良好实践都是另一回事……)

2.强制
ggplot
strip.background
/
strip.background.x
/
strip.background.y接受
element\u梯度
,而不是
element\u rect
: 与

  • 运行
    跟踪(ggplot2:::验证\u元素,编辑=TRUE)
    并替换
  • 4.完成后,运行以下操作以结束疯狂状态并恢复正常
    ggplot
    行为:
    ggplot\u global.new$element\u tree$strip.background?这是可以做到的。(无论是垃圾还是良好实践都是另一回事……)

    2.强制
    ggplot
    strip.background
    /
    strip.background.x
    /
    strip.background.y接受
    element\u梯度
    ,而不是
    element\u rect
    : 与

  • 运行
    跟踪(ggplot2:::验证\u元素,编辑=TRUE)
    并替换
  • 4.完成后,运行以下操作以结束疯狂状态并恢复正常
    ggplot
    行为:
    ggplot\u global.new$element\u tree$strip.background我怀疑是否有合适的编程方法,因为ggplot(可能是有意)没有方法进行非数据颜色渐变。但是就像所有的东西一样,你可能会找到一个破解或者解决方法。你可以用一个颜色渐变来制作一个单独的绘图,并将它放置在条带上。这将是乏味的-值得怀疑的是,我怀疑是否有一个适当的方式来编程,因为ggplot(可能是故意的)没有一种方式来进行非数据颜色渐变。但是就像所有的东西一样,你可能会找到一个破解或者解决方法。你可以用一个颜色渐变来制作一个单独的绘图,并将它放置在条带上。这将是乏味的-值得怀疑的是它是否值得付出努力同意“chartjunk或good practice”标志,但哇!非常令人印象深刻+1赞同“垃圾或良好实践”标志,但哇!非常令人印象深刻+1.
    
    p <- ggplot(mtcars, aes(mpg, drat)) + 
      geom_point() + 
      facet_grid(~"facet title") 
    
    # basic demonstration
    p +
      theme(strip.background = 
              element_gradient(fill1 = "black", fill2 = "white"))
    
    # with different gradient direction & outline specifications
    p +
      theme_minimal() +
      theme(strip.background = 
              element_gradient(fill1 = "brown", fill2 = "salmon",
                               direction = "vertical",
                               color = "white", size = 2,
                               linetype = "dotted"))
    
    # with horizontal / vertical facets in different gradient directions
    p + facet_grid("vertical title" ~ "horizontal facet title") +
      theme_classic() +
      theme(strip.background = element_gradient(fill1 = "green", fill2 = "yellow", size = 1),
            strip.background.y = element_gradient(direction = "vertical"))
    
    element_gradient <- function(fill1 = NULL, fill2 = NULL, direction = NULL,
                                 colour = NULL, size = NULL,
                                 linetype = NULL, color = NULL, inherit.blank = FALSE) {
      if (!is.null(color))  colour <- color
      structure(
        list(fill1 = fill1, fill2 = fill2, direction = direction,
             colour = colour, size = size, linetype = linetype,
             inherit.blank = inherit.blank),
        class = c("element_gradient", "element")
      )
    }
    
    element_grob.element_gradient <- function(
      element, 
      fill1 = "white", fill2 = "red", direction = "horizontal", # default: white-red gradient
      x = 0.5, y = 0.5, width = 1, height = 1, colour = NULL, 
      size = NULL, linetype = NULL, ...) {
    
      # define gradient colours & direction
      if(!is.null(element$fill1)) fill1 <- element$fill1
      if(!is.null(element$fill2)) fill2 <- element$fill2
      if(!is.null(element$direction)) direction <- element$direction  
      image <- colorRampPalette(c(fill1, fill2))(2)  
      if(direction == "horizontal") {
        image <- matrix(image, nrow = 1)
      } else {
        image <- matrix(image, ncol = 1)
      }
    
      gp <- grid::gpar(lwd = ggplot2:::len0_null(size * .pt), col = colour, lty = linetype)
      element_gp <- grid::gpar(lwd = ggplot2:::len0_null(element$size * .pt), col = element$colour,
                         lty = element$linetype, fill = NA)  
      grid::grobTree(
        grid::rasterGrob(image, x, y, width, height, ...),
        grid::rectGrob(x, y, width, height, gp = utils::modifyList(element_gp, gp), ...))
    }
    
    # make a copy of ggplot's global variables / settings, & modify its element_tree
    ggplot_global.new <- ggplot2:::ggplot_global
    ggplot_global.new$element_tree$gradient <- ggplot2:::el_def("element_gradient")
    ggplot_global.new$element_tree$strip.background <- ggplot2:::el_def("element_gradient", "gradient")
    ggplot_global.new$element_tree$strip.background.x <- ggplot2:::el_def("element_gradient", "strip.background")
    ggplot_global.new$element_tree$strip.background.y <- ggplot2:::el_def("element_gradient", "strip.background")
    
    if (!inherits(new, class(old)[1])) {
      stop("Only elements of the same class can be merged", call. = FALSE)
    }
        
    
    if (!inherits(new, class(old)[1]) & class(new)[1] != "element_gradient") {
      stop("Only elements of the same class can be merged", call. = FALSE)
    }
    
    else if (!inherits(el, eldef$class) && 
             !inherits(el, "element_blank")) {
      stop("Element ", elname, " must be a ", eldef$class, " object.")
    }
    
    else if (!inherits(el, eldef$class) && 
             !inherits(el, "element_blank") && 
             eldef$class != "element_gradient") {
      stop("Element ", elname, " must be a ", eldef$class, " object.")
    
    ggplot_global.new$element_tree$strip.background <- ggplot2:::el_def("element_rect", "rect")
    ggplot_global.new$element_tree$strip.background.x <- ggplot2:::el_def("element_rect", "strip.background")
    ggplot_global.new$element_tree$strip.background.y <- ggplot2:::el_def("element_rect", "strip.background")
    
    untrace(ggplot2:::merge_element.element)
    untrace(ggplot2:::validate_element)