将列表中的主要标题添加到for循环的图形中

时间:2018-12-11 09:28:15

标签: r list for-loop ggplot2

我有两个数据集。一个测量了几天的浓度,另一个测量了每个相关日期的风向。

library(ggplot2)

concentrations = data.frame(
  datehour = c("2017-02-15 09:00:00", "2017-02-15 10:00:00","2017-02-15 
               11:00:00", "2017-02-16 09:00:00", "2017-02-16 10:00:00", 
               "2017-02-16 11:00:00"),
  Number = c(3000, 4000, 2000, 6000, 7000, 5000),
  Hour = c(9, 10, 11, 9, 10, 11))

winddir = data.frame(
  Date = c("2017-02-15", "2017-02-16"),
  Wind = c("S", "SW"))

我使用for循环创建带有每天图表的PDF。这很好。

但是,我想在每个图表中添加一个主标题,并带有相关日期的日期和风向。因此,我将风向数据帧转换为两个列表,以用于for循环中。

我可以很好地使用日期列表,但是我不知道如何获取主标题的风向。

我尝试从c = 1开始,然后将每个循环1额外添加到c,并使用paste(wind_list[c])从wind_list获取项目。这将返回标题为c的标题(因此为“ Wind:1”,“ Wind:2”等),而不是风向。

#creating the lists
wind_list <- as.list(winddir$Wind)
date_list <- as.list(winddir$Date)

#function for plotting graph
plotdays <- function(){
  ggplot() +
    geom_line(data = concTemp, aes(x = Hour, y = Number))+
                ylab("UFP concentration") + xlab("Hour") + 
    ggtitle(paste("Average UFP concentration per hour on", paste(i),
                  "     Wind:", paste(wind_list[c])))
} 

#the for loop for creating the graphs in 1 PDF

pdf("Test.pdf", onefile = TRUE)
c = 1

for (i in date_list){
  concTemp <- subset(concentrations, grepl(i, datehour))
  if (c == 1){
    wind <-  wind_list[c]
    plotD <- plotdays()
    print(plotD)
    c = c+1
  } else {
    wind <-  wind_list[c]
    plotD <- plotdays()
    print(plotD)
    break
  }
}

dev.off()

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您的风向标包​​含因子,您的函数似乎在读取基本整数。您可以将风向添加为角色。

public ObservableCollection<ShowShipmentData> ShipmentData
{
    get { return _shipmentData; }
    set
    {
        _shipmentData = value;
        RaisePropertyChanged();
    }
}

ShipmentData = DependencyService.Get<ISQLite2().ShowShipmentTableData(GlobalVariable.BranchId); 

public Command PrintLabel
{
    get
    {
        return new Command(async (e) =>
        {
            ShowShipmentData selecedItem = (e as ShowShipmentData);
        });
    }
}

结果: enter image description here