ggplot2之Layer—— geoms(三)

来源:互联网 发布:破解三星s4支持4g网络 编辑:程序博客网 时间:2024/05/17 12:06

接着上次继续说说。


1. geom_hex 、stat_bin_hex——二进制计数器的六边形热图

2. geom_freqpoly 、geom_histogram 、stat_bin——直方图和频率多边形

3. geom_jitter——激动点数

4. geom_crossbar 、geom_errorbar 、geom_linerange 、geom_pointrange——垂直间隔:线,横杠和错误栏

5. geom_map——参考地图中的多边形


1. geom_hex 、stat_bin_hex——二进制计数器的六边形热图

将平面划分为正六边形,计算每个六边形中的个案数,然后(默认情况下)将案例数映射到六边形填充。 六角形箱子避免了有时由geom_bin2d非常规则的对齐产生的视觉伪影。

函数格式:

geom_hex(mapping = NULL, data = NULL, stat = "binhex",  position = "identity", ..., na.rm = FALSE, show.legend = NA,  inherit.aes = TRUE)stat_bin_hex(mapping = NULL, data = NULL, geom = "hex",  position = "identity", ..., bins = 30, binwidth = NULL, na.rm = FALSE,  show.legend = NA, inherit.aes = TRUE)
library(ggplot2)library(easyGgplot2)d <- ggplot(diamonds, aes(carat, price))plot1 <- d + geom_hex(col = "white",fill = "green",alpha = 0.8) + guides(fill=FALSE)#去掉图例plot2 <- d + geom_hex(bins=10,col = "white",fill = "orange",alpha = 0.7) + guides(fill=FALSE)plot3 <- d + geom_hex(binwidth = c(1, 1000),col = "red",fill = "gray",alpha = 0.8) + guides(fill=FALSE)plot4 <- d + geom_hex(binwidth = c(.1, 500),col = "red",fill = "gray",alpha = 0.8) + guides(fill=FALSE)ggplot2.multiplot(plot1,plot2,plot3,plot4, cols=2)

这里写图片描述

2. geom_freqpoly 、geom_histogram 、stat_bin——直方图和频率多边形

通过将x轴划分成箱体并计算每个仓中的观测数量,可视化单个连续变量的分布。 直方图(geom_histogram)用条显示计数; 频率多边形(geom_freqpoly),用线显示计数。 当您想比较分类变量级别的分布时,频率多边形更合适。 stat_bin仅适用于连续x数据。 如果你的x数据是离散的,你可能想使用stat_count。

函数格式:

geom_freqpoly(mapping = NULL, data = NULL, stat = "bin",  position = "identity", ..., na.rm = FALSE, show.legend = NA,  inherit.aes = TRUE)geom_histogram(mapping = NULL, data = NULL, stat = "bin",  position = "stack", ..., binwidth = NULL, bins = NULL, na.rm = FALSE,  show.legend = NA, inherit.aes = TRUE)stat_bin(mapping = NULL, data = NULL, geom = "bar", position = "stack",  ..., binwidth = NULL, bins = NULL, center = NULL, boundary = NULL,  breaks = NULL, closed = c("right", "left"), pad = FALSE,  na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
library(ggplot2)library(easyGgplot2)plot1 <- ggplot(diamonds, aes(carat)) +  geom_histogram(fill = "pink",alpha = 0.7)#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.plot2 <- ggplot(diamonds, aes(carat)) +  geom_histogram(fill = "orange",alpha = 0.7,binwidth = 0.01)plot3 <- ggplot(diamonds, aes(carat)) +  geom_histogram(fill = "green",alpha = 0.7,bins = 50)# Rather than stacking histograms, it's easier to compare frequency# polygonsplot4 <- ggplot(diamonds, aes(price, fill = cut)) +  geom_histogram(binwidth = 500)ggplot2.multiplot(plot1,plot2,plot3,plot4, cols=2)

这里写图片描述

ggplot(diamonds, aes(price, colour = cut)) +  geom_freqpoly(binwidth = 500)

这里写图片描述

ggplot(diamonds, aes(price, ..density.., colour = cut)) +  geom_freqpoly(binwidth = 500)

这里写图片描述

3. geom_jitter

jitter geom是geom_point(position =“jitter”)的方便快捷方式。 它为每个点的位置增加了少量的随机变化,并且是处理由较小数据集中的离散性引起的叠加的有用方式。

函数格式:

geom_jitter(mapping = NULL, data = NULL, stat = "identity",  position = "jitter", ..., width = NULL, height = NULL, na.rm = FALSE,  show.legend = NA, inherit.aes = TRUE)
p <- ggplot(mpg, aes(cyl, hwy))plot1 <- p + geom_point()plot2 <- p + geom_jitter()#增加映射plot3 <- p + geom_jitter(aes(colour = class))plot4 <- p + geom_jitter(aes(colour = fl))ggplot2.multiplot(plot1,plot2,plot3,plot4, cols=2)

这里写图片描述

#运用width/height来更改宽度和高度plot1 <- ggplot(mpg, aes(cyl, hwy)) + geom_jitter()plot2 <- ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25)plot3 <- ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.5, height = 0.5)plot4 <- ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.05, height = 0.05)ggplot2.multiplot(plot1,plot2,plot3,plot4, cols=2)

这里写图片描述

4. geom_crossbar 、geom_errorbar 、geom_linerange 、geom_pointrange——垂直间隔:线,横杠和错误栏

表示由x,ymin和ymax定义的垂直间隔的各种方式。 每个案例绘制一个图形对象。

函数格式:

geom_crossbar(mapping = NULL, data = NULL, stat = "identity",  position = "identity", ..., fatten = 2.5, na.rm = FALSE,  show.legend = NA, inherit.aes = TRUE)geom_errorbar(mapping = NULL, data = NULL, stat = "identity",  position = "identity", ..., na.rm = FALSE, show.legend = NA,  inherit.aes = TRUE)geom_linerange(mapping = NULL, data = NULL, stat = "identity",  position = "identity", ..., na.rm = FALSE, show.legend = NA,  inherit.aes = TRUE)geom_pointrange(mapping = NULL, data = NULL, stat = "identity",  position = "identity", ..., fatten = 4, na.rm = FALSE,  show.legend = NA, inherit.aes = TRUE)
#' # Create a simple example datasetdf <- data.frame(  trt = factor(c(1, 1, 2, 2)),  resp = c(1, 5, 3, 4),  group = factor(c(1, 2, 1, 2)),  upper = c(1.1, 5.3, 3.3, 4.2),  lower = c(0.8, 4.6, 2.4, 3.6))p <- ggplot(df, aes(trt, resp, colour = group))plot1 <- p + geom_linerange(aes(ymin = lower, ymax = upper))plot2 <- p + geom_pointrange(aes(ymin = lower, ymax = upper))plot3 <- p + geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2)plot4 <- p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)ggplot2.multiplot(plot1,plot2,plot3,plot4, cols=2)

这里写图片描述

# Draw lines connecting group meansp +  geom_line(aes(group = group)) +  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)

这里写图片描述

# If you want to dodge bars and errorbars, you need to manually specify the dodge widthp <- ggplot(df, aes(trt, resp, fill = group))p +  geom_col(position = "dodge") +  geom_errorbar(aes(ymin = lower, ymax = upper), position = "dodge", width = 0.25)

这里写图片描述

# Because the bars and errorbars have different widths# we need to specify how wide the objects we are dodging aredodge <- position_dodge(width=0.9)p +  geom_col(position = dodge) +  geom_errorbar(aes(ymin = lower, ymax = upper), position = dodge, width = 0.25)

这里写图片描述

5. geom_map——参考地图中的多边形

函数格式:

geom_map(mapping = NULL, data = NULL, stat = "identity", ..., map,  na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))values <- data.frame(  id = ids,  value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5))positions <- data.frame(  id = rep(ids, each = 4),  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,        0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,        2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2))ggplot(values) +  geom_map(aes(map_id = id), map = positions) +  expand_limits(positions)

这里写图片描述

ggplot(values, aes(fill = value)) +  geom_map(aes(map_id = id), map = positions) +  expand_limits(positions)

这里写图片描述

ggplot(values, aes(fill = value)) +  geom_map(aes(map_id = id), map = positions) +  expand_limits(positions) + ylim(0, 3)

这里写图片描述

# Better examplecrimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)crimesm <- reshape2::melt(crimes, id = 1)if (require(maps)) {  states_map <- map_data("state")  ggplot(crimes, aes(map_id = state)) +    geom_map(aes(fill = Murder), map = states_map) +    expand_limits(x = states_map$long, y = states_map$lat)  last_plot() + coord_map()  ggplot(crimesm, aes(map_id = state)) +    geom_map(aes(fill = value), map = states_map) +    expand_limits(x = states_map$long, y = states_map$lat) +    facet_wrap( ~ variable)}

这里写图片描述

ok,本次结束,下次继续。

原创粉丝点击