本文介绍了如何将以下日期集转换为长格式并制作条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的数据集如下

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)

女性职位将是2000年,2002年,2004年....

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

星期五的位置将为erate1,erate2,

The Friday position will be erate1,erate2,

推荐答案

在StackOverflow上,建议您自己尝试解决问题.但是,从您以前的问题来看,我可以看到您通常会这样做,并且这个问题很清楚,所以这里有一个答案:

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

要添加百分比,您需要对数据框进行格式化"(更长的透视图,根据年份添加性别"变量,然后将"erate *"更改为天"),然后在第二步:

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

这篇关于如何将以下日期集转换为长格式并制作条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:35