簡體   English   中英

如何以時間為 x 軸使用 geom_col

[英]How to use geom_col with time as x-axis

我正在嘗試構建一個簡單的條形圖 plot,時間以hms為 x 軸,值以 y 軸為單位。 使用geom_col不起作用,請考慮以下示例:

library(tidyverse)  
  
tribble(~time,    ~bumps,
"10:20:07.818923",     97,
"10:22:43.303376",    250,
"10:31:09.627506",    300,
"10:31:09.628242",     58,
"10:34:05.230866",    100,
"10:52:57.201534",     89,
"10:52:57.249029",     11,
"11:30:31.223439",    100,
"11:30:32.380126",    100,
"11:47:58.033180",    430,
"11:47:58.242451",     86,
"12:05:39.926110",      1,
"12:14:23.244696",     28,
"12:14:23.392286",      5,
"14:09:59.180788",   1562,
"14:22:40.992514",    170,
"15:24:07.258579",    812,
"15:24:07.258579",    749,
"15:24:07.258579",    163,
"15:44:39.228418",     71) %>% 
  
  mutate(time = lubridate::hms(time)) %>% 
  # ggplot(aes(x = as_factor(as.character(time)), y = bumps)) +
  ggplot(aes(x = time, y = bumps)) +
  geom_col()

關於如何讓這個 plot 為我工作的任何想法?

提前致謝

使用 scale_x_time

library(tidyverse)  

df <- tribble(~time,    ~bumps,
        "10:20:07.818923",     97,
        "10:22:43.303376",    250,
        "10:31:09.627506",    300,
        "10:31:09.628242",     58,
        "10:34:05.230866",    100,
        "10:52:57.201534",     89,
        "10:52:57.249029",     11,
        "11:30:31.223439",    100,
        "11:30:32.380126",    100,
        "11:47:58.033180",    430,
        "11:47:58.242451",     86,
        "12:05:39.926110",      1,
        "12:14:23.244696",     28,
        "12:14:23.392286",      5,
        "14:09:59.180788",   1562,
        "14:22:40.992514",    170,
        "15:24:07.258579",    812,
        "15:24:07.258579",    749,
        "15:24:07.258579",    163,
        "15:44:39.228418",     71) %>%
mutate(time = lubridate::hms(time)) 

ggplot(df, aes(x = time, y = bumps)) +
  geom_col() +
    geom_point() +
    scale_x_time()

代表 package (v1.0.0) 於 2021 年 3 月 9 日創建

除了 tjebos 解決方案,這里還有一個簡單的條形圖:

library(tidyverse)

df <- tribble(~time,    ~bumps,
        "10:20:07.818923",     97,
        "10:22:43.303376",    250,
        "10:31:09.627506",    300,
        "10:31:09.628242",     58,
        "10:34:05.230866",    100,
        "10:52:57.201534",     89,
        "10:52:57.249029",     11,
        "11:30:31.223439",    100,
        "11:30:32.380126",    100,
        "11:47:58.033180",    430,
        "11:47:58.242451",     86,
        "12:05:39.926110",      1,
        "12:14:23.244696",     28,
        "12:14:23.392286",      5,
        "14:09:59.180788",   1562,
        "14:22:40.992514",    170,
        "15:24:07.258579",    812,
        "15:24:07.258579",    749,
        "15:24:07.258579",    163,
        "15:44:39.228418",     71) %>% 
  mutate(time_x = as.factor(time)) 
  
  # ggplot(aes(x = as_factor(as.character(time)), y = bumps)) +
ggplot(data= df, aes(x = time_x, y = bumps)) +
  geom_bar(stat="identity") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

geom_col

ggplot() + geom_col(data = df, aes(x = time_x, y = bumps)) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

在此處輸入圖像描述

暫無
暫無

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

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