• R语言编程之分别为正负条形图着色


    library(gcookbook) # For the data set
    
    csub <- subset(climate, Source=="Berkeley" & Year >= 1900)
    
    csub$pos <- csub$Anomaly10y >= 0
    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    library(gcookbook) # For the data set
    
    csub <- subset(climate, Source=="Berkeley" & Year >= 1900)
    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity", colour="black", size=0.25) +
      
      scale_fill_manual(values=c("#CCEEFF", "#FFDDDD"), guide=FALSE)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述
    颜色改成蓝色,颜色值也跟着改

    library(gcookbook) # For the data set
    
    csub <- subset(climate, Source=="Berkeley" & Year >= 1900)
    
    csub$pos <- csub$Anomaly10y >= 0
    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="dodge")
    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity", colour="blue", size=0.25) +
      
      scale_fill_manual(values=c("#CCEFFF", "#FFDDAD"), guide=FALSE)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    library(gcookbook) # For the data set
    
    csub <- subset(climate, Source=="Berkeley" & Year >= 1900)
    
    csub$pos <- csub$Anomaly10y >= 0
    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="dodge")
    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity", colour="cyan", size=0.25) +
      
      scale_fill_manual(values=c("#CCEFFF", "#FFDDAD"), guide=FALSE)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity", colour="brown", size=0.25) +
      
      scale_fill_manual(values=c("#CCEFFF", "#FFDDAD"), guide=FALSE)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity", colour="pink", size=0.25) +
      
      scale_fill_manual(values=c( "purple4", "red4"), guide=FALSE)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
      
      geom_bar(stat="identity", position="identity", colour="white", size=0.25) +
      
      scale_fill_manual(values=c( "cyan4", "black"), guide=FALSE)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    参考文献:R数据科学—[新西兰]哈德利 威克姆,[美]加勒特 格罗勒芒德—陈光欣[译] 北京:人民邮电出版社

    百度文库—颜色大全:含中英文对照及色值

  • 相关阅读:
    使用统一功能完善图书管理系统
    MyBatisPlus中使用Mybatis方式操作数据库。
    【目标检测】YOLOv5在Android上的部署
    5个前端练手项目(html css js canvas)
    LeetCode_栈_困难_227.基本计算器(不含乘除)
    ASM字节码操作类库(打开java语言世界通往字节码世界的大门) | 京东云技术团队
    cesium 三维坐标系绘制
    Linux搭建我的世界服务器 + 公网远程联机教程「不需要公网IP」
    redis过期删除及内存淘汰策略
    MySQL—MySQL的存储引擎之InnoDB
  • 原文地址:https://blog.csdn.net/m0_38127487/article/details/127694861