I want the column header and row header of this plot to be phi = 0.8, phi = 0.9, phi = 0.95 and sd = 1, sd = 3, sd = 5, sd = 10 respectively. The phi should appear as the Greek letter symbol while the sd remains the English letter.
## simulate ARIMA(1, 0, 0)
set.seed(289805)
x1 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 1)
set.seed(671086)
x2 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 1)
set.seed(799837)
x3 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 1)
set.seed(289805)
x4 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 3)
set.seed(671086)
x5 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 3)
set.seed(799837)
x6 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 3)
set.seed(289805)
x7 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 5)
set.seed(671086)
x8 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 5)
set.seed(799837)
x9 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 5)
set.seed(289805)
x10 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 10)
set.seed(671086)
x11 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 10)
set.seed(799837)
x12 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)
xx <- 1:10
df <- data.frame(xx, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)
reshapp <- reshape2::melt(df, id = "xx")
NEWDAT <- data.frame(y = reshapp$value, x = reshapp$xx, sd = rep(rep(c(sd=1, sd=3, sd=5, sd=10), each = 10), each = 3),phi = rep(rep(c(.8, .9, .95), each = 10), 4))
ggplot(NEWDAT, aes(x = x, y = y)) + geom_line() + geom_point() + labs(x = 'lb', y = 'RMSE') + facet_grid(sd ~ phi, scales = "free_y") +
theme_bw() + ggplot2::scale_y_continuous(expand = c(0.0, 0.00))
[Edited to account for the refactor in the third line of code]
A quick fix is to either create or rename the faceted key with Phi expressed in UTF8 encoding, plus the creation of the string you want (ie. "Phi|SD = N")
In this case I create a new variable:
NEWDAT %>%
mutate(phi_label = paste0("\U03D5 = ", phi), #"\U03D5" represents the character "ϕ"
sd_label = fct_reorder(.f = paste0("sd =", sd), .x = sd)) %>%
ggplot(aes(x = x, y = y)) + geom_line() + geom_point() + labs(x = 'lb', y = 'RMSE') +
facet_grid(sd_label ~ phi_label, scales = "free_y") +
theme_bw() + ggplot2::scale_y_continuous(expand = c(0.0, 0.00))
Here is a solution with plotmath using only standard characters, no special escape sequences.
NEWDAT <- data.frame(y = reshapp$value, x = reshapp$xx, sd = rep(rep(c(sd=1, sd=3, sd=5, sd=10), each = 10), each = 3),phi = rep(rep(c(.8, .9, .95), each = 10), 4))
NEWDAT$sd <- factor(NEWDAT$sd, levels = NEWDAT$sd, labels = paste("sd ==", NEWDAT$sd))
NEWDAT$phi <- with(NEWDAT, factor(phi, levels = phi, labels = paste("phi ==", phi)))
ggplot(NEWDAT, aes(x = x, y = y)) +
geom_line() +
geom_point() +
scale_y_continuous(expand = c(0.0, 0.00)) +
labs(x = 'lb', y = 'RMSE') +
facet_grid(
sd ~ phi,
scales = "free_y",
labeller = label_parsed
) +
theme_bw()
A partial answer is this: Use this facet_grid call instead of the one you're using right now.
facet_grid(sd ~ phi, scales = "free_y", labeller = . %>% label_both(sep = " = "))
Using bquote
facet_grid(sd ~ phi, scales = "free_y",
labeller =
label_bquote(rows = sigma == .(sd), cols = phi == .(phi))
)
This will yield the right labels.
Answer inspired from this post
Related
This is a follow up question to Combine ggflags with linear regression in ggplot2
I have a plot like below with a log-linear model for x and y for certain countries that I have made in R with ggplot2 and ggflags:
The problem is when I want to print out the regression equation, the R2 and the p-value with the help of stat_regline_equation and stat_cor, I get values for a linear model and not the log-linear model I want to use.
How can I solve this?
library(ggplot2)
library(ggflags)
library(ggpubr)
library(SciViews)
set.seed(123)
Data <- data.frame(
country = c("at", "be", "dk", "fr", "it"),
x = runif(5),
y = runif(5)
)
ggplot(Data, aes(x = x, y = y, country = country, size = 11)) +
geom_flag() +
scale_country() +
scale_size(range = c(10, 10)) +
geom_smooth(aes(group = 1), method = "lm", , formula = y ~ log(x), se = FALSE, size = 1) +
stat_regline_equation(label.y = 0.695,
aes(group = 1, label = ..eq.label..), size = 5.5) +
stat_cor(aes(group = 1,
label =paste(..rr.label.., ..p.label.., sep = "~`,`~")),
label.y = 0.685, size = 5.5, digits= 1)
edit: I have also tried to use ln(x) instead of log(x) but I do not get any results when printing out the coefficient from that either.
There are four things you need to do:
Provide your regression formula to the formula argument of stat_regline_equation
Use sub to change "x" to "log(x)" in eq.label
Change the x aesthetic of stat_cor to log(x)
Fix the x limits inside coord_cartesian to compensate
ggplot(Data, aes(x = x, y = y, country = country, size = 11)) +
geom_flag() +
scale_country() +
scale_size(range = c(10, 10)) +
geom_smooth(aes(group = 1), method = "lm", , formula = y ~ log(x),
se = FALSE, size = 1) +
stat_regline_equation(label.y = 0.695, label.x = 0.25,
aes(group = 1, label = sub("x", "log(x)", ..eq.label..)),
size = 5.5,
formula = y ~ log(x),
check_overlap = TRUE, output.type = "latex") +
stat_cor(aes(group = 1, x = log(x),
label =paste(..rr.label.., ..p.label.., sep = "~`,`~")),
label.x = 0.25,
label.y = 0.65, size = 5.5, digits= 1, check_overlap = TRUE) +
coord_cartesian(xlim = c(0.2, 1))
I want this ggplot-facet to look like this
set.seed(1)
n10_sd1_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 1)
set.seed(1)
n10_sd1_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 1)
set.seed(1)
n10_sd1_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 1)
set.seed(1)
n10_sd3_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 3)
set.seed(1)
n10_sd3_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 3)
set.seed(1)
n10_sd3_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 3)
set.seed(1)
n10_sd5_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 5)
set.seed(1)
n10_sd5_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 5)
set.seed(1)
n10_sd5_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 5)
set.seed(1)
n10_sd10_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 10)
set.seed(1)
n10_sd10_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 10)
set.seed(1)
n10_sd10_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 10)
xx = 1:10
n10_df <- data.frame(xx = 1:10, x1 = n10_sd1_arma0.5_0.3, x2 = n10_sd1_arma0.5_0.4, x3 = n10_sd1_arma0.35_0.6, x4 = n10_sd3_arma0.5_0.3, x5 = n10_sd3_arma0.5_0.4, x6 = n10_sd3_arma0.35_0.6, x7 = n10_sd5_arma0.5_0.3, x8 = n10_sd5_arma0.5_0.4, x9 = n10_sd5_arma0.35_0.6, x10 = n10_sd10_arma0.5_0.3, x11 = n10_sd10_arma0.5_0.4, x12 = n10_sd10_arma0.35_0.6)
n10_df |>
tidyr::pivot_longer(-xx) |>
dplyr:: mutate(id = as.numeric(gsub("x", "", name))) |>
dplyr::arrange(id, xx) |>
dplyr::select(-id) |>
dplyr::mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
psi = rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)), each = 10), 4)) |>
dplyr::mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
psi = factor(psi, levels = psi, labels = gsub("c", "", paste("\U03A8 =", psi)))) |>
ggplot2::ggplot(aes(x = xx, y = value)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::scale_y_continuous(expand = c(0.0, 0.00)) +
ggplot2::labs(x = "Time", y = "Series") +
ggplot2::facet_grid(sd ~ psi, scales = "free_y") +
ggplot2::theme_bw() + ggplot2::theme(strip.text.x = ggplot2::element_text(size = 20, face = "bold"), strip.text.y = ggplot2::element_text(size = 16, face = "bold"), axis.title = ggplot2::element_text(size = 20), axis.title.x = ggplot2::element_text(angle = 0, hjust = 0.5,
vjust = 0.5, size = 20), axis.title.y = ggplot2::element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 20))
I think my problem lies in how to twist this part rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)) to be what I want. Please help!
Edit
n10_df |>
tidyr::pivot_longer(-xx) |>
dplyr:: mutate(id = as.numeric(gsub("x", "", name))) |>
dplyr::arrange(id, xx) |>
dplyr::select(-id) |>
dplyr::mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
psi = rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)), each = 10), 4)) |>
dplyr::mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
psi = factor(psi, levels = psi, labels = sapply(psi, function(x) sprintf('\U03C6 = %s, \U1D717 = %s', x[1], x[2])))) |>
ggplot2::ggplot(aes(x = xx, y = value)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::scale_y_continuous(expand = c(0.0, 0.00)) +
ggplot2::labs(x = "Time", y = "Series") +
ggplot2::facet_grid(sd ~ psi, scales = "free_y") +
ggplot2::theme_bw() + ggplot2::theme(strip.text.x = ggplot2::element_text(size = 20, face = "bold"), strip.text.y = ggplot2::element_text(size = 16, face = "bold"), axis.title = ggplot2::element_text(size = 20), axis.title.x = ggplot2::element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 20), axis.title.y = ggplot2::element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 20))
I have this this ggplot with facet_grid function:
set.seed(1)
df <- data.frame(xx = 1:10, x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10), x4 = rnorm(10), x5 = rnorm(10), x6 = rnorm(10), x7 = rnorm(10), x8 = rnorm(10), x9 = rnorm(10), x10 = rnorm(10), x11 = rnorm(10), x12 = rnorm(10))
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(-xx) %>%
mutate(id = as.numeric(gsub("x", "", name))) %>%
arrange(id, xx) %>%
select(-id) %>%
mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
phi = rep(rep(list(c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)), each = 10), 4)) %>%
mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
phi = factor(phi, levels = phi, labels = gsub("c", "", paste("\U03D5 =", phi)))) %>%
ggplot(aes(x = xx, y = value)) +
geom_line() +
geom_point() +
scale_y_continuous(expand = c(0.0, 0.00)) +
labs(x = "Time", y = "Series") +
facet_grid(sd ~ phi, scales = "free_y") +
theme_bw()
provided as a satisfactory answer for this question.
What I want
I wantto increase(or customize) the labels sd = c(sd = 1, sd = 3, sd = 5, sd = 10) at the right side and the labels at the top phi = c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6))of the plot. And also, how to make them bold.
Easiest way is with:
previous_code_blocks+
theme(strip.text.x = element_text(size = 10, face = "bold"),
strip.text.y = element_text(size = 10, face = "bold"))
You can customise with the usual element_text() args here, changing the font family and other such things, hjust, vjust etc.
If you're looking for more control of the labeller in the future, or for more advanced customisations, you can find documentation at: https://ggplot2.tidyverse.org/reference/labeller.html
I'm plotting a discrete CDF. I have a few questions regarding geom_step which I'm not finding by using Google.
Is it possible to make the line segment representing the jump dashed
rather than solid to better show whats going on?
Is it possible to add geom_point more efficiently than I do? (less
c/p).
Below is my current solution:
library(tidyverse)
library(ggthemes)
theme_set(theme_few())
x0 <- seq(-0.5, -0.01, by = 0.01)
x1 <- seq(0, 0.99, by = 0.02)
x2 <- seq(1, 1.99, by = 0.02)
x3 <- seq(2, 2.99, by = 0.02)
x35 <- seq(3, 3.49, by = 0.01)
x4 <- seq(3.5, 3.99, by = 0.01)
tibble_ex <- tibble(
x0 = x0,
x1 = x1,
x2 = x2,
x3 = x3,
x35 = x35,
x4 = x4
)
tibble_ex %>%
gather(x, xax, x0:x4) %>%
mutate(cdf = case_when(x == 'x0' ~ 0,
x == 'x1' ~ 1/2,
x == 'x2' ~ 3/5,
x == 'x3' ~ 4/5,
x == 'x35' ~ 9/10,
x == 'x4' ~ 1)) %>%
ggplot(aes(x = xax, y = cdf)) +
geom_step() +
geom_point(aes(x = 0, y = 0), size = 3, shape = 21, fill = 'white') +
geom_point(aes(x = 1, y = 0.5), size = 3, shape = 21, fill = 'white') +
geom_point(aes(x = 2, y = 3/5), size = 3, shape = 21, fill = 'white') +
geom_point(aes(x = 3, y = 4/5), size = 3, shape = 21, fill = 'white') +
geom_point(aes(x = 3.5, y = 9/10), size = 3, shape = 21, fill = 'white') +
geom_point(aes(x = 0, y = 0.5), size = 3, shape = 21, fill = 'black') +
geom_point(aes(x = 1, y = 3/5), size = 3, shape = 21, fill = 'black') +
geom_point(aes(x = 2, y = 4/5), size = 3, shape = 21, fill = 'black') +
geom_point(aes(x = 3, y = 9/10), size = 3, shape = 21, fill = 'black') +
geom_point(aes(x = 3.5, y = 1), size = 3, shape = 21, fill = 'black') +
labs(x = 'x', y = 'F(x)')
ggplot will be more powerful to use if you can put your data into a data frame and structure it so that the characteristics of your data can be mapped directly.
Here's a way to take your data and augment it with additional rows that represent the connecting points, by matching each x with the prior cdf value. I added a column, type, to keep track of which is which. I also arrange df so that geom_segment plots the points in the right order.
new_steps <-
tibble(x = c(0:3, 3.5, 4),
cdf = c(0, .5, .6, .8, .9, 1))
df <- new_steps %>%
mutate(type = "cdf") %>%
bind_rows(new_steps %>%
mutate(type = "prior",
cdf = lag(cdf))) %>%
drop_na() %>%
arrange(x, desc(type))
Then we can map the points' fill and the geom_segments' linetype to type.
ggplot(df) +
geom_point(aes(x, cdf, fill = type),
shape = 21) +
scale_fill_manual(values = c("black", "white")) +
geom_segment(aes(x = lag(x), y = lag(cdf),
xend = x, yend = cdf,
lty = type)) +
scale_linetype_manual(values = c("dashed", "solid"))
(1) No, there is not a built-in way to make the geom_step half-dashed. But if you post this as a separate question, perhaps someone will help create a new geom for this.
(2) The answer is to put the points you want plotted in a data frame, like anything else you might want to plot:
point_data = data.frame(x = rep(c(0, 1, 2, 3, 3.5), 2),
y = c(0, rep(c(.5, .6, .8, .9), 2), 1),
z = rep(c("a", "b"), each = 5))
# calling your gathered/mutated version of tibble_ex df
ggplot(df, aes(x = xax, y = cdf)) +
geom_step() +
geom_point(data = point_data, aes(x = x, y = y, fill = z), shape = 21) +
scale_fill_manual(values = c("white", "black"), guide = FALSE) +
labs(x = 'x', y = 'F(x)')
For the second part of your question, you can put all the coordinates in a separate data frame and call geom_point only once:
ddf <- data.frame(xax = rep(c(0:3, 3.5), 2),
cdf = c(0, .5, .6, .8, .9, .5, .6, .8, .9, 1),
col = rep(c("white", "black"), each = 5))
dev.new()
tibble_ex %>%
gather(x, xax, x0:x4) %>%
mutate(cdf = case_when(x == 'x0' ~ 0,
x == 'x1' ~ 1/2,
x == 'x2' ~ 3/5,
x == 'x3' ~ 4/5,
x == 'x35' ~ 9/10,
x == 'x4' ~ 1)) %>%
ggplot(aes(x = xax, y = cdf)) +
geom_step() +
geom_point(data = ddf, aes(fill = I(col)), size = 3, shape = 21) +
labs(x = 'x', y = 'F(x)')
Just curious how can you generate the dcauchy distribution from Wikipedia:
Normally, you have
dcauchy(x, location = 0, scale = 1, log = FALSE)
for one line density p(x) v.s x
I assume in order to generate the diagram from wiki, a data.frame involves?
cauchy_dist <- data.frame(cauchy1 = rcauchy(10, location = 0, scale = 1, log = FALSE), cauchy2 = ....... , cauchy3 = ..... )
or you just need to
plot(x, P(x))
and then add lines to it?
You can use ggplot2's stat_function:
ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = dcauchy, n = 1e3, args = list(location = 0, scale = 0.5), aes(color = "a"), size = 2) +
stat_function(fun = dcauchy, n = 1e3, args = list(location = 0, scale = 1), aes(color = "b"), size = 2) +
stat_function(fun = dcauchy, n = 1e3, args = list(location = 0, scale = 2), aes(color = "c"), size = 2) +
stat_function(fun = dcauchy, n = 1e3, args = list(location = -2, scale = 1), aes(color = "d"), size = 2) +
scale_x_continuous(expand = c(0, 0)) +
scale_color_discrete(name = "",
labels = c("a" = expression(x[0] == 0*","~ gamma == 0.5),
"b" = expression(x[0] == 0*","~ gamma == 1),
"c" = expression(x[0] == 0*","~ gamma == 2),
"d" = expression(x[0] == -2*","~ gamma == 1))) +
ylab("P(x)") +
theme_bw(base_size = 24) +
theme(legend.position = c(0.8, 0.8),
legend.text.align = 0)
You could create the data as follows:
location <- c(0, 0, 0, -2)
scale <- c(0.5, 1, 2, 1)
x <- seq(-5, 5, by = 0.1)
cauchy_data <- Map(function(l, s) dcauchy(x, l, s), location, scale)
names(cauchy_data) <- paste0("cauchy", seq_along(location))
cauchy_tab <- data.frame(x = x, cauchy_data)
head(cauchy_tab)
## x cauchy1 cauchy2 cauchy3 cauchy4
## 1 -5.0 0.006303166 0.01224269 0.02195241 0.03183099
## 2 -4.9 0.006560385 0.01272730 0.02272830 0.03382677
## 3 -4.8 0.006833617 0.01324084 0.02354363 0.03600791
## 4 -4.7 0.007124214 0.01378562 0.02440091 0.03839685
## 5 -4.6 0.007433673 0.01436416 0.02530285 0.04101932
## 6 -4.5 0.007763656 0.01497929 0.02625236 0.04390481
Map is used to apply a function of multiple variables to just as many vectors element by element. Thus, the first list element of cauchy_data will contain the following
dcauchy(x, location[1], scale[1])
and so on. I then put the Cauchy data in a data frame together with the vector of x coordinates, x. So you have the desired data table.
There are, of course, many ways to plot this. I prefer to use ggplot and show you how to plot as an example:
library(tidyr)
library(ggplot2)
curve_labs <- paste(paste("x0 = ", location), paste("gamma = ", scale), sep = ", ")
plot_data <- gather(cauchy_tab, key = curve, value = "P", -x )
ggplot(plot_data, aes(x = x, y = P, colour = curve)) + geom_line() +
scale_colour_discrete(labels = curve_labs)
You could tweak the plot in many ways to get something that more closely resembles the plot from Wikipedia.