0

I really need to fix the asthetic look of my plot. How do I change "alpha_1", "beta_1" and "alpha_1+beta_1" to actually being greek letters AND with the subscript? And furthermore, how do I change the red color to black in my legend aswell?

This is what my code plots: Plot

pparameters <- (ggplot(data = dataframe_alpha_1_time_melt, 
                aes(x = c(506,526,546,566,586,
                          606,626,646,666,686,
                          706,726,746,766,786,
                          806,826,846,866,886,
                          906,926,946,966,986,
                          1006), y = value))
                + geom_line(aes(linetype = "alpha_1"))
                + geom_line(data = data.frame(index = c(566,606), value = c(2.3134888e-01, 2.1824306e-01)),
                            aes(x = c(566,606), y = value, linetype = "alpha_1"), 
                            color = 'red')
                + geom_line(data = dataframe_beta_1_time_melt,
                            aes(x = c(506,526,546,566,586,
                                      606,626,646,666,686,
                                      706,726,746,766,786,
                                      806,826,846,866,886,
                                      906,926,946,966,986,
                                      1006), y = value, linetype = "beta_1"))
                + geom_line(data = data.frame(index = c(566,606), value = c(6.9072532e-01, 7.0119969e-01)),
                                              aes(x = c(566,606), y = value, linetype = "beta_1"),
                                              color = 'red')
                + geom_line(data = dataframe_phi_1_time_melt,
                            aes(x = c(506,526,546,566,586,
                                      606,626,646,666,686,
                                      706,726,746,766,786,
                                      806,826,846,866,886,
                                      906,926,946,966,986,
                                      1006), y = value, linetype = "alpha_1+beta_1"))
                + geom_line(data = data.frame(index = c(566,606), value = c(2.3134888e-01+6.9072532e-01,2.1824306e-01+7.0119969e-01)),
                            aes(x = c(566,606), y = value, linetype = "alpha_1+beta_1"),
                            color = 'red')
                + scale_linetype_manual(name = "Parameter values", 
                                        values = c("alpha_1" = "dotted",
                                                   "beta_1" = "dashed",
                                                   "alpha_1+beta_1" = "solid"),
                                        na.value = 0)
                + ggtitle("GARCH-parameters")
                + theme(plot.title = element_text(hjust = 0.5)) 
                + xlab("Index") 
                + xlim(506,1006)
                + ylab("Parameter"))

Please don't hate my pretty ugly coding...

UPDATE: This is the dataframes:

alpha_0_time <- c(1.1748204e-07, 5.0480320e-06, 5.3019681e-06, 4.6938408e-06, NA,
                  4.6302995e-06, 4.7767390e-06, 4.7836535e-06, 4.7671368e-06, 4.6037647e-06,
                  5.1590071e-06, 4.4693325e-06, 4.0124395e-06, 4.1799942e-06, 4.0282396e-06,
                  4.0504593e-06, 4.1409772e-06, 4.1530203e-06, 4.0975707e-06, 4.0765892e-06,
                  4.2088018e-06, 4.0489639e-06, 4.2625406e-06, 4.3364828e-06, 4.2168075e-06,
                  4.1120033e-06)

alpha_1_time <- c(1.9677291e-02, 1.9698912e-01, 2.4010802e-01, 2.3134888e-01, NA,
                  2.1824306e-01, 2.1745388e-01, 2.1548852e-01, 2.1406954e-01, 2.1061723e-01,
                  2.1177395e-01, 1.9742529e-01, 1.9495637e-01, 2.1085685e-01, 2.0148171e-01,
                  2.0133930e-01, 2.0371192e-01, 2.0661984e-01, 2.0060420e-01, 1.9862234e-01,
                  2.1361334e-01, 2.0753846e-01, 2.1759517e-01, 2.1918565e-01, 2.1961464e-01,
                  2.1746189e-01)

beta_1_time <- c(9.7435188e-01, 6.8629419e-01, 6.5772942e-01, 6.9072532e-01, NA,
                 7.0119969e-01, 6.9709570e-01, 6.9576082e-01, 6.9445402e-01, 6.9850120e-01,
                 6.9681839e-01, 7.2815250e-01, 7.4638895e-01, 7.3267263e-01, 7.4284978e-01,
                 7.4086609e-01, 7.3701197e-01, 7.3802934e-01, 7.4300428e-01, 7.4309692e-01,
                 7.3343073e-01, 7.4187524e-01, 7.3044531e-01, 7.2588079e-01, 7.2618486e-01,
                 7.2795289e-01)

phi_1_time <- alpha_1_time+beta_1_time


dataframe_alpha_1_time <- data.frame(index = c(506,526,546,566,586,
                                               606,626,646,666,686,
                                               706,726,746,766,786,
                                               806,826,846,866,886,
                                               906,926,946,966,986,
                                               1006), values = alpha_1_time)
dataframe_alpha_1_time_melt <- melt(dataframe_alpha_1_time, id = c('index'))

dataframe_beta_1_time <- data.frame(index = c(506,526,546,566,586,
                                              606,626,646,666,686,
                                              706,726,746,766,786,
                                              806,826,846,866,886,
                                              906,926,946,966,986,
                                              1006), values = beta_1_time)
dataframe_beta_1_time_melt <- melt(dataframe_beta_1_time, id = c('index'))

dataframe_phi_1_time <- data.frame(index = c(506,526,546,566,586,
                                             606,626,646,666,686,
                                             706,726,746,766,786,
                                             806,826,846,866,886,
                                             906,926,946,966,986,
                                             1006), values = phi_1_time)
dataframe_phi_1_time_melt <- melt(dataframe_phi_1_time, id = c('index'))
mas2
  • 75
  • 11
  • Does https://stackoverflow.com/q/5293715/3358272 address your problem? – r2evans May 25 '20 at 01:32
  • `ggplot(mtcars, aes(mpg, disp)) + geom_point() + labs(title = expression(alpha[1]), x = expression(beta[0]), y = expression(Gamma[infinity]))` – r2evans May 25 '20 at 01:33

1 Answers1

1

You can follow this example using expression() for labels

library(ggplot2)

ggplot(mtcars, aes(mpg, hp, col = factor(am))) +
    geom_line() +
    scale_color_discrete(labels = c(expression(beta[1]), 
                                    expression(alpha[1] + beta[1])))

enter image description here

Crimc
  • 195
  • 17
  • But how to do so with legends and linetype? – mas2 May 25 '20 at 01:37
  • If you provide the data (`dataframe_alpha_1_time_melt`, `dataframe_beta_1_time_melt`, `dataframe_phi_1_time_melt`) it would be easier to adapt – Crimc May 25 '20 at 01:43
  • I fixed it, but how do I change the color of my highlighted symbols for the legends to black? The dataframe can be found now. – mas2 May 25 '20 at 01:47
  • I'm not sure what you're looking for, how would the desired plot look like? With your actual code you can add the greek letters likes this `pparameters + scale_linetype_discrete(labels = c(expression(alpha[1]), expression(alpha[1] + beta[1]), expression(beta[1])))` – Crimc May 25 '20 at 02:40