14

I've got this data:

         No    Yes
Female  411   130
Male    435   124

which was created using the standard table command. Now with plot I can plot this as such:

plot(table(df$gender, df$fraud))

and it then outputs a 2x2 bar chart.

So my question is, how can I do this with ggplot2? Is there any way with out transforming the table-object to a data frame? I would do that, but it becomes a mess and you then need to rename column and row headers and it just becomes a mess for what is really a quite simple thing?

1
  • Does: plot(table(df$gender, df$fraud)) actually return a mosaicplot right? Commented Aug 20, 2022 at 18:25

3 Answers 3

25

Something such as

ggplot(as.data.frame(table(df)), aes(x=gender, y = Freq, fill=fraud)) + 
    geom_bar(stat="identity")

gets a similar chart with a minimum amount of relabelling.

Sign up to request clarification or add additional context in comments.

1 Comment

It would be helpful if there was an example of this that could be seen ( the plot ) and run ( the code )
5

ggplot2 works with data frame, so, you have to convert table into a frame. Here is a sample code:

myTable <- table(df$gender, df$fraud)
myFrame <- as.data.frame(table(myTable))

Now, you can use myFrame in ggplot2:

ggplot(myFrame, aes(x=gender))+
  geom_bar(y = Freq)

see Coerce to a Data Frame for more information.

Comments

1

For the record, janitor::tabyl() outputs contingency tables that are data.frames. As such, they are more convenient for a workflow based on tidyverse tools.

For example:

# Data
df <- data.frame(gender = c(rep("female", times = 411),
                            rep("female", times = 130),
                            rep("male", times = 435),
                            rep("male", times = 124)),
                 fraud = c(rep("no", times = 411),
                           rep("yes", times = 130),
                           rep("no", times = 435),
                           rep("yes", times = 124)))

# Plotting the tabulation with tidyverse tools
df |> 
  janitor::tabyl(gender, fraud) |>
  tidyr::gather(key = fraud, value = how_many, no:yes) |>
  ggplot2::ggplot(aes(y = how_many, x = gender, fill = fraud)) +
  geom_col()

Note: The sequence of piped results with janitor and tidyr has the benefit of being more transparent, but it essentially replicates the same result achieved with as.data.frame(table(df)).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.