如何在R(ggplot2)的1个x轴上绘制2列

时间:2018-10-09 21:54:12

标签: r ggplot2 plot dataset

我已经创建了该图,但是我想在x轴上将TimeDate组合在一起。 (请注意,在我的数据集中,TimeDate在单独的列中):

Plot

代码:

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

我该怎么做才能在x轴上同时显示“时间”和“日期”?

1 个答案:

答案 0 :(得分:0)

如@ r2evans所述,您应该结合时间和日期,例如

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

,然后将DateTime用作x轴。