Seite 1 von 2
POSIXct in richtige Länge bringen?
Verfasst: Mo Jun 11, 2018 6:14 pm
von Olafson Larson
Hallo,
Wie kann ich ein POSIXct Objekt in die richtige Länge bringen? D.h. Start von Datum x und Ende bei Datum x?
Code: Alles auswählen
datum <- as.POSIXct(datum, format="%Y-%m-%d %H:%M:%S", tz='UTC')
Ich möchte es anschliessend mit ggplot plotten.
Re: POSIXct in richtige Länge bringen?
Verfasst: Mo Jun 11, 2018 6:55 pm
von EDi
Was ist 'datum' in deinem Code?
Reichen dir die Operatoren >, <, <=, >= aus? Vielleicht brauchst du noch ein & oder ein |.
Was soll geplottet werden? Ggplot will data.frames, keine Vektoren...
Re: POSIXct in richtige Länge bringen?
Verfasst: Di Jun 12, 2018 11:12 am
von Olafson Larson
datum ist in meinem Code eine Variable (POSIXct) die ein Datum sowie eine Uhrzeit beinhaltet.
Wie geht das mit den Operatoren? Wenn ich das probiere
wir in die Variable
geschrieben.
Geplottet werden soll eine Variable (num) auf der y-Achse und die Variable datum auf der x-Achse.
ggplot will sicher data.frames aber es funktioniert bei mir auch tadellos mit POSIXct Variablen
Re: POSIXct in richtige Länge bringen?
Verfasst: Di Jun 12, 2018 2:14 pm
von jogo
Hallo Olafson,
leider verstehe ich immer noch nicht, welche Struktur Deine Variable
datum hat. Kannst Du bitte das Ergebnis von
in Deine nächste Nachricht kopieren?
Danke, Jörg
Re: POSIXct in richtige Länge bringen?
Verfasst: Di Jun 12, 2018 3:39 pm
von Olafson Larson
Oh, sorry....
Das ist die Struktur der Variable datum:
Code: Alles auswählen
> str(datum)
POSIXct[1:5833], format: "2018-05-30 07:33:37" "2018-05-30 07:39:37" "2018-05-30 07:42:38" "2018-05-30 07:45:38" "2018-05-30 07:48:39" ...
Re: POSIXct in richtige Länge bringen?
Verfasst: Mi Jun 13, 2018 9:16 am
von jogo
Olafson Larson hat geschrieben: ↑Di Jun 12, 2018 3:39 pm
Oh, sorry....
Das ist die Struktur der Variable datum:
Code: Alles auswählen
> str(datum)
POSIXct[1:5833], format: "2018-05-30 07:33:37" "2018-05-30 07:39:37" "2018-05-30 07:42:38" "2018-05-30 07:45:38" "2018-05-30 07:48:39" ...
prima, es ist also wirklich ein Vektor mit POSIXct-Elementen. Bisher war ich mir da nicht sicher, ob es nicht auch ein Vektor von
character hätte sein können.
Jetzt habe ich nur noch die Frage, was meinst Du mit
in die richtige Länge bringen Willst Du den Vektor verkürzen oder verlängern? Oder möchtest Du die Uhrzeit oder das Datum entfernen? Kannst Du bitte auch zeigen, wie der Code bis zum Aufruf von ggplot aussieht und evtl. was daran bis jetzt nicht funktioniert.
Gruß, Jörg
Re: POSIXct in richtige Länge bringen?
Verfasst: Mi Jun 13, 2018 11:23 am
von Olafson Larson
Danke Jörg für die Antwort.
Ich möchte 2 plots in "einer" Grafik erstellen. Beide plots sollten ab dem selben Datum beginnen: 31.05.2018
Der eine plot beginnt aber bereits ab dem 30.05, daher möchte ich den POSIXct einkürzen, also dass er erst ab dem 31.05 beginnt.
Code: Alles auswählen
#fog sensor comparison, is not working! or lets say it is working, but no adaption of x-axis was possible
library(reshape2)
library(ggplot2)
library(readbulk)
library(scales)
library(ggpubr)
#visvais
visvais_data <- read_bulk(directory = "/Users/olaf/Desktop/R/visvais", verbose = TRUE, fun = utils::read.table)
datum <- as.POSIXct(paste(visvais_data$V1, visvais_data$V2), format="%Y-%m-%d %H:%M:%S")
#ifdews teststation
lora <- read.csv("/Users/olaf/Desktop/R/IfDews_LoRa_Test(5) copy.csv", header = TRUE, sep=";")
lora$vis <- as.numeric(lora$vis)
dat <- sub("T"," ",lora$time)
dat <- strtrim(dat, 19)
dat <- as.POSIXct(dat, format="%Y-%m-%d %H:%M:%S", tz='UTC')
#visvais plot
plot1 <- ggplot(data=visvais_data) +
geom_point(mapping=aes(x=datum,y=V5), color="blue") +
coord_cartesian(ylim = c(0, 2000)) +
geom_hline(yintercept=1000, linetype="dashed")+
scale_x_datetime( breaks = date_breaks("1 day"),
labels = date_format("%d-%m %H:%S", tz="UTC"),
expand = c(0,0))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#lora plot
plot2 <- ggplot(data=lora,aes(x=dat,y=vis)) +
geom_point(color="blue") +
coord_cartesian(ylim = c(0, 2000)) +
geom_hline(yintercept=1000, linetype="dashed")+
scale_x_datetime( breaks = date_breaks("1 day"),
labels = date_format("%d-%m %H:%S", tz="UTC"),
expand = c(0,0))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#plot multiple plots together, requires library ggpubr
ggarrange(plot1,plot2,ncol=1,nrow=2)
Re: POSIXct in richtige Länge bringen?
Verfasst: Mi Jun 13, 2018 11:45 am
von jogo
Hallo Olafson,
Olafson Larson hat geschrieben: ↑Mi Jun 13, 2018 11:23 am
Ich möchte 2 plots in "einer" Grafik erstellen. Beide plots sollten ab dem selben Datum beginnen: 31.05.2018
Der eine plot beginnt aber bereits ab dem 30.05, daher möchte ich den POSIXct einkürzen, also dass er erst ab dem 31.05 beginnt.
das läuft auf ein
subset() auf den entsprechenden Dataframes hinaus.
Dafür muss man aber die Daten in den Dataframes zusammenhalten.
Code: Alles auswählen
#visvais
visvais_data <- read_bulk(directory = "/Users/olaf/Desktop/R/visvais", verbose = TRUE, fun = utils::read.table)
datum <- as.POSIXct(paste(visvais_data$V1, visvais_data$V2), format="%Y-%m-%d %H:%M:%S")
... hier trennst Du gerade
datum von den anderen zugehörigen Daten
Besser ist:
Code: Alles auswählen
visvais_data$datum <- as.POSIXct(paste(visvais_data$V1, visvais_data$V2), format="%Y-%m-%d %H:%M:%S")
und
Code: Alles auswählen
#ifdews teststation
lora <- read.csv("/Users/olaf/Desktop/R/IfDews_LoRa_Test(5) copy.csv", header = TRUE, sep=";")
lora$vis <- as.numeric(lora$vis)
lora$dat <- sub("T"," ",lora$time)
lora$dat <- strtrim(dat, 19)
lora$dat <- as.POSIXct(dat, format="%Y-%m-%d %H:%M:%S", tz='UTC')
Wie kann es weiter gehen? Vielleicht so:
Code: Alles auswählen
T_null <- max(min(visvais_data$datum), min(lora$dat))
# und beim ggplot() dann:
ggplot(data=visvais_data[visvais_data$datum>=T_null], ...)
# ...
ggplot(data=lora[lora$dat>=T_null], ...)
... oder gibt es bei
ggplot() einen Parameter für das subsetting?
Nachtrag: ja, es gibt sowas: z.B.
https://stackoverflow.com/a/18165730/5414452
Gruß, Jörg
Re: POSIXct in richtige Länge bringen?
Verfasst: Do Jun 14, 2018 12:16 pm
von Olafson Larson
Danke für die Antwort.
Diese Zeile:
liefert mir folgenden Fehler:
Code: Alles auswählen
> T_null <- max(min(visvais_data$datum), min(lora$dat))
Warning message:
In min(lora$dat) : no non-missing arguments to min; returning Inf
Re: POSIXct in richtige Länge bringen?
Verfasst: Do Jun 14, 2018 12:55 pm
von Olafson Larson
Ich dachte ich habs jetzt, aber ist zum verzweiflen
Code: Alles auswählen
visvais_data <- read_bulk(directory = "/Users/olaf/Desktop/R/visvais", verbose = TRUE, fun = utils::read.table)
visvais_data$datum <- as.POSIXct(paste(visvais_data$V1, visvais_data$V2), format="%Y-%m-%d %H:%M:%S")
datum_sub <- subset(visvais_data, datum >= as.POSIXct('2018-05-31 00:00'))
plot1 <- ggplot(data = datum_sub, aes(x=datum, y= V5)) +
Die Daten sind in datum_sub eigentlich nun richtig. V5 ist eine int Variable und datum_sub$datum beginnt erst ab dem 31.05.2018. Wenn man das ganze aber nun plottet ändert sich nichts. Das gibt es doch nicht!
Code: Alles auswählen
> str(datum_sub)
'data.frame': 68614 obs. of 15 variables:
$ V1 : Factor w/ 9 levels "2018-05-30","2018-05-31",..: 2 2 2 2 2 2 2 2 2 2 ...
$ V2 : Factor w/ 15881 levels "22:00:22","22:00:32",..: 719 720 721 722 723 724 725 726 727 728 ...
$ V3 : Factor w/ 1 level "\001PW": 1 1 1 1 1 1 1 1 1 1 ...
$ V4 : Factor w/ 1 level "1\00200": 1 1 1 1 1 1 1 1 1 1 ...
$ V5 : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
$ V6 : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
$ V7 : Factor w/ 1 level "///": 1 1 1 1 1 1 1 1 1 1 ...
$ V8 : Factor w/ 1 level "//": 1 1 1 1 1 1 1 1 1 1 ...
$ V9 : Factor w/ 1 level "//": 1 1 1 1 1 1 1 1 1 1 ...
$ V10 : Factor w/ 1 level "//": 1 1 1 1 1 1 1 1 1 1 ...
$ V11 : Factor w/ 1 level "//////": 1 1 1 1 1 1 1 1 1 1 ...
$ V12 : Factor w/ 1 level "//////": 1 1 1 1 1 1 1 1 1 1 ...
$ V13 : Factor w/ 1 level "////\003": 1 1 1 1 1 1 1 1 1 1 ...
$ File : chr "visvais201805310000.dat" "visvais201805310000.dat" "visvais201805310000.dat" "visvais201805310000.dat" ...
$ datum: POSIXct, format: "2018-05-31 00:00:22" "2018-05-31 00:00:32" "2018-05-31 00:00:42" "2018-05-31 00:00:52" ...