user243550
user243550

Reputation: 123

Setting 24-hour scale on x-axis

I have a plot in R-studio seven curves each curve represents a single day. As in the x-axis, there are hours with an interval of five. I have that to the same in such a pattern that they are a one-hour interval instead of five. the code I use for the plot is plot(fts[1:7],ann=TRUE, cex=2,ylim=c(30,150),ylab=" Prices(MWh)",xlab="Hours"). please help

Upvotes: 0

Views: 871

Answers (1)

Rfanatic
Rfanatic

Reputation: 2280

Below two examples

Sample data:

a=c(20,21,1,6,7,9,10,18,35,60,125,153,178,181,236,217,172,317,526,553,657,383,171,50)

Sample code:

plot(0:23, a, type='b', ylab=" Prices(MWh)",xlab="Hours", cex=2)
axis(side=1, at=c(0:23))
axis(side=2, at=seq(0, 600, by=100))
box()

Output: enter image description here

Another possible solution with ggpplot() using Gridwatch data

Shaping the data:

df$time <- as.POSIXct(df$time)
df$times<- format(df$time, "%H:%M")
df$day<-factor(df$day, levels=c("Monday", "Tuesday"))

Creating the plot:

library(ggplot2)

ggplot(df, aes(x=times, y=value, group=day)) + 
  geom_line(aes(color=day), lwd = 1) + 
  geom_point(data = df[df$times %in% c("08:00", "12:00", "20:00"), ], size=3, aes(color=day))+
  scale_color_manual(values = c("red","blue"))+
  labs(x="Time (hours)", y="Demand (GW)", title="", color="Day")+
  theme_bw()+ 
  theme(axis.text.x = element_text(angle = 90, hjust = 1,family="Times", face="bold", size=12, color="black"), 
        axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
        axis.text.y = element_text(family="Times", face="bold", size=12, color="black"),
        axis.title.y = element_text(family="Times", face="bold", size=16, color="black"),
        strip.text = element_text(size=18, face="bold"),
        plot.title = element_text(size=20, face="bold"))+
  theme(legend.position = "none")+
  theme(legend.title= element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.position="right",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5))+
  theme(legend.background = element_rect(colour = 'black', fill = 'white', linetype='solid'))+
  theme(panel.spacing = unit(0, "lines"))+
  scale_x_discrete(breaks = c( "00:00","01:00", "02:00","03:00","04:00","05:00","06:00","07:00","08:00", 
                               "09:00","10:00","11:00","12:00","13:00","14:00" ,"15:00","16:00", 
                               "17:00","18:00","19:00","20:00","21:00","22:00","23:00"), expand = c(0,0))+   
  scale_y_continuous(limits= c(180,450))

Outcome:

enter image description here

Sample data (reduced):

df<-structure(list(`Week beginning` = c("28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", "28/02/2022", 
"28/02/2022", "28/02/2022", "28/02/2022"), time = structure(c(34, 
334, 634, 934, 1234, 1535, 1834, 2134, 2434, 2734, 3034, 3334, 
3634, 3935, 4234, 4534, 4834, 5135, 5434, 5735, 6034, 6335, 6634, 
6935, 7235, 7534, 7834, 8134, 8434, 8734, 9034, 9334, 9636, 9934, 
10235, 10534, 10834, 11136, 11434, 11734, 12034, 12334, 12635, 
12934, 13236, 13534, 13835, 14134, 14435, 14735, 15035, 15334, 
15635, 15934, 16234, 16535, 16834, 17134, 17435, 17734, 18035, 
18334, 18635, 18935, 19234, 19535, 19834, 20134, 20435, 20734, 
21035, 21335, 21635, 21935, 22235, 22534, 22834, 23134, 23434, 
23735, 24036, 24334, 24635, 24934, 25235, 25535, 25834, 26134, 
26434, 26735, 27034, 27335, 27635, 27935, 28234, 28534, 28835, 
29134, 29434, 29734), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    value = c(274.95, 258.03, 259.55, 257.54, 257.54, 254.99, 
    253.8, 253.55, 257.38, 258.22, 259.7, 261.01, 263.36, 263.85, 
    259.11, 264.32, 263.12, 262.17, 260.72, 253.55, 263.36, 258.64, 
    264.32, 256.24, 255.81, 254.2, 256.24, 252.59, 251.36, 251.36, 
    250.54, 249.75, 249.63, 248.87, 248.35, 247.59, 247.12, 245.69, 
    243.27, 242.52, 242.31, 242.91, 241.65, 240.45, 240.84, 240.52, 
    240.53, 240.28, 240.59, 240.52, 240.37, 240.66, 241.02, 241.09, 
    241.35, 241.77, 241.71, 242, 242.35, 242.7, 242.94, 243.75, 
    244.58, 245.77, 246.59, 247.14, 248.14, 248.68, 249.17, 251.56, 
    255.02, 258.37, 262.32, 266.3, 274.07, 282.82, 287.03, 291.77, 
    297.52, 300.87, 305.98, 311.04, 315.93, 319.67, 324.16, 327.46, 
    332.8, 339.59, 345.85, 348.41, 350.85, 351.75, 354.62, 357.57, 
    360.39, 363.65, 365.19, 366.6, 368.77, 366.5), day = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Monday", 
    "Tuesday"), class = "factor"), times = c("00:00", "00:05", 
    "00:10", "00:15", "00:20", "00:25", "00:30", "00:35", "00:40", 
    "00:45", "00:50", "00:55", "01:00", "01:05", "01:10", "01:15", 
    "01:20", "01:25", "01:30", "01:35", "01:40", "01:45", "01:50", 
    "01:55", "02:00", "02:05", "02:10", "02:15", "02:20", "02:25", 
    "02:30", "02:35", "02:40", "02:45", "02:50", "02:55", "03:00", 
    "03:05", "03:10", "03:15", "03:20", "03:25", "03:30", "03:35", 
    "03:40", "03:45", "03:50", "03:55", "04:00", "04:05", "04:10", 
    "04:15", "04:20", "04:25", "04:30", "04:35", "04:40", "04:45", 
    "04:50", "04:55", "05:00", "05:05", "05:10", "05:15", "05:20", 
    "05:25", "05:30", "05:35", "05:40", "05:45", "05:50", "05:55", 
    "06:00", "06:05", "06:10", "06:15", "06:20", "06:25", "06:30", 
    "06:35", "06:40", "06:45", "06:50", "06:55", "07:00", "07:05", 
    "07:10", "07:15", "07:20", "07:25", "07:30", "07:35", "07:40", 
    "07:45", "07:50", "07:55", "08:00", "08:05", "08:10", "08:15"
    )), row.names = c(NA, 100L), class = "data.frame")

Upvotes: 1

Related Questions