Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

plot - How to structure dates on x-axis in R

I am a beginner with R. It took me the larger part of a day to not find a solution.

I need a 2D-scatterplot with dates on the x-axis. Here is what I have:

################## CSV file looks like this
#;Datum;Summe;dink;
#;1.1.2010;10;2;
#;1.2.2010;20;3;
#;10.3.2010;40;4;
#;1.4.2010;70;3;
#;15.5.2010;80;5;
#;1.6.2010;100;6;
#;24.7.2010;200;5;
#;1.8.2010;150;7;
#;1.9.2010;130;8;

csvu<-read.csv("2D-plot-Tabelle.csv", sep = ";", encoding="UTF-8")  # from Numbers

# as.Date converts into class Date 
csvu$Datum <- as.Date(csvu$Datum, "%d.%m.%Y") # this is the read format. capital Y = year with century
plot(csvu$Datum, csvu$Summe, 
     main="This is my plot", 
     xlab="Datum", 
     ylab="Summe", 
     type = "b", # both lines and dots
     lwd = 2, # line width
     # col = "darkgreen", # col = symbol surround color
     col = rainbow(25), 
     pch=16,            # pch = symbol interior color, see http://applied-r.com/r-graphics-plot-parameters/#axes
     cex = 2)           # cex = size of plot symbols

plot() apparently applies a default x-axis numbering which gives month values for 5 months and no day or year.

I need x-axis ticks and "numbering" with date in the format "%d.%m.%Y" both in monthly intervalls and according to the date. I prefer vertical writing of dates. Any suggestion?

######## Found the solution myself. Maybe interesting to others:

# add  xaxt="n", as plot-parameter (disable x-axis numbering and ticks), than
axis.Date(1,at=csvu$Datum,labels=format(csvu$Datum,"%d %b %Y"),las=2) # las=2 vertical, 1 horiz.
question from:https://stackoverflow.com/questions/65850343/how-to-structure-dates-on-x-axis-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...