0

I am new to histograms in ggplot2. I am trying to use ggplot2 to build a histogram for the dataset "Admission"(the picture below). I am using the "Gender" column to be the x-axis, and cut by the "Admit" column. However, the graph(the picture below) only shows the number of male and female which is 2 but not the data in the "Freq" column. This is the code(X is the dataset):

  ggplot(X, aes( x=Gender, fill=Admit) )+
  geom_bar(color="black") +
  labs(x="Gender",
       title="Adimission",
       fill="Admit")

I am not sure that my thinking is right, is there any one can give me some hints? Thanks.

enter image description here

enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60
Yzhang17
  • 3
  • 1
  • 6
    A histogram bins a continuous number; it is not a bar chart. The bar chart defaults to adding the occurrences unless you tell it otherwise. R can't read your mind (yet?). If you want the column `Freq` to be used, you need to tell it that. Change `geom_bar` to `geom_col` and add `y = Freq` to your `aes` in `ggplot`. It looks like you're fairly new to SO; welcome to the community! If you want great answers, make your question reproducible. It's not good to have pictures of your data. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Feb 21 '22 at 21:42
  • 2
    In addition to this not being a histogram or the type of data that a histogram is for, you haven't told ggplot anything about a variable Freq. If you want Freq to be mapped onto something like the y-axis, you have to declare that. Look at the docs for `geom_bar`—unless you assign a y-value, it plots the count of rows in the data, which, of course, is 1 per combination of gender & admit. – camille Feb 21 '22 at 21:46

1 Answers1

2

Maybe you want to go with geom_col?

df <- data.frame (Gender= as.factor(c("Male", "Female", "Male", "Female")),
                  Admit = as.factor(c("Admitted", "Admitted", "Rejected", "Rejected")),
                  Freq = c(1158, 557, 1493, 1278),
                  Prop = c(0.675, 0.324, 0.539, 0.461))

Specify the y axis to be the Frequency

ggplot(df, aes(x=Gender, y=Freq, fill=Admit) )+
  geom_col() +
  labs(x="Gender",
       title="Admission",
       fill="Admit")

Which gives:

enter image description here

Is that what you are looking for?

Gnueghoidune
  • 1,237
  • 4
  • 13