0

I got the dataset as following

year<-seq(2000, 2010,2)
erate1<-runif(6,0,1)/6
erate2<-runif(6,0,1)/6
erate3<-runif(6,0,1)/6
erate4<-runif(6,0,1)/6
barplotdf<-data.frame(erate1,erate2,erate3,erate4,year)

The female position will be 2000, 2002, 2004....

The Friday position will be erate1,erate2,

enter image description here

hard worker
  • 181
  • 8

1 Answers1

2

On StackOverflow it's recommended to provide your own attempts to solve your problem. Looking at your previous questions however, I can see that you usually do that, and this question is clear, so here is an answer:

library(tidyverse)
year<-seq(2000, 2010,2)
erate1<-runif(6,0,1)/6
erate2<-runif(6,0,1)/6
erate3<-runif(6,0,1)/6
erate4<-runif(6,0,1)/6
barplotdf<-data.frame(erate1,erate2,erate3,erate4,year)

barplotdf %>% 
  pivot_longer(cols = -c(year)) %>% 
  mutate(sex = ifelse(year %in% c(2000, 2002, 2004), "Female", "Male"),
         day = case_when(name == "erate1" ~ "Fri",
                         name == "erate2" ~ "Sat",
                         name == "erate3" ~ "Sun",
                         name == "erate4" ~ "Thur")) %>% 
  ggplot(aes(x = day, y = value, fill = day)) +
  geom_col() +
  facet_wrap("sex") +
  scale_y_continuous(labels = scales::percent_format(),
                     name = "Percent") +
  scale_fill_discrete(label = c(1, 2, 3, 4))

example_1.png

EDIT

To add the percentages, you need to 'format' the dataframe (pivot longer, add the "sex" variable based on year, and change "erate*" to "day") then create the labels in a second step:

# Load libraries
library(tidyverse)

# Create the dataframe
year<-seq(2000, 2010,2)
erate1<-runif(6,0,1)/6
erate2<-runif(6,0,1)/6
erate3<-runif(6,0,1)/6
erate4<-runif(6,0,1)/6
barplotdf<-data.frame(erate1,erate2,erate3,erate4,year)

# 'First step': format the dataframe to 'feed into' ggplot
formatted_barplotdf <- barplotdf %>% 
  pivot_longer(cols = -c(year)) %>%
  mutate(sex = ifelse(year %in% c(2000, 2002, 2004), "Female", "Male"),
         day = case_when(name == "erate1" ~ "Fri",
                         name == "erate2" ~ "Sat",
                         name == "erate3" ~ "Sun",
                         name == "erate4" ~ "Thur"))

# 'Second step': create a new variable called "perc"
labels_df <- formatted_barplotdf %>% 
  group_by(sex, day) %>% 
  summarise(perc = round(sum(value), 4))

# Plot the dataframe
ggplot(formatted_barplotdf, aes(x = day, fill = day, y = value)) +
  geom_col() +
  facet_wrap("sex") +
  scale_y_continuous(labels = scales::percent_format(),
                     name = "Percent") +
  scale_fill_discrete(label = c(1, 2, 3, 4)) +
# Add the labels from "labels_df"
  geom_text(data = labels_df, aes(y = perc,
                                 label = paste(perc * 100, "%", sep = ""),
                                 vjust = -0.5))

example_2.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • I understand add the percentage to the graph is something like:geom_text(aes( label = scales::percent(..prop..), y= ..prop.. ), stat= "count", vjust = -.5)+ However, when I add this to your code. It shows all the percentage is 100%. could you teach me how to solve this? Thanks a lot – hard worker Apr 09 '21 at 17:21
  • 1
    There are a few ways to add percentage labels to the plot; I've edited my answer to show one way of doing it. – jared_mamrot Apr 11 '21 at 03:28