简体   繁体   English

在 R 中创建马赛克 plot

[英]Creating mosaic plot in R

I have tried many things but cannot make the mosaic plot work.我尝试了很多东西,但无法使马赛克 plot 工作。 I start with a data frame:我从一个数据框开始:

df = data.frame(effect = c("no","no", "yes", "yes"),
            sex = c("f","m","f","m"),
            n = c(8,3,8,12))

df$effect <- factor((df$effect), levels=c("yes", "no"))
df$sex <- factor(df$sex)

I tried ggplot:我试过ggplot:

windows(width=3.5, height=3.5 )
ggplot(df) +
geom_bar(aes(effect, fill = sex))

I tried another ggplot:我尝试了另一个ggplot:

library(ggmosaic)
windows(width=3.5, height=3.5 )
ggplot(df) + 
geom_mosaic(aes(x = product(effect), fill = sex)) + 
labs(x = "effect", y = "number")

I tried another approach:我尝试了另一种方法:

library("graphics")
windows(width=3.5, height=3.5 )
with(df,
mosaicplot(table(effect, sex), color=TRUE))

Whatever I tried the numbers in the cells are not represented correctly on the plots.无论我尝试了什么,单元格中的数字都没有在图上正确表示。 I cannot figure out what I am doing wrong...我无法弄清楚我做错了什么......

You need to include the value for n in the definition of the plot.您需要在绘图定义中包含 n 的值。 Also since you are summing the values a geom_col() is more appropriate than geom_barr() .此外,由于您正在对值求和,因此geom_col()geom_barr()更合适。 In order to have the bars fill the either region, add position="fill" to the geometry definition.为了让条形填充任一区域,请将 position="fill" 添加到几何定义。

df = structure(list(effect = structure(c(2L, 2L, 1L, 1L), .Label = c("yes", 
     "no"), class = "factor"), sex = structure(c(1L, 2L, 1L, 2L), 
      .Label = c("f",  "m"), class = "factor"), n = c(8, 3, 8, 12)), 
      row.names = c(NA, -4L), class = "data.frame")

ggplot(df, aes(effect, y=n, fill = sex)) +
  geom_col(position="fill")

在此处输入图片说明

To change the bar's widths you can try something like:要更改栏的宽度,您可以尝试以下操作:

library(dplyr)
widths<-df %>% group_by(effect) %>% summarize(value=sum(n)) %>% mutate(value=value/sum(value))
ggplot(df, aes(effect, y=n, fill = sex)) +
  geom_col(position="fill", width=1.8*rep(widths$value, each=2))

You can use the mosaicplot function from graphics .您可以使用graphics 中mosaicplot函数。 However, the data needs to be in a "table" or raw data format, not aggregated.但是,数据需要采用“表格”或原始数据格式,而不是聚合。 Your data is aggregated, so we need to "deaggregate" it using xtabs :您的数据已聚合,因此我们需要使用xtabs对其进行“解聚合”:

xtab <- xtabs(n~sex+effect, data=df)
   effect
sex yes no
  f   8  8
  m  12  3

Then either of the following will work.然后以下任一方法都将起作用。

mosaicplot(xtab, main="Sex v Effect", col=TRUE)
mosaicplot(~sex+effect, data=xtab, main="Sex v Effect", col=TRUE)

在此处输入图片说明

You could also use the ggmosaic package to create a mosaic plot with a ggplot2 look like this:您还可以使用ggmosaic package 创建马赛克 plot 和ggplot2看起来像这样:

df = data.frame(effect = c("no","no", "yes", "yes"),
                sex = c("f","m","f","m"),
                n = c(8,3,8,12))

df$effect <- factor((df$effect), levels=c("yes", "no"))
df$sex <- factor(df$sex)
library(ggmosaic)
ggplot(data = df) +
  geom_mosaic(aes(x = product(effect), fill=sex, weight = n)) +
  theme_mosaic()

Created on 2022-08-15 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 15 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM