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"  )

这里写图片描述

本次讲解结束,下次继续。

原创粉丝点击