BARPLOT多个变量(R)

发布于 2025-01-21 17:12:21 字数 733 浏览 0 评论 0原文

我想创建一个带有多个变量的条图。

我有几个变量:“你去餐厅吗”,“你去杂货店吗?” ...所有值都是“是”或“否”。我想创建一个带有x =“ yes”和“ no”的小号,y = count,fill =所有变量。

但是我不明白该怎么做,我在互联网上搜索,但我的问题是由于我的X造成的。

I tried this :

consommation_apres <- teletravail %>% select(reponse = rep("yes","no"), AP_livraison,AP_livraison_plateforme,AP_aucun,AP_resto,AP_drive,AP_livraison)

ggplot(consommation_apres, aes(x = reponse, y = count(), fill = consommation_apres)) +
  labs(title="Consommation après le Télétravail") +
  geom_bar(stat = "identity", position = "dodge")

This is my Data :

Data in french oui = yes, non = no

I真的很抱歉这个问题。

感谢您的帮助。

I want to create a bar plot with multiple variables.

I have several variables: "Do you go to restaurant", "Do you go to grocery store ?" ... All the values are "yes" or "no". I want to create a barplot with x = "yes" and "no" and y = count, fill = all the variable.

But I don't understand how to do it, I search on the internet but my problem is due to my x.

I tried this :

consommation_apres <- teletravail %>% select(reponse = rep("yes","no"), AP_livraison,AP_livraison_plateforme,AP_aucun,AP_resto,AP_drive,AP_livraison)

ggplot(consommation_apres, aes(x = reponse, y = count(), fill = consommation_apres)) +
  labs(title="Consommation après le Télétravail") +
  geom_bar(stat = "identity", position = "dodge")

This is my Data :

Data in french oui = yes, non = no

I really sorry for the question.

Thanks you for your help.

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

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

发布评论

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

评论(1

浮世清欢 2025-01-28 17:12:21

如果您使用的是ggplot,则需要将数据放入长时间的格式中。在这里,我们还可以在绘制之前总结数据。

library(tidyverse)

df %>%
  pivot_longer(everything(), names_to = "name", values_to = "response") %>%
  group_by(name, response) %>%
  summarise(cnt = n()) %>%
  ggplot(aes(x = response, y = cnt, fill = name)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title="Consommation après le Télétravail")

output

df %>%
  pivot_longer(everything(), names_to = "name", values_to = "response") %>%
  group_by(name, response) %>%
  summarise(cnt = n()) %>%
  ggplot(aes(x = name, y = cnt, fill = response)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title="Consommation après le Télétravail")

​rel =“ nofollow noreferrer”> “在此处输入图像描述”

data

df <- structure(list(AP_livraison = c("yes", "no", "no", "yes", "no", 
"no", "yes", "no", "no"), AP_livraison_plateforme = c("yes", 
"yes", "no", "yes", "yes", "no", "yes", "yes", "no"), AP_aucun = c("yes", 
"yes", "no", "yes", "yes", "no", "yes", "yes", "no"), AP_resto = c("yes", 
"no", "no", "yes", "no", "no", "yes", "no", "no"), AP_drive = c("yes", 
"yes", "no", "yes", "yes", "no", "yes", "yes", "no")), class = "data.frame", row.names = c(NA, 
-9L))

You need to put the data into a long format for plotting if you are using ggplot. Here, we can also summarize the data before plotting.

library(tidyverse)

df %>%
  pivot_longer(everything(), names_to = "name", values_to = "response") %>%
  group_by(name, response) %>%
  summarise(cnt = n()) %>%
  ggplot(aes(x = response, y = cnt, fill = name)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title="Consommation après le Télétravail")

Output

enter image description here

Or if you wanted to see each category together, then we could do:

df %>%
  pivot_longer(everything(), names_to = "name", values_to = "response") %>%
  group_by(name, response) %>%
  summarise(cnt = n()) %>%
  ggplot(aes(x = name, y = cnt, fill = response)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title="Consommation après le Télétravail")

enter image description here

Data

df <- structure(list(AP_livraison = c("yes", "no", "no", "yes", "no", 
"no", "yes", "no", "no"), AP_livraison_plateforme = c("yes", 
"yes", "no", "yes", "yes", "no", "yes", "yes", "no"), AP_aucun = c("yes", 
"yes", "no", "yes", "yes", "no", "yes", "yes", "no"), AP_resto = c("yes", 
"no", "no", "yes", "no", "no", "yes", "no", "no"), AP_drive = c("yes", 
"yes", "no", "yes", "yes", "no", "yes", "yes", "no")), class = "data.frame", row.names = c(NA, 
-9L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文