R geom_forecast object not found - r

I want to create a forecasting plot via ggplot2. So, I have been trying to follow this example, however the desired output is not as intended, as I would like to have year on x-axis and AvgTMean on the y-axis`.
Purpose is the forecast the next 10 years.
How can I do this?
Data (AvgTMaxYear):
structure(list(year = 1980:2021, AvgTMean = c(24.2700686838937,
23.8852956598276, 25.094446596092, 24.1561175050287, 24.157183605977,
24.3047482638362, 24.7899738481466, 24.5756232655603, 24.5833086228592,
24.7344695534483, 25.3094451071121, 25.2100615173707, 24.3651692293534,
24.5423890611494, 25.2492166633908, 24.7005097837931, 24.2491591827443,
25.0912281781322, 25.0779264303305, 24.403294248319, 24.4983991453592,
24.4292324356466, 24.8179824927011, 24.7243948463075, 24.5086534543966,
24.2818632071983, 24.4567195220259, 24.8402224356034, 24.6574465515086,
24.5440715673563, 23.482670620977, 24.9979594684914, 24.5452453980747,
24.9271462811494, 24.7443215819253, 25.8929839790805, 25.1801908261063,
25.2079308058908, 25.0722425561207, 25.4554644289799, 25.4548979078736,
25.0756772250287)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-42L))
Method 1:
library(tidyverse)
library(forecast)
# Convert to timeseries object
AvgTMaxYearTS = ts(AvgTMaxYear)
# Plot
autoplot(AvgTMaxYearTS) + geom_forecast()
Output:
Method 2:
# Using fortify.ts
p = ggplot(aes(x = year, y = AvgTMean), data = AvgTMaxYear)
p = p + geom_line()
p + geom_forecast()
Error:
Error in FUN(X[[i]], ...) : object 'AvgTMean' not found

When I run your second method it runs like this:
library(ggplot2)
library(forecast)
p = ggplot(aes(x = year, y = AvgTMean), data = AvgTMaxYear)
p = p + geom_line()
p + geom_forecast()
Output:

Related

How can I change dates on an X axis into 'day 1', 'day 2' etc for a line graph plot?

I am trying to modify a line graph i have already made. On the x axis, it has the data in which a participant completed a task. However, I am trying to make it so the x axis simply show each completed session of the task as day 1, day 2 etc.... Is there a way to do this?
My code for the line graph is as follows:
ggplot(data = p07_points_scored, aes(x = day, y = total_score, group = 1)) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
labs(title=" P07s Total score on the training tool",
x = "Date of training completion",
y = "Total Score",
color = "lightblue") +
geom_smooth()
To further add to this. I have 4 separate line graphs from individual participants showing their total scores within the task. Is there a way to combine the separate graphs together into 1?
Many thanks :)
enter image description here
Here is an example with fake data: The key point is to mutate a new column days and assign it to the x axis with fct_inorder():
library(tidyverse)
library(lubridate)
# Create some fake data:
date <- dmy("6-8-2022"):dmy("5-9-2022")
y = rnorm(31, mean = 2300, sd = 100)
df <- tibble(date, y)
df %>%
mutate(days = paste0("day",row_number())) %>%
ggplot(aes(x = fct_inorder(days), y = y, group= 1)) +
geom_point()+
geom_line()
data:
df <- structure(list(date = 19210:19240, y = c(2379.71407792736, 2349.90296535465,
2388.14396999868, 2266.84629740315, 2261.95099255488, 2270.90461436351,
2438.19569234793, 2132.6468717962, 2379.46892613664, 2406.13636097426,
2176.9392984643, 2219.0521150482, 2221.22674399102, 2399.82972150781,
2396.76276645913, 2233.62763324748, 2468.98833991591, 2397.47855248058,
2486.96828322353, 2330.04116860874, 2280.66624489061, 2411.09933781266,
2281.06682518505, 2281.63162850277, 2235.66952459084, 2271.2152525563,
2481.86164459452, 2544.25592495568, 2411.90218614317, 2275.60378793237,
2297.98843827031)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-31L))

For Loop for ggplot2 in R

I have this data frame where I want to create multiple plots at the same time in a loop, but when I run the code it gives me an error. Can anyone please tell me what I am doing wrong!
Data:
structure(list(Date = structure(c(289094400, 297043200, 304992000,
312854400, 320716800, 328665600), tzone = "UTC", class = c("POSIXct",
"POSIXt")), NORTH = c(4.06976744186047, 5.51675977653633, 7.2799470549305,
4.75015422578655, 4.59363957597172, 3.15315315315317), YORKSANDTHEHUMBER = c(4.0121120363361,
5.45851528384282, 9.52380952380951, 6.04914933837431, 3.03030303030299,
5.42099192618225), NORTHWEST = c(6.57894736842105, 6.95256660168939,
6.50060753341436, 5.5904164289789, 4.59211237169096, 4.70041322314051
), EASTMIDS = c(4.98489425981872, 8.20143884892085, 6.91489361702127,
5.22388059701494, 5.61465721040189, 4.64465584778958), WESTMIDS = c(4.65838509316771,
4.74777448071216, 8.66855524079319, 6.56934306569344, 3.22896281800389,
3.17535545023698), EASTANGLIA = c(6.74525212835624, 8.58895705521476,
8.47457627118643, 10.7291666666667, 4.8447789275635, 4.84522207267835
), OUTERSEAST = c(6.7110371602884, 7.53638253638255, 9.47317544707589,
8.56512141280351, 3.82269215128102, 2.11515863689776), OUTERMET = c(4.54545454545458,
6.58505698607005, 7.36633663366336, 7.08225746956843, 4.3747847054771,
1.68316831683168), LONDON = c(8.11719500480309, 10.3065304309196,
6.32299637535239, 7.65151515151515, 1.30190007037299, 2.1535255296978
), SOUTHWEST = c(6.17577197149644, 7.71812080536912, 7.63239875389407,
9.45489628557649, 2.46804759806079, 2.19354838709679), WALES = c(6.09418282548476,
8.35509138381203, 7.40963855421687, 7.01065619742007, 1.15303983228513,
3.47150259067357), SCOTLAND = c(5.15222482435597, 4.12026726057908,
5.40106951871658, 8.67579908675796, -0.280112044817908, 2.94943820224719
), NIRELAND = c(4.54545454545454, 4.94752623688156, 4.42857142857145,
2.96397628818967, 6.06731620903454, 0.0835073068893502), UK = c(5.76890543055322,
7.20302836425676, 7.39543442582184, 7.22885986848197, 3.23472252213347,
2.95766398929048)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Code:
for (i in 2:ncol(data2)) { # Printing ggplot within for-loop
print(ggplot(data2, aes(x = Date, y = data2[, i])) + # Basic ggplot2 plot of x & y's
geom_line() +
labs(title = "Uk States",
y = "",
x = "") +
theme_bw() +
geom_hline(yintercept = 0))
Sys.sleep(1)
}
Error:
Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
Error in is.finite(x) : default method not implemented for type 'list'
I would suggest to loop over the column names instead of value. You may then use .data to use as y-index.
library(tidyverse)
for(i in names(data2)[-1]) { # Printing ggplot within for-loop
# Basic ggplot2 plot of x & y's
print(ggplot(data2, aes(x = Date, y = .data[[i]])) +
geom_line()+ labs(title = "Uk States",
y = "",
x = "")+
theme_bw()+
geom_hline(yintercept = 0))
Sys.sleep(1)
}
You may also try facet_wrap to combine multiple plots together.
data2 %>%
pivot_longer(cols = -Date) %>%
ggplot(aes(Date, value)) +
geom_line() + facet_wrap(~name) +
labs(title = "Uk States", x = "", y = "") +
theme_bw() +
geom_hline(yintercept = 0)
Another way of generating ggplot in a loop is to use lapply, where we loop for colnames and use aes_string as the aesthetic mapping.
Here the results are saved to the list ggplot_list, where you can extract individual plot by indexing (e.g. plot for NORTH is stored in ggplot_list[[1]])
Note that I've changed labs(title = i) so that the plot title would be your column names.
library(ggplot2)
ggplot_list <- lapply(colnames(data2[-1]), \(i) {
ggplot(data2, aes_string("Date", x)) +
geom_line() +
labs(title = i, y = "", x = "") +
theme_bw() +
geom_hline(yintercept = 0)
})

Setting time limits on axis removes all values

I have this simple data:
structure(list(ID = 1:2, timing = structure(c(1654641111.14,
1654640774.523), tzone = "CET", class = c("POSIXct", "POSIXt"))), class = "data.frame", row.names = c(NA,
-2L))
ID timing
1 1 2022-06-08 00:31:51.140
2 2 2022-06-08 00:26:14.523
When I plot using ggplot2, I get this:
ggplot(df_test,
aes(x = ID,
y = timing)) +
geom_point()
But when I set limits, the graph is empty:
lims <- as.POSIXct(strptime(c("35:00", "25:00"),
format = "%M:%OS",
tz = "CET"))
ggplot(df_test,
aes(x = ID,
y = timing)) +
geom_point() +
scale_y_datetime(limits = lims)
Warning message:
Removed 2 rows containing missing values (geom_point).
I tried to find any solution, but I can't figure out why the limits remove all my values. Does anyone have a solution?
This is simply due to your limits being round the wrong way (they should be earliest, latest whereas you have latest, earliest).
So you can do:
ggplot(df_test,
aes(x = ID,
y = timing)) +
geom_point() +
scale_y_datetime(limits = rev(lims))

r does not allow the x axis to display the title (now with added data)

The question was how to get R to display titles on the x- and y-axes when the plot is rotated. mtext was not allowing this to happen. The question then became how to do this with the data at hand.
Here is my edited code and data.
Small segment of my Data:
library(ggplot)
x <- structure(list(
CS1 = c(51.176802507837, 11.289327763008, 10.8584547767754, 5.37665764546685, 6.47159365761892),
CS2 = c(34.9956506731101, 45.7147446193383, 23.788413903316, 42.4969135802469, 18.8998879103283),
CS3 = c(3.59556251631428, 5.59228312932411, 11.7117536894149, 15.7240944017563, 9.72486977228754),
CS4 = c(0.830633241559198, 2.57358541893362, 3.05352639873916, 7.01238591916558, 2.98276253547777),
CS5 = c(6.6094547746612, 7.67873290538655, 9.93544994944388, 8.49609094535301, 6.71423210935406)),
class = c("tbl_df", "tbl", "data.frame"))
Now some code to make a ggplot.
xplot<-ggplot(x, aes(y = test, y = CS2, group = test))+
geom_boxplot()+
labs(y = "Intensity",
x = "Variable")+
scale_x_discrete()
xplot
Try using ggplot from the tidyverse.
<del>It is useful to have a basic dataset to run from:<\del> Now that you have some data
library(tidyverse)
x <-structure(list(
CS1 = c(51.176802507837, 11.289327763008, 10.8584547767754, 5.37665764546685, 6.47159365761892),
CS2 = c(34.9956506731101, 45.7147446193383, 23.788413903316, 42.4969135802469, 18.8998879103283),
CS3 = c(3.59556251631428, 5.59228312932411, 11.7117536894149, 15.7240944017563, 9.72486977228754),
CS4 = c(0.830633241559198, 2.57358541893362, 3.05352639873916, 7.01238591916558, 2.98276253547777),
CS5 = c(6.6094547746612, 7.67873290538655, 9.93544994944388, 8.49609094535301, 6.71423210935406)),
row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame")
)
Now gather that data into two columns
x1 <- gather(x, test, values, CS1:CS5)
Now plot
xplot<-ggplot(x1, aes(x = test, y = values, group = test))+
geom_boxplot()+
labs(y = "Intensity",
x = "Variable")
xplot + coord_flip()

Adding confidence intervals ggplot

I want to plot intervals of confidence of fitted values. I read some post related, but I am still stuck..These are a sample of the date:
pd <-structure(list(date = 1:5, obs = c(44.6651011845397, 62.3441339250369,
52.8968240161506, 51.7795930633478, 63.1284636561025), pred = c(47.2643891039645,
55.7996450577325, 52.9566469533233, 51.3393289316, 59.0011440099732)),
.Names = c("date", "obs", "pred"), row.names = c(NA, 5L), class = "data.frame")
pd2 <- structure(list(date = 1:5, lwr = c(44.8529592578518, 54.9926370476338,
51.7358955911624, 49.401869166722, 58.1674619447108), upr = c(49.6758189500772,
56.6066530678312, 54.1773983154842, 53.2767886964779, 59.8348260752356
)), .Names = c("date", "lwr", "upr"), row.names = c(NA, 5L), class = "data.frame")
dd <- melt(pd, id=c("date")) #Data
dd2 <- melt(pd2,id=c("date")) #Intervals of conf.
p <- ggplot(dd) + geom_line(aes(x=date, y=value, colour=variable))
p <- p + geom_smooth(aes(x=date, y=value, ymax=lwr, ymin=upr), #1 way
colour='grey', data=dd2, stat='identity')
Also tried...
# p+ geom_ribbon(data=dd2,aes(ymin=lwr,ymax=upr),alpha=0.3) #2.
I received the error:
Error in eval(expr, envir, enclos) : object 'lwr' not found ....what am I missing?
I also tried to do it without using melt ... but then I also had problems with the legend!
In the first way, the dd object you are using is not having the lwr/upr columns. So you cannot really plot them.
Can you do a:
dd<-merge(dd,pd2,by='date')
just after the melts and then:
p <- ggplot(dd) + geom_line(aes(x=date, y=value, colour=variable))
p + geom_ribbon(data=dd,aes(x=date, y=value, ymin=lwr,ymax=upr, group=variable),alpha=0.3)
Is this helping?

Resources