简体   繁体   中英

Plotting a grouped bar plot in R when you have yes/no columns

I have a small table like so:

在此处输入图片说明

I wish to plot this information as a grouped bar chart in R. However, for that to work correctly, a 'success' row is required above the yes/no columns, which is easy to do in excel but I am not sure how to place that in R. This is my code currently. How do I add a row above where the no/yes columns are?

status1 <- c("currently", "missing", "never", "previously")
no1 <- c(107, 8, 131, 142)
yes1 <- c(104, 8, 232, 132)
tata <- data.frame(status1, no1, yes1)

ggplot(tata, aes(fill=yes1, y=no1, x=status1)) + 
  geom_bar(position="dodge", stat="identity")

You need to convert your data from wide format to long format, this is possible using tidyr function gather, below is a code which will help you to do the required.

library(tidyr)
library(dplyr)
library(ggplot2)

status1 <- c("currently", "missing", "never", "previously")
no1 <- c(107, 8, 131, 142)
yes1 <- c(104, 8, 232, 132)
tata <- data.frame(status1, no1, yes1)

tata %>%
  gather(key = "success", value = value, -status1) %>%
  ggplot(aes(y = value, x = status1, fill = success)) + 
  geom_bar(position = "dodge", stat = "identity")

To play around and add some titles to the graph you can do the following

tata %>%
  gather(key = "success", value = value, -status1) %>%
  ggplot(aes(y = value, x = status1, fill = success)) + 
  geom_bar(position = "dodge", stat = "identity") +
  xlab("Status") + # To change x- axis
  ylab("Number of People") + # To change y-axis
  ggtitle("Success Analysis") + # To change title
  labs(fill = "Success Legend") + # To change legend title
  scale_fill_manual(labels = c("No", "Yes"), values = c("orange", "green")) # To change legend values titles and their colours

I think you need to reshape your data in a longer format. You can do it using pivot_longer function from tidyr :

library(tidyr)
tata %>% pivot_longer(., -status1, names_to = "variable", values_to = "values")

# A tibble: 8 x 3
  status1    variable values
  <fct>      <chr>     <dbl>
1 currently  no1         107
2 currently  yes1        104
3 missing    no1           8
4 missing    yes1          8
5 never      no1         131
6 never      yes1        232
7 previously no1         142
8 previously yes1        132

Then, you can plot your data using yes/no as x-axis like this:

library(tidyr)
tata %>% pivot_longer(., -status1, names_to = "variable", values_to = "values") %>%
  ggplot(., aes(x = variable, y = values, fill = status1))+
  geom_bar(stat = "identity", position = position_dodge())

在此处输入图片说明

or using status1 as x-axis

library(tidyr)
tata %>% pivot_longer(., -status1, names_to = "variable", values_to = "values") %>%
  ggplot(., aes(x = status1, y = values, fill = variable))+
  geom_bar(stat = "identity", position = position_dodge())

在此处输入图片说明

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