简体   繁体   中英

How to use geom_col with time as x-axis

I'm trying to build a simple bar plot, time in hms as x-axis and a value in y-axis. Using geom_col does not work, consider the following example:

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

Any ideas on how to make this plot work for me?

Thanks in advance

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

Created on 2021-03-09 by the reprex package (v1.0.0)

Additional to tjebos solution here a simple barchart:

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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