簡體   English   中英

找到每個因子與 plot 直方圖之間的不同值 r

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

我正在學習 r,我想根據 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)
)

我的目標是找到例如:因素Breakfast我想得到它的革命像13.5 - 12.75 , 25.5 - 13.5 , 27.4 - 25.5我想要同樣的LunchDinner然后使用這些差異值 plot 通過使用ggplot在 3 個不同的圖表中。

對此的任何幫助將不勝感激。 謝謝!!!

我們創造了不同的群體

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)

-輸出

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM