-2

I had the Dataset like

Name,School,Grade,Hobby,Addr1,Addr2,State
Sanjay1,BiharSchool1,11,"Cricket,Hockey,Badminton",xxxx,India,Bihar
Sanjay2,UpSchool1,11,"Volleyball,Hockey,Football",xxxx,India,UP
Sanjay3,BiharSchool2,11,"Boxing",xxxx,India,Bihar
Sanjay4,MPSchool1,11,"Cricket,Hockey,Badminton",xxxx,India,MP
Sanjay5,BiharSchool3,11,"Boxing",xxxx,India,Bihar
Sanjay6,MPSchool1,11,"Cricket,Hockey,Badminton",xxxx,India,MP

Is there way I can create Chart for the following requirements

  1. Show State Name and Different Number of School Frequency. Like Above shows Bihar State has 3 different School, UP & MP has one school ( Even MP has two record but School name is same)
  2. Show Hobbies Frequency like Cricket- 3times, Hockey-4 times, Boxing-2 times and so on
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Sajaym
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 27 '22 at 21:51

1 Answers1

0

1.a Bar plot of School counts by State

Separate the rows by Hobby, and count the needed occurrences. Then pipe to a bar plot.

x <- '
Name,School,Grade,Hobby,Addr1,Addr2,State
Sanjay1,BiharSchool1,11,"Cricket,Hockey,Badminton",xxxx,India,Bihar
Sanjay2,UpSchool1,11,"Volleyball,Hockey,Football",xxxx,India,UP
Sanjay3,BiharSchool2,11,"Boxing",xxxx,India,Bihar
Sanjay4,MPSchool1,11,"Cricket,Hockey,Badminton",xxxx,India,MP
Sanjay5,BiharSchool3,11,"Boxing",xxxx,India,Bihar
Sanjay6,MPSchool1,11,"Cricket,Hockey,Badminton",xxxx,India,MP'
df1 <- read.csv(textConnection(x))

suppressPackageStartupMessages(library(tidyverse))

df1 %>%
  separate_rows(Hobby) %>%
  count(State, School) %>%
  ggplot(aes(State, n, fill = School)) +
  geom_col(position = position_dodge2(preserve = "single")) +
  theme_bw()

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


1.b Count of School by State

df1 %>%
  count(State, name = "School") %>%
  ggplot(aes(State, School)) +
  geom_col(fill = "steelblue2", position = position_dodge()) +
  theme_bw()

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


2. Bar plot of Hobby

The same as above, separate the rows by Hobby, then compute the rows hobbies frequencies. And pipe to a bar chart.

df1 %>%
  separate_rows(Hobby) %>%
  count(Hobby) %>% 
  ggplot(aes(Hobby, n)) +
  geom_col(fill = "steelblue2", position = position_dodge()) +
  theme_bw()

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Hi Rui, It is perfect. Can you help in First Group as I want to show Bihar Showing Frequency 3, UP and MP with Frequency One. Don't need to show Name of School but only count of different school in each State – Sajaym May 28 '22 at 00:00
  • @Sajaym Done, see the edit. – Rui Barradas May 28 '22 at 04:52
  • Thanks Rui for your time and quick solution which also help me to dig better for more reports. – Sajaym Jun 01 '22 at 13:13