Add labels on top of plot of shapefiles - r

Consider an example dataset where I have four polygons:
I want the colour of the polygon to be based on which group it is in, a legend showing what each colour means and a label over each polygon with it's house number.
The following line of code differentiates the colour by group:
plot(df$geometry, col=df$group)
Reproducible data from example above:
structure(list(shape = c("polygon 1", "polygon 2", "polygon 3",
"polygon 4"), geometry = structure(list(structure(list(structure(c(0,
1, 1, 0, 0, 1, 1, 0, 0, 1), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg"), precision = 0, bbox = structure(c(xmin = 0,
ymin = 0, xmax = 1, ymax = 1), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), n_empty =
0L),
structure(list(structure(c(2, 3, 3, 2, 2, 3, 3, 2, 2, 3), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox =
structure(c(xmin = 2,
ymin = 2, xmax = 3, ymax = 3), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"),
n_empty = 0L),
structure(list(structure(c(4, 5, 5, 4, 4, 4, 4, 5, 5, 4), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox =
structure(c(xmin = 4,
ymin = 4, xmax = 5, ymax = 5), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"),
n_empty = 0L),
structure(list(structure(c(6, 7, 7, 6, 6, 7, 7, 6, 6, 7), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox =
structure(c(xmin = 6,
ymin = 6, xmax = 7, ymax = 7), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"),
n_empty = 0L)), precision = 0, bbox = structure(c(xmin = 0,
ymin = 0, xmax = 7, ymax = 7), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), n_empty =
0L, class = c("sfc_POLYGON",
"sfc")), group = c(1, 1, 2, 2), house = c(1, 2, 3, 4)), row.names =
c(NA,
4L), class = c("sf", "tbl_df", "tbl", "data.frame"), sf_column =
"geometry", agr = structure(c(shape = NA_integer_,
group = NA_integer_, house = NA_integer_), class = "factor", .Label =
c("constant",
"aggregate", "identity")))

I couldn't plot it using the basic plot(). However, I think I achieved your goal using the ggplot package in a much more readable way, as follows:
# setup environment
library(ggplot2)
# define a data frame with x and y coordinates and their groups
df = data.frame(
group = rep(1:2, each = 8),
house = rep(1:4, each = 4),
x = c(0, 1, 1, 0,
2, 3, 3, 2,
4, 5, 5, 4,
6, 7, 7, 6),
y = c(0, 0, 1, 1,
2, 2, 3, 3,
4, 4, 5, 5,
6, 6, 7, 7),
)
# create a data frame with text labels and their coordinates
lbl = df %>%
group_by(house) %>%
summarise(x = mean(x), y = max(y) + 0.5) %>%
unique()
# plot the houses using ggplot package
ggplot(df, aes(x, y)) +
geom_polygon(aes(fill = group, group = house), colour = 'black') +
labs(title = 'House Geometries', x = 'X', y = 'Y') +
scale_fill_manual(name = 'House', values = c('black', 'red')) +
annotate('text', x = lbl$x, y = lbl$y, label = lbl$house, size = 6) +
theme(plot.title = element_text(size = 19, face = 'bold', hjust = 0.5),
legend.title = element_text(size = 15, hjust = 0.5),
legend.text = element_text(size = 14, hjust = 0.5),
axis.title.x = element_text(size = 15),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.text.y = element_text(size = 14))
Here is the final plot:
By the way, I work with civil engineering and your problem was quite interesting to me...
Let me know if this is what you were planning to do.

Related

How to set the stat_function in for loop to plot two graphs with normal distribution, central and variance parameters

I would like to create the following plots in parallel
I have used the following code using the wide format dataset:
sumstatz_1 <- data.frame(whichstat = c("mean",
"sd upr",
"sd lwr",
"median"),
value = c(mean(data$score),
mean(data$score)+sd(data$score),
mean(data$score)-sd(data$score),
median(data$score)))
plot2 = ggplot(data, aes(x = score)) +
geom_histogram(aes(y =..density..),
breaks = seq(0, max(data$score), by = 5),
colour = "black",
fill = "white") + stat_function(fun = dnorm,
args = list(mean = mean(data$score, na.rm = TRUE),
sd = sd(data$score, na.rm = TRUE)),
colour = 'black', size = 1) +
labs(title='score', x='score', y= 'Distribution') +
geom_vline(data=sumstatz_1,aes(xintercept = value,
linetype = whichstat,
col = whichstat),size=1)
I have taken it by changing just the variable of interest to create the second graph. Anyway, I would like to create the same result by using an interactive graph. Here I have set up the following code that I have converted into a long format for convenience and then I have coded the following for loop:
for (i in 101:ncol(long)) {
p <- ggplot(long, aes(x = points)) +
geom_histogram(aes(y =..density..),
breaks = seq(0, 50, by = 3),
colour = "black",
fill = "white") + facet_grid(.~ score)
} for (j in seq_along(long$score)){
p +
stat_function(fun = dnorm[???],
args = list(mean = mean(long$points[long$score == 'j'], na.rm = TRUE),
sd = mean(long$points[long$score == 'j'], na.rm = TRUE)),
colour = 'black', size = 1)
}
print(p)
But I have no clue how to set parameters in stat_function() nor wether it is possible to use in a for loop or another iterative method. Would you have possibly any suggestion?
Here the dataset
structure(list(ID = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7,
7, 8, 8, 9, 9, 10, 10), score = structure(list(MM_score = c("score_2",
"score_1", "score_2", "score_1", "score_2", "score_1", "score_2",
"score_1", "score_2", "score_1", "score_2", "score_1", "score_2",
"score_1", "score_2", "score_1", "score_2", "score_1", "score_2",
"score_1")), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame")), points = c(53, 13.25, 17.5, 1.59090909090909,
48.5, 6.92857142857143, 40, 3.63636363636364, 46, 7.07692307692308,
38, 4.47058823529412, 14.5, 1.61111111111111, 19.5, 3.54545454545455,
37.5, 3.40909090909091, 5.5, 0.916666666666667)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), groups = structure(list(
ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .rows = structure(list(
1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18,
19:20), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))

Using Map on Function if name of df lists match

I have given a
a namend vector col:
col <- c(id = "CLUSTER", x = "LONGNUM", y = "LATNUM", n = "Severely.stunted.child.under.5.years..Total", pos = "Severely.stunted.child.under.5.years.Yes")
#a List of Dataframes with the the Elements of col as Columns and namend after a specific study area. (see deput below) The List Results contains 19 different files (different years)
a list of shapefiles of with 6 Elements (corresponding countries):
study_area <- c("Ethiopia", "Liberia", "Malawi", "Rwanda", "Uganda", "Zimbabwe")
Countries <- lapply(study_area, function(x){gisco_get_countries(country= x, resolution = 60 )})
Countries <- lapply(Countries, function(x) {as_Spatial(x, cast = TRUE, IDs = c("CNTR_NAME", "ISO§_CODE", "CNRT_ID", "NAME_ENGL", "FID"))})
names(Countries) <- study_area
I would like to preform the function from the prevR Library :
s.prevR(Results[[1]], col, Countries[[1]])
But actually for every element in the lists where the names fit:
I tried something like: Map(function(x, y) { as.prevR(x, col, y)}, Results, Countries)
But there it does (obviously) not match by names of x and y
dput( dput(Results[[1]][1:5,1:24])
structure(list(CLUSTER = c("", "1", "10", "100", "101"), Severely.stunted.child.under.5.years.No = c(3438,
8, 7, 9, 6), Severely.stunted.child.under.5.years.Yes = c(1047,
4, NA, 7, 1), Severely.stunted.child.under.5.years..Total = c(4485,
12, 7, 16, 7), Stunted.child.under.5.years.No = c(2531, 2, 7,
7, 5), Stunted.child.under.5.years.Yes = c(1954, 10, NA, 9, 2
), Stunted.child.under.5.years..Total = c(4485, 12, 7, 16, 7),
Severely.wasted.child.under.5.years.No = c(4295, 11, 7, 16,
7), Severely.wasted.child.under.5.years.Yes = c(190, 1, NA,
NA, NA), Severely.wasted.child.under.5.years..Total = c(4485,
12, 7, 16, 7), Wasted.child.under.5.years.No = c(3957, 10,
7, 16, 6), Wasted.child.under.5.years.Yes = c(528, 2, NA,
NA, 1), Wasted.child.under.5.years..Total = c(4485, 12, 7,
16, 7), Severely.underweight.child.under.5.years.No = c(4028,
10, 7, 12, 7), Severely.underweight.child.under.5.years.Yes = c(457,
2, NA, 4, NA), Severely.underweight.child.under.5.years..Total = c(4485,
12, 7, 16, 7), Underweight.child.under.5.years.No = c(3185,
7, 7, 12, 5), Underweight.child.under.5.years.Yes = c(1300,
5, NA, 4, 2), Underweight.child.under.5.years..Total = c(4485,
12, 7, 16, 7), LATNUM = c(NA, 10.889096, 5.323272, 8.830199,
10.806748), LONGNUM = c(NA, 37.269565, 39.556812, 40.72964,
39.7703), SurveyId = c("ET2005DHS", "ET2005DHS", "ET2005DHS",
"ET2005DHS", "ET2005DHS"), DHSC = c("ET", "ET", "ET", "ET",
"ET"), Country = c("Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia",
"Ethiopia")), row.names = c(NA, 5L), class = "data.frame")
and Countries
dput(Countries[[1]])
new("SpatialPolygonsDataFrame", data = structure(list(CNTR_NAME = "Federal Democratic Republic of Ethiopia",
ISO3_CODE = "ETH", CNTR_ID = "ET", NAME_ENGL = "Ethiopia",
FID = "ET"), class = "data.frame", row.names = 1L), polygons = list(
new("Polygons", Polygons = list(new("Polygon", labpt = c(39.6420582930584,
8.63562315843106), area = 93.13026982, hole = FALSE, ringDir = 1L,
coords = structure(c(41.6307, 42.4043, 41.816, 41.8348,
42.9681, 42.7628, 42.9804, 43.9589, 45.6126, 46.9411,
47.8524, 45.6126, 45.4747, 45.2923, 44.9162, 43.4741,
42.8138, 41.9101, 41.2328, 40.708, 39.9305, 39.5667,
38.9731, 38.1026, 36.9621, 35.9477, 35.8294, 35.3235,
35.0325, 34.9588, 34.5428, 33.7557, 33.0448, 33.2485,
33.8204, 34.0937, 34.1132, 34.4181, 34.8021, 35.2153,
35.6227, 36.1342, 36.5603, 37.2972, 37.5268, 37.9201,
38.5391, 39.0217, 40.0851, 40.8941, 41.6307, 13.3913,
12.4686, 11.6292, 11.0448, 10.9974, 10.7159, 10.0644,
9.0545, 8.4674, 8.0224, 7.9151, 5.5657, 5.4241, 5.2367,
4.9368, 4.7993, 4.301, 3.9823, 3.9616, 4.2326, 3.8858,
3.5224, 3.5158, 3.6459, 4.3833, 4.62, 5.2367, 5.413,
5.8494, 6.4537, 6.7418, 7.6074, 7.899, 8.381, 8.4168,
8.6026, 9.4986, 10.6735, 10.8052, 11.9187, 12.5064, 12.8315,
14.2577, 14.3876, 14.2588, 14.8128, 14.4413, 14.5899,
14.5456, 14.0891, 13.3913), dim = c(51L, 2L)))), plotOrder = 1L,
labpt = c(39.6420582930584, 8.63562315843106), ID = "1",
area = 93.13026982)), plotOrder = 1L, bbox = structure(c(33.0448,
3.5158, 47.8524, 14.8128), dim = c(2L, 2L), dimnames = list(c("x",
"y"), c("min", "max"))), proj4string = new("CRS", projargs = "+proj=longlat +datum=WGS84 +no_defs"))
If the Countries names are all in the Results names and if 'Results' have duplicates for names, then we can make the Countries to have the same length by replicating based on the names of the 'Results'
Map(function(x, y) { as.prevR(x, col, y)}, Results, Countries[names(Results)])

Adding p-values to ggplot; ggsignif says it can only handle data with groups that are plotted on the x-axis

I have data as follows, to which I am trying to add p-values:
library(ggplot2)
library(ggsignif)
library(dplyr)
data <- structure(list(treatment = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1), New_Compare_Truth = c(57,
61, 12, 14, 141, 87, 104, 90, 12, 14), total_Hy = c(135,
168, 9, 15, 103, 83, 238, 251, 9, 15), total = c(285, 305, 60,
70, 705, 435, 520, 450, 60, 70), ratio = c(47.3684210526316,
55.0819672131148, 15, 21.4285714285714, 14.6099290780142, 19.0804597701149,
45.7692307692308, 55.7777777777778, 15, 21.4285714285714), Type = structure(c(2L,
2L, 1L, 1L, 3L, 3L, 5L, 5L, 4L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), `Sample Selection` = c("Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu"), p_value = c(0.0610371842601616, 0.0610371842601616,
0.346302201593934, 0.346302201593934, 0.0472159407450147, 0.0472159407450147,
0.0018764377521242, 0.0018764377521242, 0.346302201593934, 0.346302201593934
), x = c(2, 2, 1, 1, 3, 3, 5.5, 5.5, 4.5, 4.5)), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
breaks_labels <- structure(list(Type = structure(c(2L, 1L, 3L, 5L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), x = c(2,
1, 3, 5.5, 4.5)), row.names = c(NA, -5L), class = c("data.table",
"data.frame"))
data %>%
ggplot(aes(x = x, y = ratio)) +
geom_col(aes(fill = `Sample Selection`), position = position_dodge(preserve = "single"), na.rm = TRUE) +
geom_text(position = position_dodge(width = .9), # move to center of bars
aes(label=sprintf("%.02f %%", round(ratio, digits = 1)), group = `Sample Selection`),
vjust = -1.5, # nudge above top of bar
size = 4,
na.rm = TRUE) +
# geom_text(position = position_dodge(width = .9), # move to center of bars
# aes(label= paste0("(", ifelse(variable == "Crime = 0", `Observation for Crime = 0`, `Observation for Crime = 1`), ")"), group = `Sample Selection`),
# vjust = -0.6, # nudge above top of bar
# size = 4,
# na.rm = TRUE) +
scale_fill_grey(start = 0.8, end = 0.5) +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_x_continuous(breaks = breaks_labels$x, labels = breaks_labels$Type) +
theme_bw(base_size = 15) +
xlab("Norm group for corporate Hy") +
ylab("Percentage Compliant Decisions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_signif(annotation=c("p=0.35", "p=0.06", "p=0.05", "p=0.34", "p=0.00"), y_position = c(30, 40, 55 ,75, 90), xmin=c(0.75,1.75,2.75,3.75,4.75),
xmax=c(1.25,2.25,3.25,4.25,5.25))
For some reason, the last line causes the following error:
Error in f(...) :
Can only handle data with groups that are plotted on the x-axis
Since I am just putting in text and not referring to any variable, I don't really understand why this happens. Can anyone help me out? Without the last line it looks like this:
EDIT: Please note that I would like to keep the space between the third and the fourth column (which is apparently also what caused the problem, see Jared's answer).
Edit
Thanks for clarifying your expected outcome. Here is one way to include geom_signif() annotations without altering the original plot:
library(tidyverse)
library(ggsignif)
data <- structure(list(treatment = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1), New_Compare_Truth = c(57,
61, 12, 14, 141, 87, 104, 90, 12, 14), total_Hy = c(135,
168, 9, 15, 103, 83, 238, 251, 9, 15), total = c(285, 305, 60,
70, 705, 435, 520, 450, 60, 70), ratio = c(47.3684210526316,
55.0819672131148, 15, 21.4285714285714, 14.6099290780142, 19.0804597701149,
45.7692307692308, 55.7777777777778, 15, 21.4285714285714), Type = structure(c(2L,
2L, 1L, 1L, 3L, 3L, 5L, 5L, 4L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), `Sample Selection` = c("Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu"), p_value = c(0.0610371842601616, 0.0610371842601616,
0.346302201593934, 0.346302201593934, 0.0472159407450147, 0.0472159407450147,
0.0018764377521242, 0.0018764377521242, 0.346302201593934, 0.346302201593934
), x = c(2, 2, 1, 1, 3, 3, 5.5, 5.5, 4.5, 4.5)), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
breaks_labels <- structure(list(Type = structure(c(2L, 1L, 3L, 5L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), x = c(2,
1, 3, 5.5, 4.5)), row.names = c(NA, -5L), class = c("data.table",
"data.frame"))
annotation_df <- data.frame(signif = c("p=0.35", "p=0.06", "p=0.05", "p=0.34", "p=0.00"),
y_position = c(30, 40, 55 ,75, 90),
xmin = c(0.75,1.75,2.75,4.25,5.25),
xmax = c(1.25,2.25,3.25,4.75,5.75),
group = c(1,2,3,4,5))
data %>%
ggplot(aes(x = x, y = ratio, group = `Sample Selection`)) +
geom_col(aes(fill = `Sample Selection`),
position = position_dodge(preserve = "single"), na.rm = TRUE) +
geom_text(position = position_dodge(width = .9), # move to center of bars
aes(label=sprintf("%.02f %%", round(ratio, digits = 1))),
vjust = -1.5, # nudge above top of bar
size = 4,
na.rm = TRUE) +
scale_fill_grey(start = 0.8, end = 0.5) +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_x_continuous(breaks = breaks_labels$x, labels = breaks_labels$Type) +
theme_bw(base_size = 15) +
xlab("Norm group for corporate Hy") +
ylab("Percentage Compliant Decisions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_signif(aes(xmin = xmin,
xmax = xmax,
y_position = y_position,
annotations = signif,
group = group),
data = annotation_df, manual = TRUE)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, y_position, annotations
Created on 2021-07-20 by the reprex package (v2.0.0)
Previous answer
One potential solution to your problem is to plot "Type" on the x axis instead of "x", e.g.
data %>%
ggplot(aes(x = Type, y = ratio)) +
geom_col(aes(fill = `Sample Selection`),
position = position_dodge(preserve = "single"), na.rm = TRUE) +
geom_text(position = position_dodge(width = .9), # move to center of bars
aes(label=sprintf("%.02f %%", round(ratio, digits = 1)),
group = `Sample Selection`),
vjust = -1.5,
size = 4,
na.rm = TRUE) +
scale_fill_grey(start = 0.8, end = 0.5) +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
theme_bw(base_size = 15) +
xlab("Norm group for corporate Hy") +
ylab("Percentage Compliant Decisions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_signif(annotation=c("p=0.35", "p=0.06", "p=0.05", "p=0.34", "p=0.00"),
y_position = c(30, 40, 55 ,75, 90),
xmin=c(0.75,1.75,2.75,3.75,4.75),
xmax=c(1.25,2.25,3.25,4.25,5.25))

How to change the y-axis scale in plot for a forecast object?

I have the following graph made with plot. I basically plotted the outcome of an arima model. The problem, as you can see, is the y-axis. I want to rescale it so that it shows values as integers and not in scientific notation. I already tried with ylim = c(a,b) but it didn't work.
This is the data to plot:
structure(list(method = "ARIMA(1,2,0)", model = structure(list(
coef = c(ar1 = 0.165440211592995), sigma2 = 314372.871343033,
var.coef = structure(0.0387588365491072, .Dim = c(1L, 1L), .Dimnames = list(
"ar1", "ar1")), mask = TRUE, loglik = -201.464633423226,
aic = 406.929266846451, arma = c(1L, 0L, 0L, 0L, 1L, 2L,
0L), residuals = structure(c(0.144002762945477, -0.257594259049227,
169.62992413163, -40.455716409227, 3.98528254071288, 325.669119576814,
-277.933508979317, 161.058607396831, 100.485413762468, 161.981734397248,
-21.1101185099251, 467.511038095663, 167.408540762885, 264.467148159716,
-870.459264535865, 1471.66097350626, 116.971877311758, -159.918791518434,
967.205782005673, -64.1682010133445, -372.385939678148, 352.062155538701,
632.526018003249, 1002.33521590517, 479.534164073812, 461.147699502253,
-1091.4663608196, -614.056109041783), .Tsp = c(1, 28, 1), class = "ts"),
call = arima(x = corona_total$Total_Cases, order = c(1, 2,
0)), series = "corona_total$Total_Cases", code = 0L,
n.cond = 0L, nobs = 26L, model = list(phi = 0.165440211592995,
theta = numeric(0), Delta = c(2, -1), Z = c(1, 2, -1),
a = c(-779, 59138, 53578), P = structure(c(-2.22044604925031e-16,
2.86887593857152e-17, -5.56124814802562e-17, 2.86887593857152e-17,
-3.31423141286073e-17, -1.61722928090181e-32, -5.56124814802562e-17,
-3.75958688714994e-17, -5.56124814802562e-17), .Dim = c(3L,
3L)), T = structure(c(0.165440211592995, 1, 0, 0, 2,
1, 0, -1, 0), .Dim = c(3L, 3L)), V = structure(c(1, 0,
0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)), h = 0, Pn = structure(c(1,
-5.4830714621183e-18, 1.21812129054869e-17, -5.48307146211831e-18,
-3.31423141286073e-17, -1.84889274661175e-32, 1.21812129054869e-17,
-3.75958688714994e-17, -5.56124814802562e-17), .Dim = c(3L,
3L))), x = structure(c(322, 400, 650, 888, 1128, 1694,
2036, 2502, 3089, 3858, 4636, 5883, 7375, 9172, 10149, 12462,
15113, 17660, 21157, 24747, 27980, 31506, 35713, 41035, 47021,
53578, 59138, 63919), .Tsp = c(1, 28, 1), class = "ts")), class = "Arima"),
level = c(80, 95), mean = structure(c(68571.1220751691, 73201.9225591844,
77829.1955946478, 82455.8850482763, 87082.4779540027, 91709.0548868236,
96335.6291770837, 100962.203030158, 105588.776810904, 110215.350579684,
114841.924346485, 119468.498112958, 124095.071879377, 128721.645645786,
133348.219412195, 137974.793178603, 142601.366945011, 147227.940711419,
151854.514477827, 156481.088244235, 161107.662010643, 165734.235777051,
170360.80954346, 174987.383309868, 179613.957076276, 184240.530842684,
188867.104609092, 193493.6783755, 198120.252141908, 202746.825908316,
207373.399674724, 211999.973441132, 216626.54720754, 221253.120973948,
225879.694740356, 230506.268506765, 235132.842273173, 239759.416039581,
244385.989805989, 249012.563572397, 253639.137338805, 258265.711105213,
262892.284871621, 267518.858638029, 272145.432404437, 276772.006170845,
281398.579937253, 286025.153703662, 290651.72747007, 295278.301236478,
299904.875002886, 304531.448769294, 309158.022535702, 313784.59630211,
318411.170068518, 323037.743834926, 327664.317601334, 332290.891367742,
336917.46513415, 341544.038900558), .Tsp = c(29, 88, 1), class = "ts"),
lower = structure(c(67852.5693904542, 71488.0378850631, 74869.4056219101,
78042.7559156995, 81035.3037876344, 83865.5016552685, 86546.988586515,
89090.4113186268, 91504.3946218833, 93796.1160212266, 95971.6728237902,
98036.3298321095, 99994.6937502293, 101850.840164951, 103608.408563905,
105270.675078771, 106840.60926587, 108320.919172912, 109714.087632186,
111022.401864165, 112247.977899771, 113392.780933102, 114458.642437768,
115447.274680314, 116360.283118863, 117199.177067588, 117965.378927058,
118660.232219392, 119285.008620154, 119840.914142592, 120329.094601246,
120750.640459457, 121106.591147333, 121397.938922313, 121625.632332819,
121790.579335963, 121893.650112513, 121935.679615911, 121917.46988679,
121839.792160025, 121703.388787634, 121508.974997728, 121257.240507039,
120948.851002351, 120584.449504222, 120164.657624752, 119690.076729762,
119161.289014511, 118578.858501069, 117943.331964511, 117255.239794357,
116515.096796942, 115723.402943843, 114880.644070922, 113987.29253211,
113043.807811626, 112050.637097962, 111008.215822666, 109916.968166633,
108777.307536393, 67472.1905761175, 70580.7621429779, 73302.5874546909,
75706.5864702675, 77834.1231526988, 79713.3753855171, 81365.1952663977,
82805.8644073931, 84048.5730632326, 85104.2982790952, 85982.3650762524,
86690.8252744766, 87236.7242194817, 87626.2949833746, 87865.1036821527,
87958.1607268483, 87910.0076619409, 87724.7860890043, 87406.2931723477,
86958.0269138267, 86383.2235034666, 85684.8884462828, 84865.8227394482,
83928.6450686659, 82875.8107702521, 81709.6281410368, 80432.2725549711,
79045.7987518185, 77552.151591525, 75953.1755121868, 74250.6228859222,
72446.1614324952, 70541.3808230744, 68537.798584461, 66436.8653962784,
64239.9698590982, 61948.4427995644, 59563.561168772, 57086.5515820169,
54518.5935412548, 51860.8223759273, 49114.3319330342, 46280.1770432903,
43359.3757867774, 40352.9115785747, 37261.7350923526, 34086.7660377659,
30828.8948056289, 27488.983993257, 24067.8698209688, 20566.3634495346,
16985.2522073028, 13325.3007348137, 9587.25205390016, 5771.82856756041,
1879.73299626436, -2088.35074420535, -6131.75671876397, -10249.8361987147,
-14441.9569333302), .Dim = c(60L, 2L), .Dimnames = list(NULL,
c("80%", "95%")), .Tsp = c(29, 88, 1), class = c("mts",
"ts", "matrix")), upper = structure(c(69289.674759884, 74915.8072333057,
80788.9855673855, 86869.0141808532, 93129.6521203709, 99552.6081183786,
106124.269767652, 112833.994741689, 119673.158999925, 126634.585138142,
133712.175869179, 140900.666393806, 148195.450008524, 155592.451126622,
163088.030260485, 170678.911278435, 178362.124624152, 186134.962249926,
193994.941323469, 201939.774624305, 209967.346121516, 218075.690621001,
226262.976649151, 234527.491939421, 242867.631033688, 251281.88461778,
259768.830291125, 268327.124531608, 276955.495663662, 285652.73767404,
294417.704748202, 303249.306422807, 312146.503267748, 321108.303025584,
330133.757147894, 339221.957677567, 348372.034433833, 357583.15246325,
366854.509725187, 376185.334984769, 385574.885889976, 395022.447212698,
404527.329236203, 414088.866273707, 423706.415304653, 433379.354716939,
443107.083144745, 452889.018392812, 462724.59643907, 472613.270508444,
482554.510211415, 492547.800741645, 502592.64212756, 512688.548533298,
522835.047604926, 533031.679858227, 543277.998104707, 553573.566912819,
563917.962101668, 574310.770264724, 69670.0535742206, 75823.0829753909,
82355.8037346047, 89205.1836262851, 96330.8327553065, 103704.73438813,
111306.06308777, 119118.541652923, 127128.980558576, 135326.402880273,
143701.483616717, 152246.170951439, 160953.419539271, 169816.996308198,
178831.335142237, 187991.425630358, 197292.726228081, 206731.095333834,
216302.735783307, 226004.149574644, 235832.10051782, 245783.58310782,
255855.796347471, 266046.121551069, 276352.103382299, 286771.433544331,
297301.936663213, 307941.557999181, 318688.352692291, 329540.476304445,
340496.176463526, 351553.785449769, 362711.713592006, 373968.443363436,
385322.524084435, 396772.567154431, 408317.241746781, 419955.270910389,
431685.428029961, 443506.533603539, 455417.452301683, 467417.090277392,
479504.392699952, 491678.341489281, 503937.9532303, 516282.277249338,
528710.393836741, 541221.412601694, 553814.470946882, 566488.732651987,
579243.386556237, 592077.645331285, 604990.74433659, 617981.94055032,
631050.511569476, 644195.754673588, 657416.985946874, 670713.539454249,
684084.766467015, 697530.034734447), .Dim = c(60L, 2L), .Dimnames = list(
NULL, c("80%", "95%")), .Tsp = c(29, 88, 1), class = c("mts",
"ts", "matrix")), x = structure(c(322, 400, 650, 888, 1128,
1694, 2036, 2502, 3089, 3858, 4636, 5883, 7375, 9172, 10149,
12462, 15113, 17660, 21157, 24747, 27980, 31506, 35713, 41035,
47021, 53578, 59138, 63919), .Tsp = c(1, 28, 1), class = "ts"),
series = "corona_total$Total_Cases", fitted = structure(c(321.855997237055,
400.257594259049, 480.37007586837, 928.455716409227, 1124.01471745929,
1368.33088042319, 2313.93350897932, 2340.94139260317, 2988.51458623753,
3696.01826560275, 4657.11011850993, 5415.48896190434, 7207.59145923711,
8907.53285184028, 11019.4592645359, 10990.3390264937, 14996.0281226882,
17819.9187915184, 20189.7942179943, 24811.1682010133, 28352.3859396781,
31153.9378444613, 35080.4739819968, 40032.6647840948, 46541.4658359262,
53116.8523004977, 60229.4663608196, 64533.0561090418), .Tsp = c(1,
28, 1), class = "ts"), residuals = structure(c(0.144002762945477,
-0.257594259049227, 169.62992413163, -40.455716409227, 3.98528254071288,
325.669119576814, -277.933508979317, 161.058607396831, 100.485413762468,
161.981734397248, -21.1101185099251, 467.511038095663, 167.408540762885,
264.467148159716, -870.459264535865, 1471.66097350626, 116.971877311758,
-159.918791518434, 967.205782005673, -64.1682010133445, -372.385939678148,
352.062155538701, 632.526018003249, 1002.33521590517, 479.534164073812,
461.147699502253, -1091.4663608196, -614.056109041783), .Tsp = c(1,
28, 1), class = "ts")), class = "forecast")
This is the code I used to make the plot (ignore the dotted exponential curve):
plot(forecast, shaded = TRUE, shadecols=NULL, lambda = NULL, col = 1, fcol = 4, pi.col=1,
pi.lty=2, ylim = NULL, main = "Out-of-Sample Forecast", ylab = "Number of Cases",
xlab = "Days (since 23/03/2020)") + abline(v = 28:29, col= "#FF000033", lty=1, lwd=5)
Output:
Can anyone please help me with this?
I couldn't load your object in my R session, so I'm assuming your plot works like a regular one.
You have 2 options.
Either you set options(scipen = 10) (or some high value), which is a quick fix, but if you need some plots with scientific notation and others without on the same graphics window, this will not work.
You define the axis yourself, with the format you need.
You can use axTicks(2) to get the position of default ticks and then format the labels as you need.
I recommend option 2. Here's a quick example :
x <- seq(1,10, l = 100)
y <- x*1e5
par(mfrow = c(1,2))
plot(x, y, main = "custom axis", yaxt = "n")
ticks <- axTicks(2) # get axis ticks
axis(2, at = ticks, labels = formatC(ticks, format = 'd')) # make axis
plot(x, y, main = "default axis")
Outputs :
You can take a look at other potential options in the answers to this post

How to export list of lists with different sizes in r

I researched ways to write this type of list, but I could not achieve.
Here is my data:
a<-structure(list(X2005 = structure(list(hours = list(c(0.2, 0,
4), c(0.2, 4)), maxx = structure(list(maxh2 = 4, maxh3 = 4), .Names = c("maxh2",
"maxh3"))), .Names = c("hours", "maxx")), X2006 = structure(list(
hours = list(c(1.8, 0, 1), c(1.8, 1)), maxx = structure(list(
maxh2 = 1.8, maxh3 = 1.8), .Names = c("maxh2", "maxh3"
))), .Names = c("hours", "maxx")), X2007 = structure(list(
hours = list(c(4.2, 0, 0), c(4.2, 0)), maxx = structure(list(
maxh2 = 4.2, maxh3 = 4.2), .Names = c("maxh2", "maxh3"
))), .Names = c("hours", "maxx")), X2008 = structure(list(
hours = list(c(0.1, 6, 0), c(3.1, 3)), maxx = structure(list(
maxh2 = 6, maxh3 = 3.1), .Names = c("maxh2", "maxh3"))), .Names = c("hours",
"maxx"))), .Names = c("X2005", "X2006", "X2007", "X2008"))
I need to see this list of lists in a excel sheet.
We could try this:
write.csv(do.call("rbind",list(unlist(a))),"testme.csv")
You could also try this and do some reshape2ing before export.
write.csv(do.call("cbind",list(unlist(a))),"testme2.csv")
Viewing the structure these yield:
View(do.call("cbind",list(unlist(a))))
Another option as suggested by #jay.sf :
openxlsx::write.xlsx(do.call("rbind",list(unlist(a))),"testme.xlsx")

Resources