Legende in 5 er Schritten

Wie erstelle ich Grafiken, was ist zu beachten?

Moderatoren: EDi, jogo

Antworten
philoo7
Beiträge: 9
Registriert: Mo Nov 09, 2020 9:55 am

Legende in 5 er Schritten

Beitrag von philoo7 »

Hallo, habe einen Graphen, wo die Y-Achse in 10 er Schritten dargestellt ist, ich diese aber in 5 er Schritten von -20 bis 20 darstellen möchte. Leider haben weder Bücher, Videos noch Beiträge mir weitergeholfen. Ich denke es ist ganz einfach. Vielen Dank schon mal im Voraus. :)
Lg Phil

Auzug aus meinem Script:

Code: Alles auswählen

### plot daily values
r <- as.POSIXct(round(range(daily.val$days), "days"))
plot(t_dmean ~ days, data=daily.val, 
     type='b', pch=4,col='blue', lwd=1.5,cex=0.65, ylim=c(-20,20),
     main="Wetterstation1 2014-2015",ylab='Mean monthly air temperature [°C]',xlab='',xaxt='n')

axis.POSIXct(1,daily.val$days,at = seq(r[1], r[2], by = "months"), # possibilities: day, days, weeks, months, quarter
             format="%d/%m/%Y",las=2,cex.axis=0.95)
#axis.POSIXct(1,daily.val$days,at = c('2019-11-13','2019-12-01'), # possibilities: day, days, weeks, months, quarter
            
axis.POSIXct(1,daily.val$days,at = c('2014-10-01','2015-07-31'), # possibilities: day, days, weeks, months, quarter
             format="%d/%m/%Y",las=2,cex.axis=0.95) 
# SD borders
lines(t.p.sd~daily.val$days,col='red',lty=1,lwd=0.3)
lines(t.m.sd~daily.val$days,col='red',lty=1,lwd=0.3)

# SD polygon
polygon(
  x = c(daily.val$days, rev(daily.val$days)),
  y = c(t.p.sd, rev(t.m.sd)),
  col = "#cc000033",
  border=NA
)
Zuletzt geändert von jogo am Mo Nov 09, 2020 12:34 pm, insgesamt 1-mal geändert.
Grund: Formatierung verbessert. http://forum.r-statistik.de/viewtopic.php?f=20&t=29
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: Legende in 5 er Schritten

Beitrag von bigben »

Ohne Daten konnte ich das nicht ausprobieren, aber probier mal sowas:

Code: Alles auswählen

axis(2, at = seq(-20, 20, 5))
LG,
Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
philoo7
Beiträge: 9
Registriert: Mo Nov 09, 2020 9:55 am

Re: Legende in 5 er Schritten

Beitrag von philoo7 »

Hallo Bernhard,
das hat leider nicht funktioniert. Trotzdem danke. Die Daten sind zu groß für den Anhang und in der Uni kann ich keine Daten hochladen. Versuche es später mal zu Hause.
Lg Phil
philoo7
Beiträge: 9
Registriert: Mo Nov 09, 2020 9:55 am

Re: Legende in 5 er Schritten

Beitrag von philoo7 »

Hallo,
nun hab ich wenigstens schon mal Striche alle 5 Werte. Bin auf dem richtigen Weg denke ich.
Dateianhänge
Rplot_tesut.png
Rplot_tesut.png (10.15 KiB) 790 mal betrachtet
philoo7
Beiträge: 9
Registriert: Mo Nov 09, 2020 9:55 am

Re: Legende in 5 er Schritten

Beitrag von philoo7 »

philoo7
Beiträge: 9
Registriert: Mo Nov 09, 2020 9:55 am

Re: Legende in 5 er Schritten

Beitrag von philoo7 »

hier das komplette Script. Von meinem Dozenten, angepasst von mir auf meine Daten. ###################################

Code: Alles auswählen

##### HOBO Datenlogger        #####
##### Rudi Sailer, 2019-09-11 #####
###################################

##### HOBO data export as CSV ##### 
# A) File > Export Table Data
# ---> ONLY Temperature > UNSELECT 'Coupler attached' to 'End Of File' when export data
# B) File > Export Table Data > PREFERENCES ('Wheel')
# ---> EXclude Line numbers
# ---> Column Seperator > Tab
# ---> Date and Time > in ONE column




####Read Data 
getwd()
setwd("A:/Uni/Schneegleiten_Masterarbeit/Wetterdaten_Zorin/Weather stations")
getwd()
library (readxl) 



#wtable= Wetterstation1_2014_2015

wtable <-read_excel("Wetterstation1_2014_2015_Lufttemp.xlsx",na="NA")



colnames(wtable) <- c('Date','T')



##### AGGREGATE VALUES to >>> DAILY <<< VALUES #####

# ---> solution with package lubridate >> works for different time formats without
#                                         manipulation as shown above
library(lubridate) # for character to datum conversion
tmp.Date <- ymd_hms(wtable$Date)
tmp.Date <- dmy_hms(wtable$Date)
wtable$Date <- tmp.Date
#print(wtable)

### SUBSET --> to start axis label with specific date
#wtable <- subset(wtable, Date >= ("2014-10-01") & Date < ("2014-07-31"))

### calc MEAN and SD
# create a vector which containes only days or hours out of e.g. 10' minutes
days <- cut(wtable$Date,breaks='months') # 'days' 'hours' 'weeks' 'months' 'quarters'
days <- as.POSIXct(days)



# create data frame with days and t (Lufttemperatur)
data.days <- data.frame(days=days, date_time=wtable$Date, t=as.numeric(wtable$T))

data.days <- data.frame(days=days, t=as.numeric(wtable$T))
## MEAN / SD ---> with AGGREGATE
data.days.mean <- aggregate(as.numeric(data.days$t), 
                            by=list(data.days$days),FUN=mean, na.rm=TRUE)
data.days.sd <- aggregate(as.numeric(data.days$t), 
                          by=list(data.days$days),FUN=sd, na.rm=TRUE)
# combine date and calculated values
daily.val <- cbind(data.days.mean,data.days.sd$x)
colnames(daily.val) <- c('days','t_dmean','t_dsd')



# ## MEAN / SD ---> alternative with PLYR package
# # Function for calc SD and mean >> use in ddply below
# # calc SD and mean for t with function getcalc (package plyr, '.' to allow object names without quoting)
# library(plyr)
# getcalc  <- function(Df) c(t_dmean = mean(Df$t,na.rm=T),    # MEAN for t (air temperature)
#                            t_dsd = sd(Df$t,na.rm=T))
# daily.val <- ddply(data.days, .(days), getcalc)

### create vector +/- SD
#   mean +/- SD
t.p.sd <- daily.val$t_dmean + daily.val$t_dsd
t.m.sd <- daily.val$t_dmean - daily.val$t_dsd

### plot daily values

axis(2, at = seq(-20, 20, 5))
r <- as.POSIXct(round(range(daily.val$days), "days"))
plot(t_dmean ~ days, data=daily.val,
     type='b', pch=4,col='blue', lwd=1.5,cex=0.65, ylim=c(-20,20),
     main="Wetterstation1 2014-2015",ylab='Mean monthly air temperature [°C]',xlab='',xaxt='n')

axis.POSIXct(1,daily.val$days,at = seq(r[1], r[2], by = "months"), # possibilities: day, days, weeks, months, quarter
             format="%d/%m/%Y",las=2,cex.axis=0.95)
#axis.POSIXct(1,daily.val$days,at = c('2019-11-13','2019-12-01'), # possibilities: day, days, weeks, months, quarter
            
axis.POSIXct(1,daily.val$days,at = c('2014-10-01','2015-07-31'), # possibilities: day, days, weeks, months, quarter
             format="%d/%m/%Y",las=2,cex.axis=0.95) 
# SD borders
lines(t.p.sd~daily.val$days,col='red',lty=1,lwd=0.3)
lines(t.m.sd~daily.val$days,col='red',lty=1,lwd=0.3)

# SD polygon
polygon(
  x = c(daily.val$days, rev(daily.val$days)),
  y = c(t.p.sd, rev(t.m.sd)),
  col = "#cc000033",
  border=NA
)

abline(0,0)
#abline(-3,0,lty=3)
#axis(2,at=-3,labels=('-3'))


legend('topleft', c('Mean monthly air temperature [°C]','Mean monthly air temperature +/- SD [°C]'), 
       col=c('blue','#cc000033'),
       lty=c(1,1),lwd=c(1.5,6.5),bty='n', pch=c(4,NA),
       
)

write.table(daily.val,w.f,quote=F, sep='\t', col.names = T,row.names = F)
Zuletzt geändert von jogo am Mo Nov 09, 2020 12:34 pm, insgesamt 1-mal geändert.
Grund: Formatierung verbessert. http://forum.r-statistik.de/viewtopic.php?f=20&t=29
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: Legende in 5 er Schritten

Beitrag von bigben »

Hallo philoo,
philoo7 hat geschrieben: Mo Nov 09, 2020 11:08 amnun hab ich wenigstens schon mal Striche alle 5 Werte. Bin auf dem richtigen Weg denke ich.
Kommen diese Striche denn jetzt von dem axis-Befehl (der genau diese 5 Striche zeichnen sollte) oder sind sie einfach entstanden, als Du das Fenster mit der Grafik etwas größer gezogen hast oder gibt es noch ein Drittes?
JEdenfalls finde ich, dass Du das Grafikfenster etwas größer machen solltest, damit da überhaupt Platz für weitere Zahlen an der y-Achse entsteht.

LG,
Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
philoo7
Beiträge: 9
Registriert: Mo Nov 09, 2020 9:55 am

Re: Legende in 5 er Schritten

Beitrag von philoo7 »

Hallo Bernhard, das ist eine gute Idee. Versuche ich gleich mal. LG Phil
Antworten