微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用xyplot使一年中的几个月成为我的x轴

如何解决如何使用xyplot使一年中的几个月成为我的x轴

这是我的数据 [![在此处输入图像描述][1]][1] 我的代码

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,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,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

输出

enter image description here

我想使用日期作为我的 x 轴,日期涵盖 24 个月。它从 01-11-17 开始,到 01-10-19 结束。任何人都可以帮忙。

解决方法

不使用您的数据很难提供答案。您需要以@r2evans 上面所说的可用格式提供数据。但是,您可以将显示为字符串的 Date 行转换为 Date 类型并将其用作 X 轴。您可以通过在比例列表中添加格式来格式化日期的显示方式。 例如,在您的情况下:

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

或您需要的任何格式。

,

这是您完成任务的一种方式:

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

,

我使用这组指令得到了解决方案: #From Painless way to install a new version of R?

在旧版本的 R 中运行(或通过 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)

然后所有的错误消息都消失了,丢失的包 tidyverseggplot2 又回来了,我有我想要的 x 轴的图

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。