简体   繁体   English

在R中的数据框中创建条形图,其值对应于多个时间序列的最新日期

[英]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? 有没有一种方法可以制作条形图,在x轴上带有最后一个日期(在这种情况下为2017-06-30 ),在y上带有TS1TS2TS3 (或4,8,12 )的相应值-轴?

Ideally, the solution would be in base-R, but any other way is also fine! 理想情况下,解决方案是在base-R中进行,但其他任何方式也都可以!

For the last date 2017-06-30 you can do (step by step): 对于最后日期2017-06-30,您可以(逐步)进行操作:

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. las=1最后使标签成为水平。

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. order(as.Date(as.character(Data[,1]), "%Y-%m-%d")),1这将在我们用来进行排序的第一列中给出日期的有序索引绘图标签。

nrow(Data) in the first line and third line point to the last entry. 第一行和第三行中的nrow(Data)指向最后一个条目。

beside=T makes them to be plotted in a group. beside=T使它们成组绘制。

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?" 在评论中回答OP的问题: “是否有一种简单的方法来显示每个条形下方的时间序列名称(在此示例中为TS1,TS2,TS3),而不是整个轴的日期以及条形上方的值? “

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 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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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