R-gghalves包将图表“合”起来

要想使多个图合理放置,做到既不能互相遮挡又不能更改大小、位置等属性,确实较为麻烦。而R-gghalves包则可以很好的解决这个问题,gghalves包可轻松绘制半面图,如将箱线图分半,这样就可以实现在箱线图旁绘制其他的图表类型。官网为:https://erocoar.github.io/gghalves/。接下来,简单介绍下R-gghalves的主要绘图函数:

  • geom_half_boxplot():绘制半箱线图
  • geom_half_violin():绘制半小提琴图
  • geom_half_point():绘制半点图

由于以上绘图函数和ggplot2中对应的geom_boxplot()geom_violin()geom_point()参数类似,这里不再过多赘述。需要注意的是上述三个绘图函数中有一个特殊参数-side用于显示哪一半图形,l代表左,r代表右,默认值为l

R-gghalves包实例演示

geom_half_point()

在使用geom_half_point()函数绘制之前,先使用geom_point()绘制,以便大家更好理解:

  1. point <- ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  2. geom_point(shape=21,size=3,fill="#A593E0",colour="black",stroke=.8) +
  3. labs(
  4. title = "Example of <span style='color:#D20F26'>gghalves::geom_half_point function</span>",
  5. subtitle = "processed charts with <span style='color:#1A73E8'>geom_point()</span>",
  6. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  7. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  8. theme(
  9. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  10. size = 20, margin = margin(t = 1, b = 12)),
  11. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  12. plot.caption = element_markdown(face = 'bold',size = 12))

绘图技巧 | 合理利用图层空间的可视化技巧 - 图1
Example of geom_point()
接下来使用geom_half_point()绘制:

  1. half_point <- ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  2. geom_half_point(shape=21,size=3,fill="#A593E0",colour="black",stroke=.8,side = "l")

绘图技巧 | 合理利用图层空间的可视化技巧 - 图2
Example of geom_half_point
可以看出,geom_half_point()使点在水平或者垂直方向上进行抖动,而 side = "l" 的设置则使其左移。
此外,当对不同类别的数据点进行绘制时,则需要使用geom_half_point_panel(),这里和箱线图一同绘制:

  1. ggplot(iris, aes(y = Sepal.Width)) +
  2. geom_boxplot(fill="#A593E0") +
  3. geom_half_point_panel(shape=21,size=2.5,colour="black",stroke=.8,
  4. aes(x = 0.5, fill = Species),
  5. transformation = ggbeeswarm:::position_quasirandom())

绘图技巧 | 合理利用图层空间的可视化技巧 - 图3
Example of geom_half_point_panel and geom_boxplot

geom_half_boxplot()

还是一样,首先使用geom_boxplot()绘制:

  1. gplot(iris, aes(x = Species, y = Sepal.Width,fill=Species)) +
  2. geom_boxplot() +
  3. ggsci::scale_fill_nejm()+
  4. labs(
  5. title = "Example of <span style='color:#D20F26'>gghalves::geom_half_boxplot function</span>",
  6. subtitle = "processed charts with <span style='color:#1A73E8'>geom_boxplot()</span>",
  7. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  8. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  9. theme(
  10. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  11. size = 20, margin = margin(t = 1, b = 12)),
  12. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  13. plot.caption = element_markdown(face = 'bold',size = 12))

绘图技巧 | 合理利用图层空间的可视化技巧 - 图4
Example of geom_boxplot
geom_half_boxplot():

  1. ggplot(iris, aes(x = Species, y = Sepal.Width,fill=Species)) +
  2. geom_half_boxplot(side = "r")

绘图技巧 | 合理利用图层空间的可视化技巧 - 图5
Example of geom_half_boxplot

geom_half_violin()

这里直接绘制geom_half_violin()结果:

  1. ggplot(iris, aes(x = Species, y = Sepal.Width,fill=Species)) +
  2. geom_half_violin(side="r") +
  3. ggsci::scale_fill_nejm() +
  4. labs(
  5. title = "Example of <span style='color:#D20F26'>gghalves::geom_half_violin function</span>",
  6. subtitle = "processed charts with <span style='color:#1A73E8'>geom_half_violin()</span>",
  7. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  8. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  9. theme(
  10. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  11. size = 20, margin = margin(t = 1, b = 12)),
  12. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  13. plot.caption = element_markdown(face = 'bold',size = 12))

绘图技巧 | 合理利用图层空间的可视化技巧 - 图6
Example of geom_half_violin()

geom_half_dotplot()

  1. ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  2. geom_half_violin(data = iris,aes(fill=Species)) +
  3. geom_dotplot(shape=21,binaxis = "y", method="histodot", stackdir="up",fill="#A593E0",
  4. colour="black",stroke=1)+
  5. ggsci::scale_fill_nejm() +
  6. labs(
  7. title = "Example of <span style='color:#D20F26'>gghalves::geom_half_violin function</span>",
  8. subtitle = "processed charts with <span style='color:#1A73E8'>geom_violin()+geom_dotplot</span>",
  9. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  10. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  11. theme(
  12. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  13. size = 20, margin = margin(t = 1, b = 12)),
  14. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  15. plot.caption = element_markdown(face = 'bold',size = 12))

绘图技巧 | 合理利用图层空间的可视化技巧 - 图7
Example of geom_half_violin_dotplot()
当然,还可以这样:

  1. ggplot(iris, aes(x = Species, y = Sepal.Width,fill=Species)) +
  2. geom_half_violin() +
  3. geom_dotplot(shape=21,binaxis = "y", method="histodot", stackdir="up",
  4. colour="black",stroke=1) +
  5. ggsci::scale_fill_jco()

绘图技巧 | 合理利用图层空间的可视化技巧 - 图8
Example02 of geom_half_violin_dotplot()