使用 ggplot2 合并并完美对齐直方图和箱线图 [英] Merge and Perfectly Align Histogram and Boxplot using ggplot2

查看:49
本文介绍了使用 ggplot2 合并并完美对齐直方图和箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从昨天开始,我正在阅读答案和网站,以便在一个图中组合和对齐使用 ggplot2 包生成的 histogramboxplot.

这个问题与其他问题不同,因为boxplot chart需要减少heightaligned直方图.

考虑以下数据集:

my_df <- 结构(列表(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,92, 93, 94, 95, 96, 97, 98, 99, 100), 值= c(18, 9, 3,4, 3, 13, 12, 5, 8, 37, 64, 107, 11, 11, 8, 18, 5, 13, 13, 14,11, 11, 9, 14, 11, 14, 12, 10, 11, 10, 5, 3, 8, 11, 12, 11, 7,6, 6, 4, 11, 8, 14, 13, 14, 15, 10, 2, 4, 4, 8, 15, 21, 9, 5,7, 11, 6, 11, 2, 6, 16, 5, 11, 21, 33, 12, 10, 13, 33, 35, 7,7, 9, 2, 21, 32, 19, 9, 8, 3, 26, 37, 5, 6, 10, 18, 5, 70, 48,30, 10, 15, 18, 7, 4, 19, 10, 4, 32)), row.names = c(NA, 100L), class = "data.frame", .Names = c("id", "value"))

我生成了箱线图:

require(dplyr)需要(ggplot2)my_df %>% 选择(值)%>%ggplot(aes(x="", y = value)) +geom_boxplot(fill = "lightblue", color = "black") +coord_flip() +主题经典() +xlab("") +主题(axis.text.y=element_blank(),axis.ticks.y=element_blank())

我生成了直方图

my_df %>% select(id, value) %>%ggplot() +geom_histogram(aes(x = value, y = (..count..)/sum(..count..)),位置 = 身份",binwidth = 1,填充=浅蓝色",颜色=黑色")+ylab("相对频率") +主题经典()

我希望获得的结果是一个单一的情节,如:

请注意,箱线图必须降低高度并且刻度线必须完全对齐,以便为同一视觉提供不同的视角.

解决方案

您可以使用 eggcowplotpatchwork 包来组合那两个地块.有关更复杂的示例,另请参阅此

since yesterday I am reading answers and websites in order to combine and align in one plot an histogram and a boxplot generated using ggplot2 package.

This question differs from others because the boxplot chart needs to be reduced in height and aligned to the left outer margin of the histogram.

Considering the following dataset:

my_df <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 
92, 93, 94, 95, 96, 97, 98, 99, 100), value= c(18, 9, 3, 
4, 3, 13, 12, 5, 8, 37, 64, 107, 11, 11, 8, 18, 5, 13, 13, 14, 
11, 11, 9, 14, 11, 14, 12, 10, 11, 10, 5, 3, 8, 11, 12, 11, 7, 
6, 6, 4, 11, 8, 14, 13, 14, 15, 10, 2, 4, 4, 8, 15, 21, 9, 5, 
7, 11, 6, 11, 2, 6, 16, 5, 11, 21, 33, 12, 10, 13, 33, 35, 7, 
7, 9, 2, 21, 32, 19, 9, 8, 3, 26, 37, 5, 6, 10, 18, 5, 70, 48, 
30, 10, 15, 18, 7, 4, 19, 10, 4, 32)), row.names = c(NA, 100L
), class = "data.frame", .Names = c("id", "value"))

I generated the boxplot:

require(dplyr)
require(ggplot2)
my_df %>% select(value) %>%
        ggplot(aes(x="", y = value)) +
        geom_boxplot(fill = "lightblue", color = "black") + 
        coord_flip() +
        theme_classic() +
        xlab("") +
        theme(axis.text.y=element_blank(),
              axis.ticks.y=element_blank())

and I generated the histogram

my_df %>% select(id, value) %>%
        ggplot() +
        geom_histogram(aes(x = value, y = (..count..)/sum(..count..)),
                       position = "identity", binwidth = 1, 
                       fill = "lightblue", color = "black") +
        ylab("Relative Frequency") +
        theme_classic()

The result I am looking to obtain is a single plot like:

Note that the boxplot must be reduced in height and the ticks must be exactly aligned in order to give a different perspective of the same visual.

解决方案

You can use either egg, cowplot or patchwork packages to combine those two plots. See also this answer for more complex examples.

library(dplyr)
library(ggplot2)

plt1 <- my_df %>% select(value) %>%
  ggplot(aes(x="", y = value)) +
  geom_boxplot(fill = "lightblue", color = "black") + 
  coord_flip() +
  theme_classic() +
  xlab("") +
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

plt2 <- my_df %>% select(id, value) %>%
  ggplot() +
  geom_histogram(aes(x = value, y = (..count..)/sum(..count..)),
                 position = "identity", binwidth = 1, 
                 fill = "lightblue", color = "black") +
  ylab("Relative Frequency") +
  theme_classic()

egg

# install.packages("egg", dependencies = TRUE)
egg::ggarrange(plt2, plt1, heights = 2:1)

cowplot

# install.packages("cowplot", dependencies = TRUE)
cowplot::plot_grid(plt2, plt1, 
                   ncol = 1, rel_heights = c(2, 1),
                   align = 'v', axis = 'lr')  

patchwork

# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
plt2 + plt1 + plot_layout(nrow = 2, heights = c(2, 1))

这篇关于使用 ggplot2 合并并完美对齐直方图和箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆