R 打印矩阵中按列显示的面板背景色,ggplot2

R 打印矩阵中按列显示的面板背景色,ggplot2,r,ggplot2,colors,facet-grid,R,Ggplot2,Colors,Facet Grid,我想制作一个绘图矩阵,背景颜色编码与特定列相对应(即,我想要我想要的颜色,并且以特定的顺序) 我使用了这里的信息,提出了一种在RStudio中运行的ggplot2中按列对背景颜色进行颜色编码的方法 但是,以前我一直在使用一些色盲颜色(虽然我不是色盲),我一生都无法将这些颜色更改为一组新的颜色,即使我在RStudio中从全局环境中清除对象,然后在新的RStudio会话中重新打开R脚本。不知何故,旧的颜色仍然被人们记住 我所看到的输出(作为JPG图像文件)是——在运行我的代码时,您肯定会得到其他颜

我想制作一个绘图矩阵,背景颜色编码与特定列相对应(即,我想要我想要的颜色,并且以特定的顺序)


我使用了这里的信息,提出了一种在RStudio中运行的ggplot2中按列对背景颜色进行颜色编码的方法

但是,以前我一直在使用一些色盲颜色(虽然我不是色盲),我一生都无法将这些颜色更改为一组新的颜色,即使我在RStudio中从全局环境中清除对象,然后在新的RStudio会话中重新打开R脚本。不知何故,旧的颜色仍然被人们记住

我所看到的输出(作为JPG图像文件)是——在运行我的代码时,您肯定会得到其他颜色

两个问题:

  • 如何控制这些颜色/为气候标高指定特定颜色
  • 我使用的方法似乎掩盖了绘图中的网格线——在我的代码底部,您可以看到我试图使用“主题”将网格线添加回绘图中,但它不起作用,或者在标高的颜色后面。有更好的方法吗

  • 这是我的密码:

    # install.packages("ggplot2")
    library(ggplot2)
    
    #### create Data ####
    #####################
    temperature<-c(12,43,15,8,7,9,12,15,14,10,34,27,10,12,33,11,15,7,9,12,25,14,9,10,34,37,10,12,33,15,8,7,9,12,15,14,10,34,37,10,12,33,11,15,7,9,12,15,14,9,10,34,37,10,12,33,15,8,7,9,12,15,14,10,34,37,10,12,33,11,15,7,9,12,15,14,9,10,34,37,10,12,15,14,9,36,10,34,37,10)
    hour<-c(2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12)
    elevation<-c("Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast")
    region<-c("Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America")
    climate<-data.frame(region,elevation,hour,temperature) # create data frame
    
    # Ensures sequence corresponds to sequence of cave zones
    climate$elevation <-factor(climate$elevation,
                          levels = c('Alpine','Montane','Steppe','Valley','Coast'),order=TRUE)
    
    # create plot matrix, background colored by elevation
    base<-ggplot(data = climate, aes(hour, temperature)) +
      geom_rect(aes(fill = elevation),xmin = -Inf,xmax = Inf, ymin = -Inf,ymax = Inf,alpha = 0.3) +
      geom_line(color = "steelblue", size = 1) +
      geom_point(color="steelblue") +
      labs(title = "Elevation by Region & Time of Day",
           subtitle = "(Temperature)",
           y = "Mean Temperature", x = "Hour of Day") + 
      facet_grid(region ~ elevation)
    base
    
    ###Plot looks like this to me:
    https://drive.google.com/file/d/1I_5GJHOW8VPZShJ4C61Uayo8zrEMPCDZ/
    # I'm certain colors will look different for you!
    
    # Trying to add gridlines (does not work)
    base + theme(
      panel.background = element_rect(fill = "transparent",colour = NA),
      plot.background = element_rect(fill = "transparent",colour = NA),
      panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                      colour = "white"), 
      panel.grid.minor = element_blank()
    )
    
    
    #安装程序包(“ggplot2”)
    图书馆(GG2)
    ####创建数据####
    #####################
    
    温度事实上我得到了和你一样的颜色。原因是您的
    高程
    是一个有序因子。因此,
    ggplot2
    默认使用
    viridis
    调色板

  • 您可以使用
    scale\u fill\u manual
    设置填充颜色,例如命名的颜色向量

  • 网格线有两个选项。您只需使用
    geom\u h/vline
    手动添加网格线即可。或者,您可以将绘图和面板背景的
    fill
    设置为
    NA
    ,并使用
    panel.ontop
    ,它将在绘图顶部绘制面板和网格线

  • 库(ggplot2)
    cols“#E76BF3”“#00B0F6”“#00BF7D”“#a300”“#F8766D”
    
    基本事实上我得到了和你一样的颜色。原因是您的
    高程
    是一个有序因子。因此,
    ggplot2
    默认使用
    viridis
    调色板

  • 您可以使用
    scale\u fill\u manual
    设置填充颜色,例如命名的颜色向量

  • 网格线有两个选项。您只需使用
    geom\u h/vline
    手动添加网格线即可。或者,您可以将绘图和面板背景的
    fill
    设置为
    NA
    ,并使用
    panel.ontop
    ,它将在绘图顶部绘制面板和网格线

  • 库(ggplot2)
    cols“#E76BF3”“#00B0F6”“#00BF7D”“#a300”“#F8766D”
    
    基础默认情况下使用viridis绘图?我以为这是比例、颜色、填充、色调……一个人永远不会停下来学习。事实上,我偶尔会感到惊讶,为什么钻石数据会以不同的颜色绘制lol(从来没有质疑过它)@tjebo哈哈。我也是。首先,我寻找一些奇怪的东西。然后我重新启动了我的R会话。然后我重新启动了R,但仍然是绿色的。。。然后我看到了factor语句中的
    order=TRUE
    参数。哇,谢谢,这非常有用。是否有可能获得打印数据后面和背景色前面的网格线?不管怎么说,你让我更接近一个职业形象。我学到了很多东西!嗨,史蒂夫。据我所知:不幸的是,没有。这就是
    panel.ontop
    的缺点,也是使用
    geom\u h/vline
    添加第二种方法的原因。最好的。。。。默认情况下使用viridis绘图?我以为这是比例、颜色、填充、色调……一个人永远不会停下来学习。事实上,我偶尔会感到惊讶,为什么钻石数据会以不同的颜色绘制lol(从来没有质疑过它)@tjebo哈哈。我也是。首先,我寻找一些奇怪的东西。然后我重新启动了我的R会话。然后我重新启动了R,但仍然是绿色的。。。然后我看到了factor语句中的
    order=TRUE
    参数。哇,谢谢,这非常有用。是否有可能获得打印数据后面和背景色前面的网格线?不管怎么说,你让我更接近一个职业形象。我学到了很多东西!嗨,史蒂夫。据我所知:不幸的是,没有。这就是
    panel.ontop
    的缺点,也是使用
    geom\u h/vline
    添加第二种方法的原因。最佳S。