R中的频率图例如数据集

发布于 2025-02-02 10:55:41 字数 604 浏览 2 评论 0原文

我拥有数据集,就像

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

在那里一样,我可以为以下要求创建图表

  1. 显示状态名称和不同数量的学校频率。像上面的显示,比哈尔邦州有3个不同的学校,UP&国会议员有一所学校(甚至国会议员都有两个记录,但学校名称相同)
  2. 显示了爱好频率,例如板球 - 3次,曲棍球-4次,拳击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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最丧也最甜 2025-02-09 10:55:41

1. School的条形图通过状态计数

通过hobby将行分开,并计算所需的事件。然后将管道带到条形图。

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

“”

在2022-05-27上由 reprex软件包(v2.0.1)


1.B School> School> School> School> by state> state

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

在2022-05-28上创建的 reprex软件包(v2.0.1)


2

。计算行爱好频率。和管道到条形图。

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

“”

reprex软件包(v2.0.1)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文