ggplot2分面柱状图柱子比例一致_R语言柱状图

本文介绍了如何使用R语言的ggplot2库创建分面柱状图,并确保柱子比例一致。文章提供了相关教程链接,包括Python、R和Matlab的地图可视化,R语言的日历图、基础地图构建、热图等内容,旨在帮助读者提升数据可视化技能。
摘要由CSDN通过智能技术生成
气象水文科研猫公众号交流邮箱:leolovehydrometeor@hotmail.com欢迎投稿&批评指正如有侵权且本公众号未能正确引用原文,请联系删除,谢谢理解、谢谢配合。
#openair包绘制泰勒图&热图

原文超链接:

Python、R和Matlab地图可视化(四)

341e60d9586090717dedf18667bb82e5.png

2a85b405d6519f383cfe24078bca288a.png

6b1f0f99a9f56677036f4dd0c65487cc.png

#openair包绘制日历图# load example data from packagedata(mydata)# basic plotcalendarPlot(mydata, pollutant = "o3", year = 2003)# show wind vectorscalendarPlot(mydata, pollutant = "o3", year = 2003, annotate = "wd")## Not run:# show wind vectors scaled by wind speed and different colourscalendarPlot(mydata, pollutant = "o3", year = 2003, annotate = "ws",cols = "heat")# show only specific months with selectByDatecalendarPlot(selectByDate(mydata, month = c(3,6,10), year = 2003),pollutant = "o3", year = 2003, annotate = "ws", cols = "heat")# categorical scale examplecalendarPlot(mydata, pollutant = "no2", breaks = c(0, 50, 100, 150, 1000),labels = c("Very low", "Low", "High", "Very High"),cols = c("lightblue", "green", "yellow", "red"), statistic = "max")## End(Not run)

15e3d28d5e46dd7f24cb810d0d5e5f5b.png

29b1d992b7cb4d8f357035149c5832d7.png

mydata %>%  selectByDate(month = c(1,2,3,4,5,6,7,8,9,10,11,12), year = format(Sys.time(),format='%Y')) %>%  calendarPlot(breaks = c(0,35,75,115,150,250,300),cols =rev(rainbow(7)),                labels = c('优','良','轻度污染','中度污染','重度污染','严重度污染'),pollutant = 'pm25',year = 2020)

e7963383368d57c84369258ea5d1d8ff.png

cd9185356be3d1226b78e18c84191f81.png

日历图往期推文超链接:

1 《R语言日历图(完)

2 《R语言基础地图构建(9)

3 《R语言热图(完)

4 《免费开源Python和R成为科研主流

5 《R解决之输出pdf矢量图中文乱码(一)

6 《R语言ggplot2分面(四)

4b361548d59ccc035bcfb5feed783f40.png427343bfc735037cab6454b8f1f3a2d6.png8c7818aecd84be1b79b884fd261cbbde.pngb5204d19de54497529203eace229f982.png1ba605f113d708c351f33f4ceba7f757.png

library(ggplot2)ggplot(mtcars,aes(x=row.names(mtcars), y = disp,fill='red'))+geom_bar(stat = "identity")+theme_bw()+ xlab("cars") +      theme(axis.text.x = element_text(angle = 90, vjust = .6, size = 10))ggplot(mtcars,aes(x=row.names(mtcars), y = disp))+geom_bar(stat = "identity")+theme_bw()+ xlab("cars") +      theme(axis.text.x = element_text(angle = 90, vjust = .6, size = 10))+coord_flip()ggplot(mtcars,aes(x=row.names(mtcars),y=disp,fill='red'))+geom_bar(stat = "identity")+theme_bw()+ xlab("cars") +      theme(axis.text.x = element_text(angle = 90, vjust = .6, size = 10))+coord_flip()

f149e83c6da6aad0ed918e7fb9b1479e.png

setwd('F:/Rpeng/28')library(ggplot2)#创建模拟数据集set.seed(1234)M Month Region Amount df df$Month #绘制横向的分面图ggplot(data = df, mapping = aes(x = Region, y = Amount, fill = Region)) + geom_bar(stat = 'identity') +      facet_wrap(~Month, ncol = 3) + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())+    theme_bw() +  # 去除地图灰色背景    theme(panel.grid.major=element_line(colour=NA),            panel.background = element_rect(fill = "transparent",colour = NA),            plot.background = element_rect(fill = "transparent",colour = NA),            panel.grid.minor = element_blank())+   # 去除地图网格theme(axis.ticks.length=unit(-0.1, "cm"), axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")) )+  theme(axis.text.x = element_text(angle=0,hjust=1),   # 旋转坐标轴label的方向        text = element_text(size = 12, face = "bold",family="serif"),          panel.spacing = unit(0,"lines") )+ #分面间隔设为零scale_y_continuous(expand = c(0,0)) #这个可以图形去掉与X轴间隙

88dd1b7344c0b344fa3893c3001173ff.png

library(gcookbook)cabbage_expggplot(cabbage_exp,aes(x = Cultivar, y = Weight, fill = Date))+ geom_bar(stat = "identity", position = "dodge")+theme_bw()+     geom_errorbar(data = cabbage_exp, aes(ymin = Weight - se, ymax = Weight + se), width = .4, position = position_dodge(0.9))         

e9bc8e2859bc78e54580e5325145199b.png

library(ggplot2)data1=read.csv( "F:/Rpeng/29/data/test2.csv" ) ggplot(data=data1,mapping=aes(x=obj,y=val,fill=obj,group=factor(1)))+  geom_bar(stat="identity")+theme_bw()ggplot(data=data1,mapping=aes(x=obj,y=val,fill=obj,group=factor(1)))+  geom_bar(stat="identity",width=0.5)+theme_bw()data1ggplot(data=data1,mapping=aes(x=obj,y=val,fill=obj,group=factor(1)))+  geom_bar(stat="identity")+theme_bw()ggplot(data=data1,mapping=aes(x=obj,y=val,fill=obj,group=factor(1)))+theme_bw()+  geom_bar(stat="identity")+geom_text(aes(label = val, vjust = -0.8, hjust = 0.5, color = obj), show.legend = TRUE)

467a9346cb745ae688e9f526969582c6.png

library(ggplot2)mydata=read.csv( "F:/Rpeng/29/data/test1.csv" ) ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_bw()

6af7ba9a805e3698688cec9a6d79a2e7.png

library(ggplot2)dat=read.csv( "F:/Rpeng/29/data/test3.csv" ) ggplot(dat, aes(x = type,y = Num,fill = Sample))+  ####position="stack"堆叠状  geom_bar(stat ="identity",width = 0.6,position ="stack")+       scale_fill_manual(values = c("red","blue"))+                labs(x = "",y = "", title = "test")+theme_bw()+      ####文字标签和geom_bar的参数position一致,同时调整文字位置vjust=0.5  geom_text(aes(label = dat$Num),position=position_stack(vjust =0.5),size = 5)+   guides(fill = guide_legend(reverse = F))+                 theme(plot.title = element_text(size = 14,face = "bold", vjust = 0.5, hjust = 0.5),          legend.title = element_blank(),                      legend.text = element_text(size = 14, face = "bold"),             legend.position = 'right',                       legend.key.size=unit(0.8,'cm'))   ggplot(dat, aes(x = type,y = Num,fill = Sample))+  ####position="stack" 改为"postion_stack(reverse=T) 反向堆叠  geom_bar(stat ="identity",width = 0.6,position =position_stack(reverse = T))+       scale_fill_manual(values = c("red","blue"))+                labs(x = "",y = "", title = "test")+theme_bw()+      ####文字标签和柱子一样需要反向堆叠  geom_text(aes(label = dat$Num),position=position_stack(reverse =T,vjust =0.5),size = 5)+   guides(fill = guide_legend(reverse = F))+                 theme(plot.title = element_text(size =14,face = "bold", vjust = 0.5, hjust = 0.5),          legend.title = element_blank(),                      legend.text = element_text(size =14, face = "bold"),             legend.position = 'right',                       legend.key.size=unit(0.8,'cm'))

a4d00605209e53c0d7c8f216f98a4ead.png

library(ggplot2)set.seed(1234)# 从数据集 diamonds中抽取1000个样本diam # 当不做任何设置时,此时的顺序按照变量clarity和color的原始顺序排列ggplot(diam,  aes(x = clarity, fill=color)) + geom_bar(stat='count') +     geom_bar(stat='count') + labs(x = 'Family Size') + theme_bw()ggplot(diam,aes(x = factor(clarity,levels=c('SI2', 'I1', 'SI1','VS2', 'VS1',  'VVS2', 'VVS1', 'IF')),fill=color))+      geom_bar(stat='count')+ labs(x = 'Family Size') + theme_bw()clardf names(clardf) library(dplyr)clardf ggplot(diam, aes(x = factor(clarity, levels=clardf$clarity),fill=color)) +      geom_bar(stat='count') + labs(x = 'Family Size') + theme_bw()ggplot(diam, aes(x = clarity, fill=color)) + geom_bar(stat='count')+      scale_fill_discrete(limits=c('J', 'I', 'H', 'G', 'F', 'D', 'E')) + labs(x = 'Family Size') + labs(fill="color")+theme_bw()

220ffe077eea90f4dfe8b7bd7563ce9c.png

081485552513020baf2db9a8f574a0e7.png

library(tidyverse)library(ggsci)names(iris)# 简单柱形图mpg%>%group_by(drv)%>%  summarise(avg=mean(hwy),se=sd(hwy))%>%ggplot(aes(drv,avg,fill='red'))+geom_col(color="black",position="dodge",width=0.3)+  geom_errorbar(aes(ymin=avg,ymax=avg+se),width=0.1)+  geom_text(aes(y=avg+se),label=c("a","b","c"),vjust=-0.5)+theme_bw()

69922a3b1d4b096454c503f13ee998ee.png

017cd96cf7b55a2d96455876c54de2ad.png

99cd53b3ebc56c173fd704b35ec452bb.png

e8bf81e5a87ceebc9ac74e81f0fa282a.png

4bd040c717be073cee7c8f247d9a8fed.png

064d350e37eabd12de1841b48f205f27.png

bafc14f262e0fb075992df503fce4d18.png

4cd963b62d915f4e12727e2b91b621e2.png

多跑跑大佬们的python源码:

48273283a034d49a9d3fcf0dcef1cacf.png

311e7221cff48c50f4a6646c4eade6f0.png

ff238a5f8e5dfec1a93ccf86b75c1685.png

9abe9885a48b479edbdb5b2af7ba7eea.png


关注气象水文科研猫公众号一起交流学习吧

关注气象水文科研猫公众号一起交流学习吧

5ac5911fed44b479a00bfd697abc10c7.png


只要你点下方【分享在看

我们就是好朋友,Thanks♪(・ω・)ノ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值