楼主: zhjfj
2902 1

关于R语言对正负条形图着色(参考R数据可视化手册) [推广有奖]

  • 0关注
  • 0粉丝

高中生

95%

还不是VIP/贵宾

-

威望
0
论坛币
15 个
通用积分
15.8129
学术水平
2 点
热心指数
2 点
信用等级
2 点
经验
3035 点
帖子
13
精华
0
在线时间
46 小时
注册时间
2019-8-11
最后登录
2020-5-28

楼主
zhjfj 发表于 2019-9-7 20:18:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
具体代码:
install.packages("gcookbook")
install.packages("ggplot2")
library(gcookbook)
library(ggplot2)
csub<-subset(climate,Source=="Berkeley"&Year>=1900)   #记住,这个Source的S一定要大写,因为它来源于gcookbook包的                                                                                        climate中的数据
csub$pos<-csub$Anomaly10y>=0    #变量pos对取值正负情况进行标识

csub
ggplot(csub,aes(x=Year,y=Anomaly10y,fill=pos))+geom_bar(stat = "identity",position="identity")
#pos映射给填充色参数fill
#ggplot2一般是使用“+”号,接后面的代码
#geom_bar绘制条形图
#position="identity"可避免系统对负值绘制堆积条形而发出警告信息
#但是这样画出来的图红色对应负值,蓝色对应正值

#下面代码对颜色进行调整
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)
#scale_fill_manual()参数对图形颜色进行调整
#guide=FALSE可以删除图例
#colour设定边框颜色(这里为图形加一个黑色的边框)
#size是设定边框线宽度的参数,单位是毫米


Global climate temperature anomaly data from 1800 to 2011
Description

[size=13.3333px]This data set includes estimated global temperature anamoly data for the years 1800 through 2011. The anomaly is the difference from the baseline temperature, which is the mean of the yearly temperatures from 1951-1980.

Variables
  • Source: Data source (Berkeley, CRUTEM3, NASA).
  • Year: Year for the estimate.
  • Anomaly1y: Temperature anomaly in Celcius, smoothed over one year.
  • Anomaly5y: Temperature anomaly in Celcius, smoothed over five years.
  • Anomaly10y: Temperature anomaly in Celcius, smoothed over ten years.
  • Unc10y: Uncertainty for 10-year-smoothed anomaly.
Source

[size=13.3333px]Berkeley Earth Project: http://berkeleyearth.org/dataset/

[size=13.3333px]Climatic Research Unit (CRUTEM3):http://www.cru.uea.ac.uk/cru/data/temperature/

[size=13.3333px]NASA: http://data.giss.nasa.gov/gistemp/


[size=13.3333px][Package gcookbook version 1.0 Index]

KEYD]JB6U28TG0QGP9SNKI3.png
上表是csub显示出的数据集

Rplot.png
下面对画出来的距平图进行修改
Rplot01.png
可以看到经过修改后,作为暖色的红色表示距平的正值,而蓝色表示负值。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


沙发
zhjfj 发表于 2019-9-7 20:22:52
csub$pos<−csub$Anomaly10y>=0

加粗字体的修改

W{E@X)GIT4`OWNWKX$FJP~H.png (6.85 KB)

W{E@X)GIT4`OWNWKX$FJP~H.png

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-2-9 04:41