查看原文
其他

R可视化11|ggplot2-图层图形语法 (3)

pythonic生物人 pythonic生物人 2022-10-25

"pythonic生物人"的第109篇分享

本文系统介绍ggplot2的统计变换(stat)、位置设置(Position adjustments)和标度(scale)。

本文目录

6、统计变换(stat
  stats can be created with a geom_ function
  stats can’t be created with a geom_ function

7、 位置设置(Position adjustments)
  条形图中stack|fill|dodge
  散点图中nudge|jitter|jitterdodge

8、标度(scale)
  marker的形状和大小
  图例和坐标轴

6、统计变换(stat

将data经过一种统计方法整理,然后再绘图,ggplot2的统计方法如下,都是以stat_开头的函数。

可以分为两类:

stats can be created with a geom_ function

  • stat_bin()geom_bar()geom_freqpoly()geom_histogram()
  • stat_bin2d()geom_bin2d()
  • stat_bindot()geom_dotplot()
  • stat_binhex()geom_hex()
  • stat_boxplot()geom_boxplot()
  • stat_contour()geom_contour()
  • stat_quantile()geom_quantile()
  • stat_smooth()geom_smooth()
  • stat_sum()geom_count()
options(repr.plot.width = 4.5, repr.plot.height = 3, repr.plot.res = 300)
f <- ggplot(mpg, aes(class, hwy))
f + geom_boxplot() 

stats can’t be created with a geom_ function

  • stat_ecdf(): compute a empirical cumulative distribution plot.
  • stat_function(): compute y values from a function of x values.
  • stat_summary(): summarise y values at distinct x values.
  • stat_summary2d()stat_summary_hex(): summarise binned values.
  • stat_qq(): perform calculations for a quantile-quantile plot.
  • stat_spoke(): convert angle and radius to position.
  • stat_unique(): remove duplicated rows.
ggplot(mpg, aes(trans, cty)) + 
  geom_point() + 
  stat_summary(geom = "point", fun = "mean", colour = "red", size = 4)

7、 位置设置(Position adjustments)

  • 条形图中stack|fill|dodge

  • position_stack(): stack overlapping bars (or areas) on top of each other.
  • position_fill(): stack overlapping bars, scaling so the top is always at 1.
  • position_dodge(): place overlapping bars (or boxplots) side-by-side.
options(repr.plot.width = 4.5, repr.plot.height = 5, repr.plot.res = 300)
dplot <- ggplot(diamonds, aes(color, fill = cut)) + 
  xlab(NULL) + ylab(NULL) + theme(legend.position = "none")


p1 <- dplot + geom_bar()#默认堆叠
p2 <- dplot + geom_bar(position = "fill")#堆叠且按比例
p3 <- dplot + geom_bar(position = "dodge")#并列

p4 <- grid.arrange(p1,p2,p3,nrow = 3)
ggsave("scale23.png", p4, width = 4.5, height = 5
  • 散点图中nudge|jitter|jitterdodge

  • position_nudge(): move points by a fixed offset.
  • position_jitter(): add a little random noise to every position.
  • position_jitterdodge(): dodge points within groups, then add a little random noise.
p1 <- ggplot(mpg, aes(displ, hwy)) + 
  geom_point(position = "jitter")
p2 <- ggplot(mpg, aes(displ, hwy)) + 
  geom_point(position = position_jitter(width = 0.05, height = 0.5))
p3 <- ggplot(mpg, aes(displ, hwy)) + 
  geom_jitter(width = 0.05, height = 0.5)

p4 <- grid.arrange(p1,p2,p3,nrow = 3)
ggsave("scale24.png", p4, width = 4.5, height = 5

8、标度(scale

这部分内容,这里介绍一些没提到的,前面写已过三篇文章详细介绍:

  • marker的形状和大小

使用第一行的数字即可使用第二行的形状。

options(repr.plot.width = 6.5, repr.plot.height = 5, repr.plot.res = 300)

#marker形状
p <- e + geom_point(aes(shape = fl, size = cyl))

p1 <- p + scale_shape() + scale_size() 
p2 <- p + scale_shape_manual(values = c(1:26))#values = c(1:26)指定marker

#marker大小
p3 <- p + scale_radius(range = c(1,6))
p4 <- p + scale_size_area(max_size = 6)
p5 <- grid.arrange(p2,p3,p4,nrow = 2)
ggsave("scale25.png", p5, width = 4.5, height = 5
  • 图例和坐标轴


本文结束,更多好文:

有用请“点赞”“在看”“分享”

有意见请移步到QQ群629562529反馈,一起进步哈!


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存