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
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)
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)
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.