Ggplot not plotting all my data

Hi. Please help me understand why this plot is not plotting my female data. female=0, male=1.

table(data_date$date, data_date$gender)
0 1
1 3 4
2 9 13
3 21 33
4 65 66
5 44 55
6 72 64
7 55 39
ggplot(data = data_date, aes(x = date, stat="count", fill = gender)) + coord_flip() +
geom_bar(data = subset(data_date, gender == "0"), color="white") +
geom_bar(data = subset(data_date, gender == "1"), color="white") +
scale_y_continuous() +
scale_x_discrete(labels = date_label) +
labs(title = "Dating Frequency", x = "Dating Frequency", y = "Count") +
scale_fill_discrete("Gender", labels = c("Female", "Male"))

By plotting two different subsets of the data frame you don't get a stacked barplot, you are masking the data with lower values.

To give you a working example of how to make the plot, can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

Here is some sample data:

iid <- c(1,2,3,4,5,6,7,8,9,10)
gender <- c(0,0,0,0,0,1,1,1,1,1) # 0=female, 1=male
date <- c(1,2,1,1,1,1,2,2,2,2) # how freq do you date? 1=sev/wk. 2=never
date <- as.factor(date)
data_date <- data.frame(cbind(iid, gender, date))
data_date
table(data_date$date, data_date$gender)
date_label <- c("sev/wk", "never")

Here is an example:

library(tidyverse)

data_date <- data.frame(
         iid = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
      gender = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
        date = c(1, 2, 1, 1, 1, 1, 2, 2, 2, 2)
)

data_date %>%
    mutate(date = factor(date, labels = c("sev/wk", "never")),
           gender = factor(gender, labels = c("Female", "Male"))) %>% 
    ggplot(aes(y = date, fill = gender)) +
    geom_bar(color = "white") +
    labs(title = "Dating Frequency",
         x = "Dating Frequency",
         y = "Count",
         fill = "Gender")

Created on 2021-05-14 by the reprex package (v2.0.0)

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you, Andresrcs.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.