美文网首页作图
第二章 条形图

第二章 条形图

作者: 芋圆学徒 | 来源:发表于2021-02-06 16:34 被阅读0次

二、条形图

1,简单的条形图

library(gcookbook)
ggplot(pg_mean,aes(group,weight,fill=group))+geom_bar(stat = "identity")
str(pg_mean)
View(pg_mean)

2,绘制簇状条形图

View(cabbage_exp)
ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(position = "dodge",stat = "identity")

ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(stat = "identity",position = "dodge",colour="black")+
  scale_fill_brewer(palette = "Paste11")

3,绘制频数分布条形图

ggplot(diamonds,aes(cut))+geom_bar()
x <- table(diamonds$cut)
barplot(x)
View(diamonds)
ggplot(diamonds,aes(cut,price))+
  geom_bar(stat = "identity")

4,条形图着色

upc <- subset(uspopchange,rank(Change)>40)
ggplot(upc,aes(Abb,Change,fill=Region))+
  geom_bar(stat = "identity")

ggplot(upc,aes(x=reorder(Abb,Change),Change,fill=Region))+
  geom_bar(stat = "identity",colour="black")+
  scale_fill_manual(values = c("#669933","#FFCC66"))+
  xlab("State")

5,对正负条形图分别着色

csub <- subset(climate,Source=="Berkeley"&Year>=1900)
csub$pos <- csub$Anomaly10y>=0
csub

ggplot(csub,aes(Year,Anomaly10y,fill=pos))+
  geom_bar(stat = "identity",position = "identity")

ggplot(csub,aes(Year,Anomaly10y,fill=pos))+
  geom_bar(stat = "identity",position = "identity",colour="black",size=0.25)+
  scale_fill_manual(values = c("#CCEEFF","#FFDDDD"),guide=F)

6. 调整条形宽度和条形间距

ggplot(pg_mean,aes(group,weight))+
  geom_bar(stat = "identity")
ggplot(pg_mean,aes(group,weight))+
  geom_bar(stat = "identity",width=0.5)
ggplot(pg_mean,aes(group,weight))+
  geom_bar(stat = "identity",width=1)

ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(stat = "identity",width = 0.5,position ="dodge")
ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(stat = "identity",width = 0.5,position = position_dodge(0.7))

7.绘制堆积条形图

ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(stat = "identity",width = 0.5)
#调整图例顺序
ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(stat = "identity",width = 0.5)+
  guides(fill=guide_legend(reverse = T))
#调整堆叠顺序
library(dplyr)
ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar, order=desc(Cultivar)))+
  geom_bar(stat = "identity",width = 0.5)

ggplot(cabbage_exp,aes(Date,Weight, fill= Cultivar))+
  geom_bar(stat = "identity",colour="black")+
  guides(fill=guide_legend(reverse = T))+
  scale_fill_brewer(palette = "Paste11")

8.绘制百分比堆积条形图

library(gcookbook)
library(plyr)
ce <- ddply(cabbage_exp,"Date",transform,percent_weight=Weight/sum(Weight)*100)
ggplot(ce,aes(Date,percent_weight,fill=Cultivar))+
  geom_bar(stat = "identity")

9.添加数据标签

ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
  geom_bar(stat = "identity",width = 0.5)+
  geom_text(aes(label=Weight),vjust=1.5,colour="white")

ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
  geom_bar(stat = "identity")+
  geom_text(aes(label=Weight),vjust=-0.2,colour="black")
ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
  geom_bar(stat = "identity")+
  geom_text(aes(label=Weight),vjust=-0.2,colour="black")+
  ylim(0,max(cabbage_exp$Weight*1.05))

ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
  geom_bar(stat = "identity")+
  geom_text(aes(label=Weight, y=Weight+0.1),colour="black")
 
ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
  geom_bar(stat = "identity",position = "dodge")+
  geom_text(aes(label=Weight),vjust=1.5,colour="white",
            size=3,position = position_dodge(.9))

10.绘制Cleveland图

library(gcookbook)
tophit <- tophitters2001[1:25,]
ggplot(tophit,aes(avg,name))+
  geom_point()
tophit[,c("name","lg","avg")]

ggplot(tophit,aes(avg,y=reorder(name,avg)))+
  geom_point(size=3)+
  theme_bw()+
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_line(colour="grey60",linetype = "dashed"))

ggplot(tophit,aes(x=reorder(name,avg),avg))+
  geom_point(size=3)+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 60,hjust = 1),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.minor.x = element_line(colour="grey60",linetype = "dashed"))

#根据因子lg进行分组,并排序,reorder不能同时对两组排序
nameorder <- tophit$name[order(tophit$lg,tophit$avg)]
tophit$name <- factor(tophit$name,levels = nameorder)

ggplot(tophit,aes(avg,name))+
  geom_segment(aes(yend=name),xend=0,colour="grey50")+
  geom_point(size=3,aes(colour=lg))+
  scale_color_brewer(palette = "Set1",limits=c("NL","AL"))+
  theme_bw()+
  theme(panel.grid.major.y = element_blank(),
        legend.position = c(1,0.55),
        legend.justification = c(1,0.5))


ggplot(tophit,aes(avg,name))+
  geom_segment(aes(yend=name),xend=0,colour="grey50")+
  geom_point(size=3,aes(colour=lg))+
  scale_color_brewer(palette = "Set1",limits=c("NL","AL"),guide=F)+
  theme_bw()+
  theme(panel.grid.major.y = element_blank())+
  facet_grid(lg~.,scales = "free_y",space = "free_y")

相关文章

  • 第二部分 第6章 基本图形

    6.1 条形图 简单条形图 barplot() 堆砌条形图和分组条形图 均值条形图 条形图的微调 par()函数 ...

  • R语言绘制条形图

    数据 简单条形图 最基本用法:barplot() 水平条形图 数据 堆砌条形图 棘状图 分组条形图 数据 均值条形图

  • 第二章 条形图

    二、条形图 1,简单的条形图 2,绘制簇状条形图 3,绘制频数分布条形图 4,条形图着色 5,对正负条形图分别着色...

  • R语言:图形

    常用的图形,这里给出案例: barplot 条形图、单向量条形图 堆砌与分组条形图 添加标签 均值条形图 棘状图 ...

  • 第二章 选择有效的图表

    12个: 简单文本、散点图、表格、折线图、热力图、斜率图、竖直条形图、水平条形图、堆叠竖直条形图、堆叠水平条形图、...

  • R数据可视化19:环状条形图

    之前我们已经讲过很多条形图啦,但是今天我们再来讲一种条形图——环状条形图(Circular barplot)。当厌...

  • R语言可视化(三):条形图绘制

    03.条形图绘制 清除当前环境中的变量 设置工作目录 barplot函数绘制条形图 ggplot2包绘制分组条形图...

  • 直方图可不是条形图,这是个深坑

    说到条形图,我们都很熟悉了,它分为两种,一种是水平条形图,一种是垂直条形图。 无论是哪种条形图,我们总能精确的对数...

  • Matplotlib实践使用笔记——基本画图

    基本画图操作 内容包括画线、条形图、直方图、饼图。 画线 画条形图 简单条形图 直方图 统计出现的次数 饼状图 会...

  • 手把手教你 Tableau 绘制条形图(一)

    手把手教你 Tableau 绘制条形图 使用条形图(柱状图)可在各类别之间比较数据。创建条形图时会将维度放在“行”...

网友评论

    本文标题:第二章 条形图

    本文链接:https://www.haomeiwen.com/subject/ohjotltx.html