简体   繁体   中英

Creating bar plot with values corresponding to latest date of multiple time-series in a dataframe in R

I have a dataframe, the first column of which are dates and the rest are values. The structure of the dataframe takes the following form:

Data <- data.frame(Date = c("2017-06-30","2017-06-29","2017-06-28","2017-06-27"), 
                   TS1 = c(1,2,3,4), TS2 = c(5,6,7,8), TS3 =c(9,10,11,12))

Is there a way to make a bar chart with the last date (in this case 2017-06-30 ) on the x-axis along with the corresponding values of TS1 , TS2 and TS3 (or 4,8,12 ) on the y-axis?

Ideally, the solution would be in base-R, but any other way is also fine!

For the last date 2017-06-30 you can do (step by step):

Data <- data.frame(
Date = c("2017-06-30","2017-06-29","2017-06-28","2017-06-27"),
TS1 = c(1,2,3,4),
TS2 = c(5,6,7,8),
TS3 = c(9,10,11,12))
Data$Date<-as.Date(Data$Date, format="%Y-%m-%d")
Data<-Data[order(Data$Date),]
last_row<-Data[nrow(Data),]
p<-barplot(t(last_row[2:4]), beside=T, names.arg = colnames(last_row)[2:4])
mtext(side=1, text=last_row$Date, line=2.5)
text(y=t(last_row[2:4])+0.8, x=p, pos=1,labels=last_row[2:4], xpd=TRUE)

在此处输入图片说明

Following up my comment:

barplot(t(Data[,2:4]), col=c("red","lightblue","gray"), 
    beside = T, las=3, 
    names.arg=Data[order(as.Date(as.character(Data[,1]), "%Y-%m-%d")),1], las=1)

在此处输入图片说明

If you only want the last time entry:

barplot(t(Data[nrow(Data),2:4]), col=c("red","lightblue","gray"), 
    beside = T, las=3, 
    names.arg=Data[order(as.Date(as.character(Data[,1]), "%Y-%m-%d")),1][nrow(Data)],las=1)

在此处输入图片说明

explanation:

las=1 at the end makes the labels to be horizontal.

order(as.Date(as.character(Data[,1]), "%Y-%m-%d")),1 this will give the ordered index of dates in the first column that we use to take the sorted labels for plotting.

nrow(Data) in the first line and third line point to the last entry.

beside=T makes them to be plotted in a group.

Update:

Answering OP's question in the comments: "Is there an easy way to display the time-series name (TS1, TS2, TS3 in this example) below each bar instead of the date for the whole axis as well as the values above the bars?"

You can define your axes as you wish. Below is an example that addresses your need:

barplot(t(Data[nrow(Data),2:4]), col=c("red","lightblue","gray"), 
        beside = T, axes=FALSE, xaxt="n") #plot with no axis
axis(2,0:12,las=2)    #y-axis
axis(1,2.5,line=2, las=1,
     labels =Data[order(as.Date(as.character(Data[,1]), "%Y-%m-%d")),1][nrow(Data)]) #x-axis with the date as the label in the second(3rd) line
axis(1,1:3+0.5,line=0, las=1,
     labels =colnames(Data[2:ncol(Data)])) #x-axis with time series names
text(x = 1:3+0.5, y = Data[nrow(Data),2:4], label = Data[nrow(Data),2:4], 
 pos = 3, cex = 0.8, col = "red", xpd=TRUE) # add the values as text to the plot

在此处输入图片说明

A solution with ggplot2

require(ggplot2)
require(reshape2)

df1 <- melt(Data, "Date")

g1 <- ggplot(subset(df1, Date>=as.Date("2017-06-30")), aes(x=Date, y=value))+
  geom_bar(aes(fill=variable), stat="identity", position="dodge")+
  theme_bw()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM