简体   繁体   English

找到每个因子与 plot 直方图之间的不同值 r

[英]Find the different value between each factor and plot the histogram in r

I am learning r and I want to build the histograms base on the revolution of each factor depend on the date in my dataframe.我正在学习 r,我想根据 dataframe 中的日期的每个因素的旋转来构建直方图。

Here is my dataframe:这是我的 dataframe:

dat <- data.frame(
  time = factor(c("Breakfast","Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner","Dinner"), levels=c("Breakfast","Lunch","Dinner")), 
  date=c("2020-01-20","2020-01-21","2020-01-22","2020-01-23","2020-01-20","2020-01-21","2020-01-22","2020-01-23","2020-01-20","2020-01-21","2020-01-22","2020-01-23"),
  total_bill = c(12.75,13.5,25.5,27.4,18.3,19.9,27.8,28.6,15.7,17.4,19.5,24.2)
)

My goal is to find for example: factor Breakfast I want to get the revolution of it like 13.5 - 12.75 , 25.5 - 13.5 , 27.4 - 25.5 and I want the same for Lunch , Dinner and then used those difference values to plot by using ggplot in 3 different graphs.我的目标是找到例如:因素Breakfast我想得到它的革命像13.5 - 12.75 , 25.5 - 13.5 , 27.4 - 25.5我想要同样的LunchDinner然后使用这些差异值 plot 通过使用ggplot在 3 个不同的图表中。

Any help for this would be much appreciated.对此的任何帮助将不胜感激。 Thank you!!!谢谢!!!

We create do a group by difference我们创造了不同的群体

library(dplyr)
library(lubridate)
library(ggplot2)
dat %>%
    mutate(date = ymd(date)) %>%
    arrange(time, date) %>%
    group_by(time) %>% 
    mutate(Diff  = c(0, diff(total_bill)))  %>% 
    ungroup %>%
    filter(Diff != 0) %>%
    ggplot(aes(x = date, y = Diff, fill = time)) +
       geom_col()+ 
       facet_wrap(~ time)

-output -输出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM