I am trying to create a function that takes 2 parameters and displays the corresponding ggplot for them. The code works fine manually, but somehow I can't get it to work inside the function shell.
Error returned:
Error in eval(expr, envir, enclos) : object 'TimeVector' not found
I am trying to fix this by forcing objects that are not found that are added to ggplot as strings. This, in turn, creates various problems in the form of
Error: Discrete value supplied to continuous scale
Removing scale_x_continuous (breaks = 0: 24) fixes this second error, but displays an empty graph, assuming ggplot is not loading at all with any data.
The data is a large block of traffic density observations, grouped by time. It looks like this:
ID Road Status Time Statusint Day Months Year Weekday
1 Me7war To Sheikh Zayid Orange 2012-10-01 00:03:00 3 1 October 12 Monday
1 Me7war To Sheikh Zayid Green 2012-10-01 05:00:00 2 1 October 12 Monday
1 Me7war To Sheikh Zayid Yellow 2012-10-01 05:24:00 5 1 October 12 Monday
"Statusint", 1 ( ) 5 ( ) "". "" Posix, "TimeVector" ggplot.
:
Plotroad <- function( roadID , Day ) {
*** Working Code ***
else {
Roadsubset <- October[October$ID == as.numeric(roadID), ]
Timesubset <- subset(Roadsubset, format(Roadsubset$Time,'%d') == "Day" )
TimeVector <- as.numeric(gsub(":" , "." , strftime(Timesubset$Time, format="%H:%M")))
ggplot(Timesubset, aes( x = "TimeVector", y = "Timesubset$Statusint")) + geom_point() +
stat_smooth() + scale_x_continuous(breaks=0:24)
Roadsubset <- October[October$ID == as.numeric(roadID), ]
Timesubset <- subset(Roadsubset, subset = Roadsubset$Day == as.integer(Date) )
TimeVector <- as.numeric(gsub(":" , "." , strftime(Timesubset$Time, format="%H:%M")))
Timesubset$timevector <- TimeVector
print(ggplot( data = Timesubset, aes_string( x = "timevector" , y = "Statusint" )) + geom_point() + stat_smooth() + scale_x_continuous(breaks=0:24) + labs(list(title = as.character(Timesubset$Road[1]) , x = "Time of Day", y = "Status")))
}
}
, ggplot . , , .
, , , , , . .