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

How to plot month values on the year scale?

I have the following data: year and values for 12 months.
How to plot the values with respect to time (denoted by years) with the spacing that there will be 12 values between the two years? Thank you

 1847   0.031   0.099  -1.585   1.170   1.763  -0.260   0.746   1.129  -0.324   0.445   2.459   1.760  
 1848  -0.792   1.770   0.757  -1.023   0.691  -1.780   1.867   2.641  -2.546  -2.436  -0.842   2.548  
 1849   2.419   2.767  -0.562  -0.990  -0.517  -3.210   1.203   0.701  -2.234  -0.078   0.802  -1.238  
 1850  -0.163   4.134  -2.216   0.965  -1.157   0.403   0.305   0.148  -2.077  -2.701   2.390   2.358  
 1851   3.293   1.028   1.504  -1.658  -1.534  -1.621  -5.395   4.679   1.852   0.777  -1.769   1.742  
 1852   1.464   0.411  -2.502  -1.597   0.245   0.093  -1.134   2.943  -2.021  -1.646  -0.930   1.029  

Something like this

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

>Solution :

Alternatively, if you wanted to put all values in a single line:

dat <- import("https://quantoid.net/files/test.txt")
names(dat) <- c("year", paste0("month_", 1:12))
dat <- dat %>% 
  pivot_longer(-year, names_pattern="month_(\\d+)", names_to="month", values_to="val") %>% 
  mutate(month = as.numeric(month), 
         yrmo = year + (month - 1)/12)

ggplot(dat, aes(x=yrmo, y=val)) + 
  geom_line() + 
  scale_x_continuous(breaks=1847:1852)

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