1

Here is my data [![enter image description here][1]][1] my code

library(ggplot2)
library(reshape)
dt1 =read.csv("C:/Users/My DELL/Documents/R_data/machine learning/dt1.csv")
head(dt1)
dt1$month <- seq(nrow(dt1))
library(reshape2)
mm <- melt(subset(dt1,select=c(month,EgbeNa,UrejeNa,EroNa,RefNa,EgbeMg,UrejeMg,EroMg,RefMg
)),id.var="month")
head(mm)
library(lattice)
xyplot(value ~ month|variable,data=mm,type="l",
       scales=list(y=list(relation="free")),
       layout=c(1,8))
dt_repr = structure(list(Date = c("01-11-17", "01-12-17", "01-01-18", "01-02-18", 
"01-03-18", "01-04-18", "01-05-18", "01-06-18", "01-07-18", "01-08-18", 
"01-09-18", "01-10-18", "01-11-18", "01-12-18", "01-01-19", "01-02-19", 
"01-03-19", "01-04-19", "01-05-19", "01-06-19", "01-07-19", "01-08-19", 
"01-09-19", "01-10-19"), month = 1:24, EgbeNa = c(27.4, 29.25, 
31.1, 20.4, 13.55, 14, 16.25, 18.5, 24.95, 16.2, 30.15, 28.6, 
35.1, 36.5, 28.45, 31.5, 38.1, 28, 32.55, 30.5, 33.2, 30.8, 13, 
24.3), UrejeNa = c(10.45, 9, 7.55, 13.35, 11.6, 12.475, 20.1625, 
27.85, 21.5, 32.05, 17.65, 15.15, 25.7, 18.8, 26.85, 20.65, 23.5, 
26.45, 30.2, 25.75, 28.3, 31.45, 44.4, 39.6), EroNa = c(44.45, 
40.55, 36.65, 43, 39.825, 36.825, 44.1, 51.65, 44.2, 56.1, 61.3, 
66.05, 15.75, 19.15, 13.05, 12.2, 21.7, 17.9, 14.6, 33.3, 21.2, 
19.6, 32.7, 25.1), RefNa = c(10.55, 9.75, 12.35, 19.65, 10.6, 
13.74, 22.62, 25.82, 20.4, 31.2, 16.95, 14.25, 15.03, 17.15, 
12.75, 13.5, 20.45, 16.8, 15.5, 25.4, 19.5, 19.8, 26.7, 25.1), 
    EgbeMg = c(4.118, 4.7155, 5.313, 4.4865, 5.1535, 5.1295, 
    5.113, 5.103, 5.721, 5.285, 3.8575, 4.128, 5.4205, 6.2975, 
    5.134, 5.4605, 5.124, 4.203, 5.2635, 5.135, 6.092, 5.575, 
    4.139, 4.8645), UrejeMg = c(3.6655, 3.977, 4.288, 4.192, 
    4.676, 4.434, 4.7005, 4.966, 5.3895, 5.7165, 4.881, 4.1015, 
    3.743, 6.132, 6.0785, 6.1775, 6.3135, 6.028, 5.739, 6.126, 
    4.5155, 4.716, 5.2165, 5.678), EroMg = c(2.472, 2.31425, 
    2.1565, 2.2115, 2.184, 2.135, 4.135, 6.2005, 5.457, 5.981, 
    5.784, 5.885, 5.406, 5.248, 4.967, 4.449, 5.058, 5.1675, 
    5.667, 6.966, 5.17, 4.8965, 7.201, 6.538), RefMg = c(3.75, 
    3.87, 4.82, 4.132, 3.98, 4.23, 4.57, 5.01, 5.02, 4.67, 4.18, 
    4.51, 5.21, 5.18, 4.76, 4.29, 4.95, 5.07, 5.45, 5.86, 5.11, 
    4.79, 6.01, 5.24)), class = "data.frame", row.names = c(NA, 
-24L)) #This data is reproducible

and the output enter image description here

I want to use Date as my x-axis, the Date covers 24 months. It starts at 01-11-17 and ends at 01-10-19. Anyone can help please.

Seyi
  • 463
  • 5
  • 11
  • 4
    *"Here is my data"* ... I'm not going to spend time transcribing your data into something usable. Please provide usable data, by pasting the output from `dput(.)`. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 04 '21 at 19:35
  • 1
    Have you tried to replace `month` in all of your code with `Date`? – r2evans Jun 04 '21 at 19:39

3 Answers3

0

It is difficult to provide answers without using your data. You need to provide your data in a usable format as @r2evans says above. However, you can convert your Date row, which appears to be a string, to Date type and use that as your X-axis. You can format how the date should be displayed by adding the format in the scales list. For example, in your case:

...
scales=list(
    y=list(relation="free"), 
    x = list(format = "%m-%Y") # or whatever format you need
),
...

or whatever format you need.

0

Here is one way how you could achieve your task:

library(tidyverse)
library(lubridate)
library(lattice)

df <- dt_repr %>% 
  pivot_longer(
    cols = c(-Date, -month),
    names_to = "names",
    values_to = "values"
  ) %>% 
  mutate(Date = dmy(Date))

xyplot(values ~ Date|names,data=df,type="l",
       scales=list(y=list(relation="free")),
       layout=c(1,8))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Yes it worked but did not for me please. I installed ```tidyverse``` but see the error report: ```Error: package or namespace load failed for ‘tidyverse’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): namespace ‘ellipsis’ 0.3.1 is already loaded, but >= 0.3.2 is required``` – Seyi Jun 07 '21 at 04:13
  • Two ways to solve: First: Restart R session (Rstudio -> Session -> new Session or Restart R). If no success the Second option: 1. `remove.packages("rlang")`. 2. `install.packages("rlang")`. Start `library(tidyverse)`. – TarJae Jun 07 '21 at 05:35
  • Same error message after trying the methods – Seyi Jun 07 '21 at 11:51
  • Could you post your session info – TarJae Jun 07 '21 at 12:25
  • ```Error: package or namespace load failed for ‘tidyverse’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]): namespace ‘vctrs’ 0.3.6 is already loaded, but >= 0.3.8 is required``` – Seyi Jun 07 '21 at 17:43
  • ```Error in dt_repr %>% pivot_longer(cols = c(-Date, -month), names_to = "names", : could not find function "%>%"``` – Seyi Jun 07 '21 at 17:44
  • ```Error in unique.default(x, nmax = nmax) : unique() applies only to vectors``` I believe I will obtain the desire result as you obtained it, just tell me what to do – Seyi Jun 07 '21 at 17:44
  • load: library(dplyr), library(tidyr) both are integrated in library(tidyverse) – TarJae Jun 07 '21 at 17:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233471/discussion-between-seyi-and-tarjae). – Seyi Jun 07 '21 at 21:45
0

I got the solution using this set of instruction: #From Painless way to install a new version of R?

Run in the old version of R (or via RStudio)

setwd("C:/Temp/") 
packages <- installed.packages()[,"Package"] 
save(packages, file="Rpackages") 

# INSTALL NEW R VERSION
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr
# See here for more on installr: https://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/  

# step by step functions:
check.for.updates.R() # tells you if there is a new version of R or not.
install.R() # download and run the latest R installer

# Install library - run in the new version of R. This calls package names and installs them from repos, thus all packages should be correct to the most recent version
setwd("C:/Temp/") 
load("Rpackages") 
for (p in setdiff(packages, installed.packages()[,"Package"])) 
install.packages(p) 

# Installr includes a package migration tool but this simply copies packages, it does not update them
copy.packages.between.libraries() # copy your packages to the newest R installation from the one version before it (if ask=T, it will ask you between which two versions to perform the copying)

Then all the error messages are gone, the missing packages tidyverse and ggplot2 came back and I have my desired plot with expected x axis

Seyi
  • 463
  • 5
  • 11