Home > Blockchain >  How to plot month values on the year scale?
How to plot month values on the year scale?

Time:12-09

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

CodePudding user response:

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

CodePudding user response:

I have a solution using the zoo package for neat plotted dates and the highcharter package to generate the chart, as well as the tidyverse to sort data.

library(zoo)
library(highcharter)
library(tidyverse)

I emulated your dataset as follows:

df <- data.frame(year = c(rep(1847,12),rep(1848,12),rep(1849,12),rep(1850,12),rep(1851,12),rep(1852,12)),
           month = c(rep(seq(01,12, by = 01),6)),
           values = c(runif(72, min = -2, max = 2)       
)) %>%
  unite("date",c(year,month),sep = "-") %>%
  mutate(date = as.yearmon(date)
) %>%
  mutate(color = ifelse(values >= 0, "above", "below"))

Which yields something like this:

head(df)
      date     values color
1 Jan 1847  0.7233567 above
2 Feb 1847  0.4621962 above
3 Mar 1847 -0.2388412 below
4 Apr 1847 -0.3818243 below
5 May 1847 -1.5017872 below
6 Jun 1847 -1.8706521 below

I created a variable that indicates whether the value is above or below 0. I then plot using highcharter's column plot function:

colors <- c("slateblue","firebrick")
df %>%
hchart("column", hcaes(x = date, y = values, group = color)) %>%
  hc_title(text = "My Chart Title",
           align =  "center",
           style = list(
             fontSize = '2em',
             color = "#000000"
           ))  %>% 
  hc_legend(enabled = FALSE) %>%
  hc_colors(colors)

Which yields:

enter image description here

CodePudding user response:

A ggplot() version with facet_wrap()

Sample data:

  df<-read.table(header=T,text=
    "Year   January February March April    May     June    July    August  October September November December
     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")

Sample code:

 library(lubridate)
 library(ggplot)

df_melt<-melt(df, id.var="Year")
df_melt$Year<- as.factor(df_melt$Year)


ggplot(df_melt, aes(x=value, y=variable, group=Year))    
geom_col(aes(fill=value)) theme_bw()  
  facet_wrap(~as.factor(Year)) 
  scale_fill_gradient(low="lightyellow", high="red")   
  labs(x="", y="", title="", fill="r")   
  theme_bw() 
  theme(plot.title = element_text(hjust = 0.5,  face="bold", size=20, color="black"))   
  theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black")) 
  theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black")) 
  theme(axis.text.x = element_text( hjust = 1,  face="bold", size=14, color="black") ) 
  theme(axis.text.y = element_text( hjust = 1,  face="bold", size=14, color="black") ) 
  theme(plot.title = element_text(hjust = 0.5)) 
  theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
        legend.position="right",
        plot.title = element_text(hjust = 0.5)) 
theme(strip.text.x = element_text(size = 16, colour = "black",family="Times", face="bold"))

enter image description here

CodePudding user response:

You need to tidy your data and give your columns names in order to identify them later. We can do so using reshape2:

names(df) <- c("Year", month.name)
df <- melt(df, id.var = "Year", variable.name = "Month")

With the data formatted like this we just need to use the correct aes in ggplot:

ggplot(df, aes(x = Month, y = value, group = Year, color = factor(Year)))  
  geom_line()   geom_point()

CodePudding user response:

df %>% 
  pivot_longer(
    -year
  ) %>% 
  ggplot(aes(x = year, y=value, color=factor(name), group=name))  
  geom_point()  
  geom_line()

enter image description here

Something like this?

library(tidyverse)

colnames(df) <- c("year", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

df %>% 
  pivot_longer(
    -year
  ) %>% 
  ggplot(aes(x = name, y=value, color=factor(year), group=year))  
  geom_point()  
  geom_line()

enter image description here

  •  Tags:  
  • r
  • Related