0

I have data from an interview and I want to make a bargraph using ggplot2.

The structure of my data looks like so: enter image description here

How do I make a bar graph like

  1. y axis must be count
  2. x axis must be "Very good", "Good", "Not bad", "Bad", "Very bad".
  3. There should be two graphs (Yesterday and Today with differ colour) above each of x variables.
  4. Optional: Can I seperate each of graphs with gender?
stefan
  • 90,330
  • 6
  • 25
  • 51
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including the code you have tried and a snippet of your data or some fake data. Please do not post an image of code/data/errors [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). – stefan Apr 12 '22 at 06:10
  • If the answer by Emily Kothe below was helpful, please accept her answer by clicking the tick sign/checkmark sign next to her reply. – Phil.He Apr 12 '22 at 09:00
  • Welcome to SO! Please do not post data as images, but share a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so others can help. – SamR Apr 12 '22 at 12:43

1 Answers1

1

If you want to make two graphs to display Yesterday and Today in a single ggplot (or want to display both in the same plot) you'll need to include a pivot_longer

Example data

df <- as.data.frame(structure(c(1L, 2, 3, 4, 5, 6, "Male", "Female", "Female", 
            "Male", "Male", "Female", "Very good", "Good", "Not bad", "Bad", 
            "Very bad", NA, "Good", "Bad", "Very good", "Very bad", "Not bad", 
            NA), .Dim = c(6L, 4L), .Dimnames = list(NULL, c("id", "Gender", 
                                                            "Yesterday", "Today"))))
df
#>   id Gender Yesterday     Today
#> 1  1   Male Very good      Good
#> 2  2 Female      Good       Bad
#> 3  3 Female   Not bad Very good
#> 4  4   Male       Bad  Very bad
#> 5  5   Male  Very bad   Not bad
#> 6  6 Female      <NA>      <NA>

Wrangling and plotting

library(tidyverse)
library(ggplot2)

df %>% 
  pivot_longer(c(-id, -Gender)) %>% 
  ggplot(aes(x = value, fill = Gender)) +
  geom_bar() +
  facet_wrap("name")

Created on 2022-04-12 by the reprex package (v2.0.1)

Emily Kothe
  • 842
  • 1
  • 6
  • 17