作者:带土;Daitu; Adam 邮箱:2505131775@qq.com
ggplot()函数的介绍、图像显示主题的介绍、颜色、大小、形状
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 指定绘图的数据和坐标系变量
ggplot(data = mpg,aes(x=displ,y = cty))+
## 设置绘图的基本主题情况
theme_bw(base_family = "STKaiti")+
## 绘制图像的类型,并绘制图像是否根据不同的类以不同样式显示
geom_point(aes(colour = fl,shape = fl))+
## 设置图像的轴标签和名称
labs(x = "发动机排量",y = "油耗",title = "mpg数据集")+
## 对名称位置和图例位置进行调整
theme(plot.title = element_text(hjust = 0.5),
legend.position = c(0.8,0.7))+
## 对坐标轴的内容进行调整
scale_x_continuous(labels = function(x) paste(x,"升",sep = ""))+
## 对图例中的颜色映射进行调整
scale_color_brewer(palette = "Set1")
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 绘制不同主题的散点图
p1 <- ggplot(mpg,aes(x=displ,y = cty))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
ggtitle("主题为:theme_bw")
p2 <- ggplot(mpg,aes(x=displ,y = cty))+
theme_classic(base_family = "STKaiti")+
geom_point(colour = "red")+
ggtitle("主题为:theme_classic")
p3 <- ggplot(mpg,aes(x=displ,y = cty))+
theme_dark(base_family = "STKaiti")+
geom_point(colour = "red")+
ggtitle("主题为:theme_dark")
p4 <- ggplot(mpg,aes(x=displ,y = cty))+
theme_gray(base_family = "STKaiti")+
geom_point(colour = "red")+
ggtitle("主题为:theme_gray")
p5 <- ggplot(mpg,aes(x=displ,y = cty))+
theme_light(base_family = "STKaiti")+
geom_point(colour = "red")+
ggtitle("主题为:theme_light")
p6 <- ggplot(mpg,aes(x=displ,y = cty))+
theme_minimal(base_family = "STKaiti")+
geom_point(colour = "red")+
ggtitle("主题为:theme_minimal")
grid.arrange(p1,p2,p3,p4,p5,p6,nrow = 2)
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 绘制不同主题的散点图
p1 <- ggplot(mpg,aes(x=displ,y = cty))+
geom_point(colour = "red")+
geom_smooth(method = "loess")+
ggtitle("geom_point + geom_smooth")
mpgclass <- as.data.frame(table(mpg$class))
colnames(mpgclass) <- c("class","Freq")
mpgclass$ymin <- mpgclass$Freq -2
mpgclass$ymax <- mpgclass$Freq + 2
p2 <- ggplot(mpgclass,aes(x = class ,y = Freq))+
geom_bar(stat = "identity",fill = "red",
alpha = 0.5)+
geom_errorbar(aes(ymin = ymin,ymax=ymax),
width = 0.25,colour = "blue")+
ggtitle("geom_bar + geom_errorbar")
p3 <- ggplot(mpg,aes(x=as.factor(cyl),y = displ))+
geom_boxplot(colour = "red",weight = 0.5)+
geom_jitter(aes(colour = displ))+
theme(legend.position = "none")+
ggtitle("geom_boxplot + geom_jitter")
p4 <- ggplot(mpg,aes(displ))+
geom_histogram(fill = "red",binwidth = 0.25,
alpha = 0.5)+
ggtitle("geom_histogram")
p5 <- ggplot(mpg,aes(x=displ,y = cty))+
geom_hex(bins = 30)+
geom_density2d()+
theme(legend.position = "none")+
ggtitle("geom_hex + geom_density2d")
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(stringr)
library(RColorBrewer)
## 数据准备
data(AirPassengers)
AirPas <- as.data.frame(AirPassengers)
AirPas$x <- as.numeric(AirPas$x)
AirPas$yearmonth <- yearmon(time(AirPassengers))
AirPas$year <- format(AirPas$yearmonth,format = "%Y")
AirPas$month <- format(AirPas$yearmonth,"%B")
## 热力图分析数据的变化
AirPas$month <- factor(AirPas$month,levels = c("一月","二月","三月",
"四月","五月", "六月","七月",
"八月","九月", "十月","十一月",
"十二月"))
p6 <- ggplot(AirPas,aes(x=year,y=month))+
geom_tile(aes(fill = x))+
scale_fill_gradientn(colours=rev(brewer.pal(10,"RdYlGn")))+
geom_text(aes(label=x),size=2.5)+
theme(legend.position = "none")+
ggtitle("geom_tile + geom_text")
p6
## `geom_smooth()` using formula 'y ~ x'
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 调整绘图主题的示例
ggplot(data = mpg)+theme_bw(base_family = "STKaiti")+
geom_boxplot(aes(x = as.factor(cyl),y = displ,
colour = as.factor(year)))+
labs(x = "气缸数量",y = "发动机排量",title = "mpg数据集",
colour = "时间",subtitle = "主题调整的例子")+
scale_x_discrete(labels = function(x) paste(x,"个",sep = ""))+
## 对图像主体进行调整
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 1),
## 调整背景
plot.background = element_rect(fill = "gray"),
## 绘图区的边框
panel.border = element_rect(colour ="blue",size = 2),
## 网格线的设置
panel.grid = element_line(linetype = 2),
## 坐标轴标签和值刻度的设置
axis.title.x = element_text(colour = "red"),
axis.title.y = element_text(hjust = 0),
axis.text.x = element_text(angle = 60),
## 刻度线的设置
axis.ticks.y = element_line(colour = "red",size = 2),
## 图例的设置
legend.position = "bottom",
legend.background = element_rect(fill = "greenyellow"),
legend.title = element_text(colour = "red"))
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 调整绘图主题的示例
ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_grid(rows = vars(cyl),cols = vars(year))+
labs(x = "油耗",y = "发动机排量",title = "mpg数据集")
ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_grid(cyl~year)+
labs(x = "油耗",y = "发动机排量",title = "mpg数据集")
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 调整绘图主题的示例
ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_wrap(vars(class),nrow = 3)+
labs(x = "油耗",y = "发动机排量",title = "mpg数据集")
ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_wrap(~class,ncol = 3)+
labs(x = "油耗",y = "发动机排量",title = "mpg数据集")
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 调整绘图主题的示例
p1 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_wrap(~cyl,scales = "fixed")+
labs(title = ' scales = "fixed" ')
p2 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_wrap(~cyl,scales = "free")+
labs(title = ' scales = "free" ')
p3 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_wrap(~cyl,scales = "free_x")+
labs(title = ' scales = "free_x" ')
p4 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(colour = "red")+
facet_wrap(~cyl,scales = "free_y")+
labs(title = ' scales = "free_y" ')
gridExtra::grid.arrange(p1,p2,p3,p4,nrow = 2)
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 连续型,根据颜色梯度划分:
p1 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(aes(colour = cty))+
scale_color_gradient(low = "blue",high = "red")+
ggtitle('scale_color_gradient(low = "blue",high = "red")')
p2 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(aes(colour = cty-20))+
scale_color_gradient2(low = "red",mid = "white",high = "blue")+
ggtitle('scale_color_gradient2(low = "red",mid = "white",high = "blue")')
p3 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(aes(colour = cty / 35))+
scale_color_gradientn(
colours = c("red","yellow","green","lightblue"),
values = c(1,0.75,0.5,0.25,0))+
ggtitle('scale_color_gradientn(colours=c("red","yellow","green","lightblue"),\nvalues = c(1,0.75,0.5,0.25,0)))')
grid.arrange(p1,p2,p3,nrow = 3)
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## 离散型颜色,
p1 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(aes(colour = class))+
scale_color_brewer(palette = "BrBG")+
ggtitle('Diverging:scale_color_brewer(palette = "BrBG")')
p2 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(aes(colour = class))+
scale_color_brewer(palette = "Set1")+
ggtitle('Qualitative:scale_color_brewer(palette = "Set1")')
p3 <- ggplot(data = mpg,aes(x = cty,y = displ))+
theme_bw(base_family = "STKaiti")+
geom_point(aes(colour = class))+
scale_color_brewer(palette = "BuGn")+
ggtitle('Sequential:scale_color_brewer(palette = "BuGn")')
gridExtra::grid.arrange(p1,p2,p3,nrow = 3)
## 3.5 ggplot2的统计变换和坐标系
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
p1 <- ggplot(mpg,aes(displ))+
geom_histogram(aes(y = ..count..),fill = "lightblue")+
ggtitle("统计变换为:count")
p2 <- ggplot(mpg,aes(displ))+
geom_histogram(aes(y = ..density..),fill = "lightblue")+
ggtitle("统计变换为:density")
p3 <- ggplot(mpg,aes(displ))+
geom_histogram(aes(y = ..ncount..),fill = "lightblue")+
ggtitle("统计变换为:ncount")
p4 <- ggplot(mpg,aes(displ))+
geom_histogram(aes(y = ..ndensity..),fill = "lightblue")+
ggtitle("统计变换为:ndensity")
grid.arrange(p1,p2,p3,p4,nrow = 2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
p1 <- ggplot(mpg)+
geom_bar(aes(x = class,fill = drv),position = "stack")+
ggtitle('position = "stack"')+
theme(axis.text.x = element_text(angle = 15))
p2 <- ggplot(mpg)+
geom_bar(aes(x = class,fill = drv),position = "dodge")+
ggtitle('position = "dodge"')+
theme(axis.text.x = element_text(angle = 15))
p3 <- ggplot(mpg)+
geom_bar(aes(x = class,fill = drv),position = "fill")+
ggtitle('position = "fill"')+
theme(axis.text.x = element_text(angle = 15))
p4 <- ggplot(mpg)+
geom_bar(aes(x = class,fill = drv),position = "identity")+
ggtitle('position = "identity"')+
theme(axis.text.x = element_text(angle = 15))
grid.arrange(p1,p2,p3,p4,nrow = 2)
## 坐标系样式
p1 <- ggplot(mpg,aes(class))+
geom_bar(aes(fill = class),show.legend = FALSE)+
ggtitle("条形图")
p2 <- ggplot(mpg,aes(class))+
geom_bar(aes(fill = class),show.legend = FALSE)+
coord_flip()+ggtitle("条形图+coord_flip()")
p3 <- ggplot(mpg,aes(class))+
geom_bar(aes(fill = class),show.legend = FALSE)+
coord_polar()+ggtitle("条形图+coord_polar()")
p4 <- ggplot(mpg)+
geom_bar(aes(x = factor(1),fill = class),
width = 1,show.legend = FALSE)+
coord_polar(theta = "y")+
ggtitle('条形图+coord_polar()')
grid.arrange(p1,p2,p3,p4,nrow = 2)
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
### 条形图
## 常见的条形图p
p1 <- ggplot(mpg)+
geom_bar(aes(x = cyl,fill = as.factor(cyl)))+
labs(title = "条形图")
p1
## Var1 Freq
## 1 4 81
## 2 5 4
## 3 6 79
## 4 8 70
p2 <- ggplot(newdata)+
geom_bar(aes(x = reorder(Var1,Freq),y = Freq,fill = Var1),
stat = "identity")+
labs(title = "条形图+升序排列")
p2
## 条形图+降序排
p3 <- ggplot(newdata)+
geom_bar(aes(x = reorder(Var1,-Freq),y = Freq,fill = Var1),
stat = "identity")+
labs(title = "条形图+降序排列")
p3
## 添加误差范围的条形图
newdata$Freqmin <- newdata$Freq - 2 ##下界的取值
newdata$Freqmax <- newdata$Freq + 5 ##上界的取值
newdata
## Var1 Freq Freqmin Freqmax
## 1 4 81 79 86
## 2 5 4 2 9
## 3 6 79 77 84
## 4 8 70 68 75
p4 <- ggplot(newdata,aes(x = reorder(Var1,-Freq),y = Freq))+
geom_bar(aes(fill = Var1),stat = "identity")+
geom_errorbar(aes(ymin = Freqmin,ymax = Freqmax),
colour = "black",width = 0.25)+
labs(title = "条形图+errorbar")
p4
## 水平条形图
p5 <- ggplot(newdata,aes(x = reorder(Var1,Freq),y = Freq))+
geom_bar(aes(fill = Var1),stat = "identity")+
geom_errorbar(aes(ymin = Freqmin,ymax = Freqmax),
colour = "black",width = 0.25)+
coord_flip()+
labs(title = "水平条形图+errorbar")
p5
## 分组条形图
p6 <- ggplot(mpg,aes(class,fill = drv))+
geom_bar(position = "dodge2")+
ggtitle("分组条形图+dodge2")
p6
## 归一化的堆叠条形图,可看出每部分所占的比例
p7 <- ggplot(mpg,aes(class,fill = drv))+
geom_bar(position = "fill")+
ggtitle("分组条形图+fill")
p7
## 双向条形图
set.seed(123)
newdata <- data.frame(x = c(1:30),y1 = abs(round(10*rnorm(30))),
y2 = -abs(round(6.8*rnorm(30))))
head(newdata)
## x y1 y2
## 1 1 6 -3
## 2 2 2 -2
## 3 3 16 -6
## 4 4 1 -6
## 5 5 1 -6
## 6 6 17 -5
p8 <- ggplot(newdata,aes(x = x))+
geom_bar(aes(y = y1),stat = "identity",fill = "red",alpha = 0.5)+
geom_text(aes(y = y1+1,label = y1),size = 3)+
geom_bar(aes(y = y2),stat = "identity",fill = "lightblue")+
geom_text(aes(y = y2-1,label = y2),size = 3)+
ggtitle("双向条形图")
p8
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## Var1 Freq
## 1 2seater 5
## 2 compact 47
## 3 midsize 41
## 4 minivan 11
## 5 pickup 33
## 6 subcompact 35
## 7 suv 62
### 基础的饼图
p1 <- ggplot(newdata)+
geom_bar(aes(x = "",y = Freq,fill = Var1),stat = "identity")+
coord_polar(theta = "y")+
labs(title = "基础的饼图")
p1
## 调整饼图的细节
p2 <- ggplot(newdata,aes(x = "",y = Freq,fill = Var1))+
geom_bar(width = 1,stat = "identity")+
coord_polar(theta = "y")+
## 调整图像的细节
theme_minimal(base_family = "STKaiti")+
theme(axis.text = element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())+
scale_fill_brewer(palette = "Set1")+
labs(title = "精修的饼图")
p2
## 甜甜圈图
p3 <- ggplot(newdata,aes(x = "",y = Freq,fill = Var1))+
geom_bar(width = 1,stat = "identity")+
coord_polar(theta = "y")+
## 调整图像的细节
theme_minimal(base_family = "STKaiti")+
theme(axis.text = element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())+
scale_fill_brewer(palette = "Set1")+
annotate("text",x = 0,y = 0,label = "Label")+
labs(title = "甜甜圈图")
p3
## 玫瑰图
p4 <- ggplot(newdata,aes(x = Var1,y = Freq,fill = Var1))+
geom_bar(width = 0.8,stat = "identity")+
geom_text(aes(y = Freq+2,label = Freq))+
scale_fill_brewer(palette = "Set1")+
coord_polar(theta = "x")+
theme_minimal(base_family = "STKaiti")+
theme(axis.text = element_blank(),
axis.ticks=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())+
labs(title = "玫瑰图")
p4
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## 绘制直方图时分面
p2 <- ggplot(airquality)+
geom_histogram(aes(Wind),bins = 25,fill = "lightblue")+
facet_wrap(~Month)+
ggtitle("直方图分面")
p2
## 根据其它变量设置填充颜色,堆积直方图
p3 <- ggplot(airquality)+
geom_histogram(aes(Wind,fill = as.factor(Month)),bins = 25)+
ggtitle("堆积直方图")
p3
## 根据自身变量设置填充颜色,堆积直方图
p4 <- ggplot(airquality)+
geom_histogram(aes(Wind,fill = cut(Wind,10)),bins = 30,
show.legend = F)+
scale_fill_discrete(h = c(0, 180) + 15, c = 50, l = 65)+
ggtitle("渐变直方图")
p4
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## 一个分组变量的箱线图
p1 <- ggplot(airquality,aes(x = as.factor(Month),y = Temp))+
geom_boxplot()+
ggtitle("箱线图")
p1
## 小提琴图加抖动散点图
p2 <- ggplot(airquality,aes(x = as.factor(Month),y = Temp))+
geom_violin(fill = "lightblue")+
geom_jitter(aes(colour = Temp),width = 0.1)+
scale_color_gradient(low = "blue",high = "red")+
ggtitle("小提琴图加抖动散点图")
p2
## 二个分组变量的箱线图
p3 <- ggplot(airquality,aes(x = as.factor(Month),y = Temp))+
geom_boxplot(aes(colour = cut(Wind,3)))+
ggtitle("分组箱线图")
p3
## 分组小提琴图
p4 <- ggplot(airquality,aes(x = as.factor(Month),y = Temp))+
geom_violin(aes(fill = cut(Wind,3)))+
ggtitle("分组小提琴图")
p4
## 二个分组变量的箱线图分面
p5 <- ggplot(airquality,aes(x = as.factor(Month),y = Temp))+
geom_boxplot(aes(colour = cut(Wind,3)),show.legend = F)+
facet_wrap(~cut(Wind,3))+
ggtitle("分面+分组箱线图")
p5
## 分组小提琴图+分面
p6 <- ggplot(airquality,aes(x = as.factor(Month),y = Temp))+
geom_violin(aes(fill = cut(Wind,3)))+
facet_wrap(~cut(Wind,3))+
ggtitle("分面+分组小提琴图")
p6
## 3.6 ggplot2可视化常见的统计图2
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## 基础的散点图
p1 <- ggplot(airquality,aes(x = Wind,y = Temp))+
geom_point(colour = "red")+
ggtitle("散点图")
p1
## 散点图+平滑曲线
p2 <- ggplot(airquality,aes(x = Wind,y = Temp))+
geom_point(colour = "red")+
geom_smooth(method = "loess")+
ggtitle("散点图+平滑曲线")
p2
## `geom_smooth()` using formula 'y ~ x'
## 分组散点图+平滑曲线
p3 <- ggplot(airquality,aes(x = Wind,y = Temp))+
geom_point(aes(colour = as.factor(Month)))+
geom_smooth(aes(colour = as.factor(Month)),method = "loess")+
scale_color_brewer(palette = "Set1")+
ggtitle("分组散点图+平滑曲线")
p3
## `geom_smooth()` using formula 'y ~ x'
## 分面散点图+平滑曲线
p4 <- ggplot(airquality,aes(x = Wind,y = Temp))+
geom_point(aes(colour = as.factor(Month)))+
geom_smooth(aes(colour = as.factor(Month)),method = "loess")+
facet_wrap(~as.factor(Month))+
scale_color_brewer(palette = "Set1")+
theme(legend.position = "none")+
ggtitle("分面散点图+平滑曲线")
p4
## `geom_smooth()` using formula 'y ~ x'
## 气泡图
p5 <- ggplot(airquality,aes(x = Wind,y = Temp))+
geom_point(aes(size = 1+ (Day / 10),colour = 1+ (Day / 10)),
show.legend = F)+
ggtitle("气泡图")
p5
## 气泡图+分面
p6 <- ggplot(airquality,aes(x = Wind,y = Temp))+
geom_point(aes(size = 1+ (Day / 10),colour = 1+ (Day / 10)),
show.legend = F)+
facet_wrap(~Month)+
ggtitle("分面气泡图")
p6
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
### 6 密度曲线系列
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
### 密度曲线
p1 <- ggplot(airquality)+
geom_density(aes(Temp),fill = "red",alpha = 0.5)+
ggtitle("密度曲线")
p1
### 分组数据的密度曲线
p2 <- ggplot(airquality)+
geom_density(aes(Temp,colour = as.factor(Month),
fill = as.factor(Month)),alpha = 0.5)+
ggtitle("分组密度曲线")
p2
### 分组数据的密度曲线进行分面
p3 <- ggplot(airquality)+
geom_density(aes(Temp),fill = "red",alpha = 0.5)+
facet_wrap(~Month,nrow = 2)+
ggtitle("分面密度曲线")
p3
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
p4 <- ggplot(mpg)+
geom_density(aes(displ,fill = drv,colour = drv),
alpha = 0.5)+
facet_wrap(~year,nrow = 2)+
ggtitle("分面分组密度曲线")
p4
## 二维密度曲线分组+分面
p6 <- ggplot(airquality)+
geom_density2d(aes(x = Temp,y = Wind,colour = as.factor(Month)),
show.legend = F)+
facet_wrap(~Month)+
ggtitle("分面二维密度曲线")
p6
library(tidyr)
## 数据准备
set.seed(123)
linedata1 <- data.frame(index = 1:100,
x1 = cumsum(rnorm(100,0,5)),
x2 = cumsum(rnorm(100,1,4)),
x3 = cumsum(rnorm(100,0,10)),
group = "class1")
head(linedata1)
## index x1 x2 x3 group
## 1 1 -2.802378 -1.8416263 21.98810 class1
## 2 2 -3.953266 0.1859086 35.11223 class1
## 3 3 3.840276 0.1991411 32.46078 class1
## 4 4 4.192818 -0.1910293 37.89272 class1
## 5 5 4.839257 -2.9975036 33.74932 class1
## 6 6 13.414581 -2.1776145 28.98685 class1
set.seed(1234)
linedata2 <- data.frame(index = 1:100,
x1 = cumsum(rnorm(100,0,8)),
x2 = cumsum(rnorm(100,1,4)),
x3 = cumsum(rnorm(100,0,10)),
group = "class2")
linedataall <- rbind(linedata1,linedata2)
head(linedataall)
## index x1 x2 x3 group
## 1 1 -2.802378 -1.8416263 21.98810 class1
## 2 2 -3.953266 0.1859086 35.11223 class1
## 3 3 3.840276 0.1991411 32.46078 class1
## 4 4 4.192818 -0.1910293 37.89272 class1
## 5 5 4.839257 -2.9975036 33.74932 class1
## 6 6 13.414581 -2.1776145 28.98685 class1
### 折线图
p1 <- ggplot(linedata1)+
geom_line(aes(x = index,y = x1),colour = "red")+
ggtitle("单个变量折线图")
p1
### 多个变量折线图
## 宽数据转化维长数据
linedata1_long <- tidyr::gather(linedata1,key = "X",
value = "value",x1:x3)
head(linedata1_long)
## index group X value
## 1 1 class1 x1 -2.802378
## 2 2 class1 x1 -3.953266
## 3 3 class1 x1 3.840276
## 4 4 class1 x1 4.192818
## 5 5 class1 x1 4.839257
## 6 6 class1 x1 13.414581
p2 <- ggplot(linedata1_long)+
geom_line(aes(x = index,y = value,colour = X,
linetype = X))+
ggtitle("多个变量折线图")
p2
### 多个变量的分组折线图
## 宽数据转化维长数据
linedataall_long <- gather(linedataall,key = "X",
value = "value",x1:x3)
head(linedataall_long)
## index group X value
## 1 1 class1 x1 -2.802378
## 2 2 class1 x1 -3.953266
## 3 3 class1 x1 3.840276
## 4 4 class1 x1 4.192818
## 5 5 class1 x1 4.839257
## 6 6 class1 x1 13.414581
p3 <- ggplot(linedataall_long)+
geom_line(aes(x = index,y = value,colour = X,
linetype = X))+
facet_wrap(~group,ncol = 1,scales = "free_y")+
ggtitle("多个变量分面折线图")
p3
## 分面折线图
p4 <- ggplot(linedataall_long)+
geom_line(aes(x = index,y = value),colour = "red")+
facet_grid(X~group,scales = "free_y")+
ggtitle("网格分面折线图")
p4
## index x1 x2 x3 group
## 1 1 -2.802378 -1.8416263 21.98810 class1
## 2 2 -3.953266 0.1859086 35.11223 class1
## 3 3 3.840276 0.1991411 32.46078 class1
## 4 4 4.192818 -0.1910293 37.89272 class1
## 5 5 4.839257 -2.9975036 33.74932 class1
## 6 6 13.414581 -2.1776145 28.98685 class1
p5 <- ggplot(linedataall,aes(x = index))+
geom_area(aes(y = x1,colour = group,fill = group),alpha = 0.25)+
ggtitle("单个变量分组面积图")
p5
## 面积图2
p6 <- ggplot(linedataall_long,aes(x = index))+
geom_area(aes(y = value,colour = group,fill = group),
alpha = 0.25)+
facet_grid(X~group,scales = "free_y")+
ggtitle("多个变量网格分面面积图")
p6
## index group X value
## 1 1 class1 x1 -2.802378
## 2 2 class1 x1 -3.953266
## 3 3 class1 x1 3.840276
## 4 4 class1 x1 4.192818
## 5 5 class1 x1 4.839257
## 6 6 class1 x1 13.414581
## 数据准备
areadata <- linedataall_long[,c("index","group","X")]
areadata$ymin <- linedataall_long$value
areadata$ymax <-
areadata$ymin + abs(rnorm(length(areadata$ymin),0,20))
head(areadata)
## index group X ymin ymax
## 1 1 class1 x1 -2.802378 8.796762
## 2 2 class1 x1 -3.953266 15.112308
## 3 3 class1 x1 3.840276 7.428848
## 4 4 class1 x1 4.192818 24.388982
## 5 5 class1 x1 4.839257 5.311789
## 6 6 class1 x1 13.414581 26.395146
p7 <- ggplot(areadata,aes(x = index))+
geom_ribbon(aes(ymin = ymin,ymax = ymax,fill = X),alpha = 0.85)+
facet_wrap(~group,ncol = 1,scales = "free_y")+
ggtitle("分面面积图geom_ribbon")
p7
## 堆积面积图
p8 <- ggplot(areadata,aes(x = index))+
geom_area(aes(y = abs(ymax),fill = X,color = X),
alpha = 0.85,position = "fill")+
facet_wrap(~group,ncol = 1,scales = "free_y")+
ggtitle("分面堆积面积图")
p8
## 热力图
library(zoo)
library(stringr)
library(RColorBrewer)
## 数据准备
data(AirPassengers)
AirPas <- as.data.frame(AirPassengers)
AirPas$x <- as.numeric(AirPas$x)
AirPas$yearmonth <- yearmon(time(AirPassengers))
AirPas$year <- format(AirPas$yearmonth,format = "%Y")
AirPas$month <- format(AirPas$yearmonth,"%B")
## 热力图分析数据的变化
AirPas$month <- factor(AirPas$month,
levels = c("一月","二月","三月",
"四月","五月", "六月","七月",
"八月","九月", "十月","十一月",
"十二月"))
head(AirPas)
## x yearmonth year month
## 1 112 1 1949 1949 一月
## 2 118 2 1949 1949 二月
## 3 132 3 1949 1949 三月
## 4 129 4 1949 1949 四月
## 5 121 5 1949 1949 五月
## 6 135 6 1949 1949 六月
p1 <- ggplot(AirPas,aes(x=year,y=month))+
geom_tile(aes(fill = x))+
scale_fill_gradient(low = "lightblue", high = "red")+
geom_text(aes(label=x),size=2.5)+
ggtitle("热力图")
p1
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## 数据转化维长数据
airlong <- gather(airquality,key = "var",
value = "value",c("Ozone","Solar.R"))
head(airlong)
## Wind Temp Month Day var value
## 1 7.4 67 5 1 Ozone 41
## 2 8.0 72 5 2 Ozone 36
## 3 12.6 74 5 3 Ozone 12
## 4 11.5 62 5 4 Ozone 18
## 5 14.3 56 5 5 Ozone NA
## 6 14.9 66 5 6 Ozone 28
p2 <- ggplot(airlong,aes(x = Day,y = Month))+
geom_tile(aes(fill = value))+
scale_fill_gradientn(colours=rev(brewer.pal(10,"RdYlGn")))+
facet_wrap(~var,ncol = 1)+
geom_text(aes(label=value),size=2,na.rm = TRUE)+
scale_x_continuous(breaks = 1:31,labels = c(1:31))+
ggtitle("热力图分面")
p2