0

I have a set of data which is measured waterflow of wells over a period of years with infrequent dates of measurements.

I made a code which nearly does what i want, but the spacing on the x-axis should correlate to the infrequent dates of measurement. I already googled for a while, may be i am not able to find a given answer due to my lack of experience with R, but i hope someone can point me in the right direction.

Thanks in advance for any given help!

Here is an image of the result i get with my code

The result of my code With even spacing on the x-axis

edit: here a minimalised full working example of the code:

#Einlesen der Daten-----
dat<- read.table("min.txt", header=TRUE, fill=TRUE, sep="\t")


#Checken der Librarys-----
#install.packages("ggplot2") # Installiert das pakent ggplot2 wenn nicht vorhanden
#install.packages("tidyverse")
library(ggplot2) # lädt paket ggplot
library(tidyverse)

#Zuweisen genereller Varialben - Projektdetails
var_gz<-dat$Projektdaten[1]
var_ag<-dat$Projektdaten[2]
var_ort<-dat$Projektdaten[3]

#Zuweisen der Variablen aus den Daten-----
Messdatum<-dat$Messdatum
Nr.1a<-dat$WF.1a


#Plotten der Grafiken

# Ausgaben
plot.width <- 16
plot.height <- 8


#Nr.1a -----
plottitel1="WF 1a"
#plottitel2="xxxxxx"
plotdata1 = Nr.1a
#plotdata2 = xxx
einheit = "l/s"
titel<-paste(var_ort,"/ ",plottitel1)
#----------
ggplot(dat, aes(Messdatum)) +                                   #startet plot, Datum auf xAchse
  geom_line(aes(y = plotdata1, colour = plottitel1, group=1)) +                     #fügt Datenreihe als Linie hinzu
  geom_point(aes(y = plotdata1, colour = plottitel1, group=1)) +                        #fügt Datenreihe als Punkte hinzu
  #----Beschriftungselemente----
xlab("Messdatum")+  #X Achses Beschriften
  ylab(paste("Messwert [",einheit,"]"))+    #Y Achses Beschriften
ggtitle(titel)              #Titel Beschriften
last_plot() + scale_colour_discrete(name="Legende")                             # ändert Legendentitel
last_plot() + theme(axis.text.x = element_text(size = 10, angle=90, hjust=1))           # dreht Beschriftung xAchse Vertikal
#last_plot() + scale_colour_manual(values=c(2))
last_plot() + theme(legend.position="bottom")                                 # setzte Legende unter Grafik
#Plot Speichern-----
ggsave(filename = paste(var_gz,"_",var_ort,"_",plottitel1,".png"), plot = last_plot(), width = plot.width, height = plot.height, units="cm")            #speichert den plot

the inputfile is a .txt named "min.txt" with the following example data:

Projektdaten    Messdatum   WF 1a                                                                                                                                               
proj nr 2015-10-21  1.000                                                                                                                                               
client  2015-10-22  2.000                                                                                                                                               
location    2015-10-23  3.000                                                                                                                                               
    2015-11-30  4.000                                                                                                                                               
    2015-12-30  5.000                                                                                                                                               
    2016-06-15  6.000                                                                                                                                               
    2016-08-25  7.000                                                                                                                                               
Jürgen L
  • 1
  • 1
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Jul 12 '22 at 16:27
  • 1
    This said: From your image I would guess that `Messdatum` is a character. Hence, try with converting to a proper date using `as.Date(Messdatum)`. – stefan Jul 12 '22 at 16:28
  • Hey, stefan, thx for the hint with the code example, i have now added one + fake data. changing Messdatum to as.Date(Messdatum) did not change anything. – Jürgen L Jul 12 '22 at 18:47
  • Run `dat$Messdatum <- as.Date(dat$Messdatum)` and then try the plot code again. – Gregor Thomas Jul 12 '22 at 18:48
  • @ Gregor Thomas, jep, thats it. Thank you and thank you stefan, i guess i just did it wrong the first time. – Jürgen L Jul 12 '22 at 18:50

0 Answers0