使用ggplot2构面网格来探索具有连续变量和分类变量的大型数据集

问题描述 投票:0回答:2

我有一个数据集,其中有> 1000个观察值属于A组或B组,并且具有〜150个类别变量和连续变量。下面的小版本。

set.seed(16)
mydf <- data.frame(ID = 1:50, group = sample(c("A", "B"), 50, replace = TRUE), length = rnorm(n = 50, mean = 0, sd = 1), weight = runif(50, min=0, max=1), color = sample(c("red", "orange", "yellow", "green", "blue"), 50,  replace = TRUE), size = sample(c("big", "small"), 50, replace = TRUE))

我想在各个变量之间直观地比较A组和B组。首先,我想使箱形图对的每个连续变量并排显示A和B,并使用条形图为每个类别变量并排显示。认为ggplot facet_grid将是理想的选择,但不确定如何根据数据类型指定绘图类型,也不确定如何在不逐一指定每个变量的情况下执行此操作。

对ggplot2帮助和任何其他探索技术感兴趣。

r ggplot2 data-visualization frame
2个回答
0
投票

如果您分别制作图,然后将它们拼成网格怎么办?

set.seed(16)
mydf <- data.frame(ID = 1:50, group = sample(c("A", "B"), 50, replace = TRUE), length = rnorm(n = 50, mean = 0, sd = 1), weight = runif(50, min=0, max=1), color = sample(c("red", "orange", "yellow", "green", "blue"), 50,  replace = TRUE), size = sample(c("big", "small"), 50, replace = TRUE))


mydf


library(tidyverse)
library(cowplot)
library(reshape)

plot_continuous <- mydf %>%
    melt(id = "group", measure.vars = c("length", "weight")) %>%
    ggplot(aes(x = group, y = value)) +
    geom_boxplot() +
    facet_wrap(~variable)

plot_color <- mydf %>%
    count(group, color) %>%
    ggplot(aes(x = group, y = n)) +
    geom_col(aes(fill = color), position = "dodge") +
    ggtitle("Color")

plot_size <- mydf %>%
    count(group, size) %>%
    ggplot(aes(x = group, y = n)) +
    geom_col(aes(fill = size), position = "dodge") +
    ggtitle("Size")



plot_grid(plot_continuous, plot_color, plot_size, ncol = 2)

0
投票

对于您的分类变量,以可比的方式显示结果的一种有用方法是显示每个答案选项的比例(即,“大小”变量的大小百分比为大,“颜色”变量的每种颜色百分比)。我知道堆栈溢出通常会建议发布问题的人首先展示他们的尝试,而不是要求立即解决方案,我也建议这样做,因为它确实可以帮助每个用户从自己的尝试中学到更多。但是,我在这里发布一个解决方案,并希望它对您来说是一个起点,如果它在所有方面当然都有用的话。

# Data preparation for stacked barchart

group_color_size_df <- mydf %>%
  select(group, color, size) %>%
  mutate(color = factor(color),
         group=factor(group),
         size=factor(size))

# Plot faceted stacked barchart

group_color_size_df %>%
  filter(.$size!="NA") %>%
  ggplot(aes(x = group), fill = size) +
  scale_y_continuous(labels=c("0%","25%","50%","75%","100%"))+
  labs(title= "Group- Size relation")+
  geom_bar(aes(fill = size), width = .35, position = position_fill(reverse = TRUE)) +
  theme(axis.title.x = element_blank(), axis.title.y = element_blank(), panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), panel.background = element_blank(), axis.text.y =element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(plot.title=element_text(size=14, face="bold", color="black"))+
  theme(plot.subtitle=element_text(size=12, face="italic", color="black"))+
  theme(axis.text.x = element_text(size=11))+
  theme(strip.text.y = element_text(angle = 270, size=10))+ 
  theme(legend.text=element_text(size=11))+
  scale_fill_discrete(name="Size", labels=c("big","small"))+
  coord_flip()+
  facet_grid(group~., switch = "y", scales = "free", space = "free") -> stacked_barchart
plot(stacked_barchart)

您可以相应地针对组绘制颜色变量。

现在,对于连续变量,箱线图是一个好主意,您只需要在“组”变量(tidyr程序包)上使用spread()即可实际创建两列“ A”和“ B”:

# Data wrangling for boxplot

length_per_group <- mydf %>%
  select(group, length, weight) %>%
  spread(., group, length) %>%
  select(A,B)

这里您不需要刻面,只需要进行箱线图绘制,因为每个变量“ A”和“ B”现在都包含“长度”数据。然后,您可以将长度替换为权重,并用相同的方法在方框中绘制“权重”变量。

我希望这会有所帮助,一旦您尝试一下,请告诉我们是否需要更多帮助。

© www.soinside.com 2019 - 2024. All rights reserved.