Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data resolution adjustment

I would like to make a line plot using half-hourly measurements. My problem is that as the number of instances rises, the plot begins to display spike trends rather than a continuous line trend. How can I change this?

Sample code:

ggplot(df, aes(x=Time, y=Price, group=Band)) + 
  geom_line(aes(color=Band), lwd=1)+
  labs(x="Time (hours)", y="Price", title="")+
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +  
  scale_y_continuous(expand = c(0,0))

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

enter image description here

Sample data for 150 cases above the plot created based on this data;

df<-structure(list(Date = structure(c(1635724800, 1635726600, 1635728400, 
1635730200, 1635732000, 1635733800, 1635735600, 1635737400, 1635739200, 
1635741000, 1635742800, 1635744600, 1635746400, 1635748200, 1635750000, 
1635751800, 1635753600, 1635755400, 1635757200, 1635759000, 1635760800, 
1635762600, 1635764400, 1635766200, 1635768000, 1635769800, 1635771600, 
1635773400, 1635775200, 1635777000, 1635778800, 1635780600, 1635782400, 
1635784200, 1635786000, 1635787800, 1635789600, 1635791400, 1635793200, 
1635795000, 1635796800, 1635798600, 1635800400, 1635802200, 1635804000, 
1635805800, 1635807600, 1635809400, 1635811200, 1635813000, 1635814800, 
1635816600, 1635818400, 1635820200, 1635822000, 1635823800, 1635825600, 
1635827400, 1635829200, 1635831000, 1635832800, 1635834600, 1635836400, 
1635838200, 1635840000, 1635841800, 1635843600, 1635845400, 1635847200, 
1635849000, 1635850800, 1635852600, 1635854400, 1635856200, 1635858000, 
1635859800, 1635861600, 1635863400, 1635865200, 1635867000, 1635868800, 
1635870600, 1635872400, 1635874200, 1635876000, 1635877800, 1635879600, 
1635881400, 1635883200, 1635885000, 1635886800, 1635888600, 1635890400, 
1635892200, 1635894000, 1635895800, 1635897600, 1635899400, 1635901200, 
1635903000, 1635904800, 1635906600, 1635908400, 1635910200, 1635912000, 
1635913800, 1635915600, 1635917400, 1635919200, 1635921000, 1635922800, 
1635924600, 1635926400, 1635928200, 1635930000, 1635931800, 1635933600, 
1635935400, 1635937200, 1635939000, 1635940800, 1635942600, 1635944400, 
1635946200, 1635948000, 1635949800, 1635951600, 1635953400, 1635955200, 
1635957000, 1635958800, 1635960600, 1635962400, 1635964200, 1635966000, 
1635967800, 1635969600, 1635971400, 1635973200, 1635975000, 1635976800, 
1635978600, 1635980400, 1635982200, 1635984000, 1635985800, 1635987600, 
1635989400, 1635991200, 1635993000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Time = structure(c(0, 1800, 3600, 5400, 7200, 9000, 
10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200, 
27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400, 
43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600, 
59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800, 
75600, 77400, 79200, 81000, 82800, 84600, 0, 1800, 3600, 5400, 
7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600, 
23400, 25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800, 
39600, 41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000, 
55800, 57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200, 
72000, 73800, 75600, 77400, 79200, 81000, 82800, 84600, 0, 1800, 
3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800, 
21600, 23400, 25200, 27000, 28800, 30600, 32400, 34200, 36000, 
37800, 39600, 41400, 43200, 45000, 46800, 48600, 50400, 52200, 
54000, 55800, 57600, 59400, 61200, 63000, 64800, 66600, 68400, 
70200, 72000, 73800, 75600, 77400, 79200, 81000, 82800, 84600, 
0, 1800, 3600, 5400, 7200, 9000), class = c("hms", "difftime"
), units = "secs"), Band = c("C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C"
), Region = c("Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", 
"Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma", "Roma"
), Price = c(3.83, 4.91, 2.97, 1.3, 2.25, 1.3, 1.89, 1.3, 2.05, 
2.35, 2.16, 5.2, 9, 9.38, 9.38, 11.84, 13.65, 14.3, 12.42, 11.79, 
11.78, 9.38, 11.37, 12.6, 11.33, 12.13, 13.3, 12.4, 14.37, 14.5, 
11.6, 14.01, 16.64, 22.46, 24.35, 24.97, 25.21, 23.57, 18.68, 
13.84, 16.65, 12.7, 14.22, 9.66, 12.45, 8.41, 12.7, 12.46, 12.56, 
12.7, 12.98, 12.89, 13.08, 12.51, 12.42, 12.42, 13.36, 12.7, 
15.08, 14.6, 15.08, 16.02, 15.87, 19.75, 18.21, 18.78, 18.48, 
17.74, 19.64, 18.32, 18.56, 18.56, 17.74, 19.08, 18.63, 18.93, 
18.63, 18.88, 18.5, 23.09, 25.66, 47.21, 92.64, 97.13, 73.73, 
39.32, 31.46, 20.8, 23.45, 17.59, 16.96, 14.29, 15.21, 15.64, 
15.26, 12.52, 13.18, 14.12, 13.92, 12.32, 13.73, 12.01, 13.84, 
12.42, 13.92, 13.26, 15.01, 13.54, 14.6, 17.16, 18.37, 20.3, 
19.08, 19.08, 19.35, 17.49, 18.77, 17.12, 16.38, 16.12, 15.7, 
15.7, 16.27, 15.08, 14.79, 15.18, 14.6, 18.38, 22.93, 28.6, 26.23, 
26.69, 26.29, 23.38, 17.38, 13.94, 14.6, 13.73, 14.41, 13.08, 
13.84, 10.42, 15.08, 15.08, 15.48, 16.04, 15.67, 12.62, 12.89, 
12.12)), row.names = c(NA, -150L), class = c("tbl_df", "tbl", 
"data.frame"))

>Solution :

You have duplicated times because there are multiple days in your data. Duplicated x-values due to the presence of multiple (unresolved) groups is a typical causefor the spikiness of the trend.

If you include the day in the group, you see separate tracks for every day without the spikiness.

library(ggplot2)
library(lubridate)

# df <- structure(...) # omitted for brevity

ggplot(df, aes(x=Time, y=Price, group= interaction(Band, day(Date)))) + 
  geom_line(aes(color=Band), lwd=1)+
  labs(x="Time (hours)", y="Price", title="")+
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +  
  scale_y_continuous(expand = c(0,0))

Created on 2022-01-19 by the reprex package (v2.0.1)

Alternatively you can use aes(Date, Price, group = Band) instead to visualse the timecourse:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading