ggplot2之Layer—— geoms(一)
来源:互联网 发布:java金额大写转换 编辑:程序博客网 时间:2024/05/17 10:26
上次讲到了一些基础知识,这次说一说ggplot2图层中的geoms,由于geoms内容太多,所以分6次说。
图层结合了数据,映射,几何(对象),统计(变换)和位置调整。通常,将使用geom_函数创建图层,如果需要,覆盖默认位置和stat。
1. geom_abline 、geom_hline 、geom_vline——参考线:水平,垂直和对角线
2. geom_bar 、geom_col 、stat_count——条形图
3. geom_bin2d 、stat_bin_2d ——2d bin计数的热图
4. geom_blank——空图
5. geom_boxplot 、stat_boxplot——盒式图
1. geom_abline 、geom_hline 、geom_vline——参考线:水平,垂直和对角线
函数格式:
geom_abline(mapping = NULL, data = NULL, ..., slope, intercept, na.rm = FALSE, show.legend = NA)geom_hline(mapping = NULL, data = NULL, ..., yintercept, na.rm = FALSE, show.legend = NA)geom_vline(mapping = NULL, data = NULL, ..., xintercept, na.rm = FALSE, show.legend = NA) Argument
library(devtools)#install_github("easyGgplot2", "kassambara")library(easyGgplot2)p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()ggplot2.multiplot(p + geom_vline(xintercept = 5),p + geom_vline(xintercept = 1:5),p + geom_hline(yintercept = 20), cols=3)
plot1 <- p + geom_abline() # Can't see it - outside the range of the dataplot2 <- p + geom_abline(intercept = 20)coef(lm(mpg ~ wt, data = mtcars))#> (Intercept) wt #> 37.285126 -5.344472 plot3 <- p + geom_abline(intercept = 37, slope = -5)plot4 <- p + geom_smooth(method = "lm", se = FALSE)ggplot2.multiplot(plot1,plot2,plot3,plot4, cols=2)
运用一些参数,在不同的分面上画线:
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~ cyl)mean_wt <- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))p + geom_hline(aes(yintercept = wt), mean_wt)
你也可以控制其他参数:
ggplot(mtcars, aes(mpg, wt, colour = wt)) + geom_point() + geom_hline(aes(yintercept = wt, colour = wt), mean_wt) + facet_wrap(~ cyl)ggplot2.multiplot(plot1,plot2,plot3, cols=3)
2. geom_bar 、geom_col 、stat_count——条形图
函数格式:
geom_bar(mapping = NULL, data = NULL, stat = "count", position = "stack", ..., width = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)geom_col(mapping = NULL, data = NULL, position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)stat_count(mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
g <- ggplot(mpg, aes(class))plot1 <- g + geom_bar()plot2 <- g + geom_bar(aes(weight = displ))plot3 <- g + geom_bar(aes(fill = drv))bar <- ggplot(data = mpg) + geom_bar( mapping = aes(x = class, fill = class), show.legend = TRUE, width = 1 ) + theme(aspect.ratio = 1,legend.position = "bottom") + labs(x = NULL, y = NULL)plot4 <- bar + coord_flip()df <- data.frame(x = rep(c(2.9, 3.1, 4.5), c(5, 10, 4)))plot5 <- ggplot(df, aes(x)) + geom_bar()plot6 <- ggplot(df, aes(x)) + geom_histogram(binwidth = 0.5)ggplot2.multiplot(plot1,plot2,plot3,plot4,plot5,plot6, cols=3)
3. geom_bin2d 、stat_bin_2d ——2d bin计数的热图
函数格式:
geom_bin2d(mapping = NULL, data = NULL, stat = "bin2d", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)stat_bin_2d(mapping = NULL, data = NULL, geom = "tile", position = "identity", ..., bins = 30, binwidth = NULL, drop = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
df <- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)plot1 <- df + geom_bin2d()#> Warning: Removed 478 rows containing non-finite values (stat_bin2d).# You can control the size of the bins by specifying the number of# bins in each direction:plot2 <- df + geom_bin2d(bins = 10)#> Warning: Removed 478 rows containing non-finite values (stat_bin2d).#> Warning: Removed 4 rows containing missing values (geom_tile).plot3 <- df + geom_bin2d(bins = 30)#> Warning: Removed 478 rows containing non-finite values (stat_bin2d).# Or by specifying the width of the binsplot4 <- df + geom_bin2d(binwidth = c(0.1, 0.1))#> Warning: Removed 478 rows containing non-finite values (stat_bin2d).ggplot2.multiplot(plot1,plot2,plot3,plot4,cols = 2)
4. geom_blank——空图
函数格式:
geom_blank(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., show.legend = NA, inherit.aes = TRUE)
ggplot(mtcars, aes(wt, mpg))
5. geom_boxplot 、stat_boxplot——盒式图
函数格式:
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", ..., outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)stat_boxplot(mapping = NULL, data = NULL, geom = "boxplot", position = "dodge", ..., coef = 1.5, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p <- ggplot(mpg, aes(class, hwy))plot1 <- p + geom_boxplot()plot2 <- p + geom_boxplot() + geom_jitter(width = 0.2)plot3 <- p + geom_boxplot() + coord_flip()plot4 <- p + geom_boxplot(notch = TRUE)plot5 <- p + geom_boxplot(varwidth = TRUE)plot6 <- p + geom_boxplot(fill = "white", colour = "#3366FF")ggplot2.multiplot(plot1,plot2,plot3,plot4,plot5,plot6,cols = 3)
用其他颜色显示出离群点。
p + geom_boxplot(outlier.colour = "red", outlier.shape = 1)
根据某一变量类别来显示箱体颜色。
p + geom_boxplot(aes(colour = drv))
连续变量盒子分组可用cut_width。
ggplot(diamonds, aes(carat, price)) + geom_boxplot(aes(group = cut_width(carat, 0.25)))
箱体透明设置。
ggplot(diamonds, aes(carat, price)) + geom_boxplot(aes(group = cut_width(carat, 0.25)),alpha = 0.1)
用stat =“identity”,可以使用自己的计算绘制一个boxplot:
y <- rnorm(100)df <- data.frame( x = 1, y0 = min(y), y25 = quantile(y, 0.25), y50 = median(y), y75 = quantile(y, 0.75), y100 = max(y))ggplot(df, aes(x)) + geom_boxplot( aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100), stat = "identity" )
本次讲解结束,下次继续。
阅读全文
2 0
- ggplot2之Layer—— geoms(一)
- ggplot2之Layer—— geoms(二)
- ggplot2之Layer—— geoms(三)
- ggplot2(一)
- ggplot2——柱状图
- ggplot2——饼图篇
- R语言之ggplot2绘图序列一
- R可视化绘图一ggplot2之qplot
- 慕课—R语言之数据可视化—学习笔记 3.6ggplot2绘图系统(中)
- 慕课—R语言之数据可视化—学习笔记 3.6ggplot2绘图系统(下)
- ggplot2——主题篇
- ggplot2——图例篇
- ggplot2——坐标系篇
- ggplot2——玫瑰图
- OpenLayers学习——Layer体系(一)
- iOS动画——Layer Animations(一)
- iOS动画——Layer Animations(一)
- TCP/IP详解卷一学习笔记之二——Link Layer
- Unity Shader学习笔记:纹理动画
- 第十次多校联合hdu6178Monkeys(输入输出挂)
- C++中尽量使用C++提供的类型转换(2)---《More Effective C++》
- Bellman-Ford-解决负权边—C
- C-volatile,const
- ggplot2之Layer—— geoms(一)
- 14.路由表中存的是什么
- log4j2的使用详解(1)
- matlab仿真迈克尔逊干涉仪工作 单频非定域等倾干涉
- NLP深度学习 —— CS224学习笔记8
- PageHelper的使用
- windows ssh 连接 ubuntu
- 汇编语言32位加减乘除运算题
- 实现宠物跟随移动效果