-1

I have created this plot, but i want to combine Time and Date together in the x-axis. (note that in my dataset, Time and Date are in separate columns):

Plot

Code:

library(shiny)
library(shinydashboard)
library(ggplot2)
library(scales)

shinyServer(function(input,output){
output$Histogram <- renderPlot({

Energy <- read.csv("Energy.csv")
Energy$Date <- as.POSIXct(Energy$Date)

ggplot(data=Energy, mapping=aes(x=Date, Time, y=kWh)) +
  scale_x_datetime(date_breaks= "1 month") +
  geom_point(aes(color=Time)) + 
  theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))
})

What can i do to make 'Time' and 'Date' in the x-axis together?

camille
  • 16,432
  • 18
  • 38
  • 60
Maryamsi
  • 1
  • 2
  • 3
    Please make this question *reproducible*. This includes sample data (e.g., `dput(head(x))`). Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Oct 09 '18 at 21:59
  • 2
    Lacking that, my first cut would be to combine `Time` and `Date` into `POSIXct`. – r2evans Oct 09 '18 at 22:00
  • 2
    In addition to making the question reproducible, it's also easiest to help when the question is minimal. Setting up a Shiny server isn't necessary for helping you debug this plot, so it doesn't need to be in the question. It also appears you're missing a closing bracket, but that only matters if you don't edit out the Shiny stuff – camille Oct 09 '18 at 22:08

1 Answers1

0

As @r2evans mentioned, you should combine Time and Date, e.g.

library(dplyr)
Energy <- Energy %>%
  mutate(DateTime = lubridate::ymd_hm(paste(Date, Time))

and then use DateTime as your x axis.

Jon Spring
  • 55,165
  • 4
  • 35
  • 53