How di I make bar charts in which bars are in dodge position

I want the bar chart in such a way that the variables "kan_sp","kan_read" come side by side in a single plot. But I am unable to do it.

library(tidyverse)
nithin<-tibble::tribble(
  ~student_id, ~kan_sp, ~kan_read, ~eng_sp, ~eng_read,
      "st001",      1L,        0L,      1L,        0L,
      "st002",      1L,        0L,      1L,        0L,
      "st003",      1L,        0L,      1L,        0L,
      "st004",      1L,        1L,      1L,        0L,
      "st005",      1L,        0L,      1L,        0L,
      "st006",      1L,        1L,      1L,        0L,
      "st007",      1L,        0L,      1L,        0L,
      "st008",      1L,        0L,      0L,        1L,
      "st009",      0L,        0L,      0L,        1L,
      "st010",      0L,        1L,      1L,        1L,
      "st011",      0L,        1L,      1L,        1L,
      "st012",      0L,        1L,      0L,        0L,
      "st013",      1L,        1L,      0L,        0L,
      "st014",      1L,        1L,      1L,        1L,
      "st015",      1L,        1L,      1L,        1L
  )

nithin %>% 
  ggplot()+geom_bar(kan_sp,position="dodge")+geom_bar(kan_read,position="dodge")+
  geom_bar(eng_sp,position="dodge")+
  geom_bar(eng_read,position="dodge")
#> Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomBar, : object 'kan_sp' not found

Yeah i actually know the code I wrote was stupid. I just want the count of 1s and 0s.

Hi @kuttan98,

Is this what you're looking for?

library(tidyverse)
nithin<-tibble::tribble(
  ~student_id, ~kan_sp, ~kan_read, ~eng_sp, ~eng_read,
  "st001",      1L,        0L,      1L,        0L,
  "st002",      1L,        0L,      1L,        0L,
  "st003",      1L,        0L,      1L,        0L,
  "st004",      1L,        1L,      1L,        0L,
  "st005",      1L,        0L,      1L,        0L,
  "st006",      1L,        1L,      1L,        0L,
  "st007",      1L,        0L,      1L,        0L,
  "st008",      1L,        0L,      0L,        1L,
  "st009",      0L,        0L,      0L,        1L,
  "st010",      0L,        1L,      1L,        1L,
  "st011",      0L,        1L,      1L,        1L,
  "st012",      0L,        1L,      0L,        0L,
  "st013",      1L,        1L,      0L,        0L,
  "st014",      1L,        1L,      1L,        1L,
  "st015",      1L,        1L,      1L,        1L
)

# Basic Plot

nithin %>%
  pivot_longer(-student_id) %>%
  ggplot(aes(x = name, y = value)) +
  geom_col()

# Make it nicer

nithin %>%
  pivot_longer(-student_id) %>%
  mutate(name = str_replace_all(name, "_", " ") %>% str_to_title() ,
         name = fct_reorder(name, value, sum)) %>%
  ggplot(aes(x = name, y = value)) +
  geom_col() +
  theme_light() +
  labs(x = NULL, y = "Count") +
  scale_y_continuous(expand = expansion(c(0,.1)))

Created on 2022-03-05 by the reprex package (v2.0.1)

Oh yes, This is what I wanted. I don't why I used dodge.. Thanks a lot.

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.