简体   繁体   中英

Frequency Plot in R for example dataset

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM