steve steve - 3 months ago 12
R Question

Ggplot2 - Create a function in order to manually control the line type of several variables

I would like to plot the values of several tables. Each of these tables have a different/unknown number of variables/columns.

I am using the following code in order to plot the data:

library(ggplot2)
library(reshape2)
#data <- read.table("jony.csv", header = TRUE, sep = ";", fill = TRUE)
data <- read.table(text="MONTH;GFDL.ESM2M_HBV_IWW_WFDisi;GFDL.ESM2M_SWIM;GFDL.ESM2M_WaterGAP3;GFDL.ESM2M_HYPE;GFDL.ESM2M_VIC;month_mean;q70
1;853.455161290323;550.116774193548;746.965913978495;469.31688172043;546.64752688172;633.300451612903;452.931661075269
2;1037.55011792453;632.34445754717;805.189285714286;567.411202830189;763.929245283019;761.284861859839;452.931661075269
3;782.714301075269;447.378494623656;561.674193548387;422.475483870968;591.257634408602;561.100021505376;452.931661075269
", header = TRUE, sep = ";", fill = TRUE)
jony <- melt(data, id.vars="MONTH")
p <- ggplot(jony, aes(MONTH,value, col=variable))
p + geom_line(size = 0.1) +
geom_hline(aes(yintercept = 0), linetype="dotted") +
ylab("Runoff [m3/s]") +
xlab("Month") +
theme_bw() +
theme(legend.key = element_blank())+
scale_color_discrete(name='Models GCM_HM') +
ggtitle("Jony")


In order to manually control the colors of the variables "month_mean" and "q70" I have added the following function to the code:

f <- function(x, cols, pal = rainbow) {
stopifnot(names(cols) %in% x)
pal <- pal(length(x)-length(cols))
names(pal) <- setdiff(x, names(cols))
pal <- c(pal, cols)
return(pal)
}
p + scale_color_manual(
values = f(levels(jony$variable), c("month_mean"="black", "q70"="red"))
)


Now I would like to adapt this function in order to control the line type of the two variables "month_mean" and "q70". I have added the following to the code:

f2 <- function(x, cols, lin = lty) {
stopifnot(names(cols) %in% x)
lin <- lin(length(x)-length(cols))
names(lin) <- setdiff(x, names(cols))
lin <- c(lin, cols)
return(lin)
}
p + scale_linetype_manual(
values = f2(levels(jony$variable), c("month_mean"="dotted", "q70"="dashed"))
)


But it is not working and it is even deleting the colors affection done by the first function "f". Does anyone knows why?

Answer

Ok. So as I said lty is a parameter and not a function like rainbow. Therefore you cannot simply generate linetypes with it.

Here is an example that works:

library(ggplot2)
library(reshape2)
#data <- read.table("jony.csv", header = TRUE, sep = ";", fill = TRUE)
data <- read.table(text="MONTH;GFDL.ESM2M_HBV_IWW_WFDisi;GFDL.ESM2M_SWIM;GFDL.ESM2M_WaterGAP3;GFDL.ESM2M_HYPE;GFDL.ESM2M_VIC;month_mean;q70
                   1;853.455161290323;550.116774193548;746.965913978495;469.31688172043;546.64752688172;633.300451612903;452.931661075269
                   2;1037.55011792453;632.34445754717;805.189285714286;567.411202830189;763.929245283019;761.284861859839;452.931661075269
                   3;782.714301075269;447.378494623656;561.674193548387;422.475483870968;591.257634408602;561.100021505376;452.931661075269
                   ", header = TRUE, sep = ";", fill = TRUE)
jony <- melt(data, id.vars="MONTH")
p <- ggplot(jony)

p <- p + geom_line(size = 1, aes(MONTH,value, col=variable, linetype=variable)) +
  geom_hline(aes(yintercept = 0), linetype="dotted") +
  ylab("Runoff [m3/s]") +
  xlab("Month") +
  theme_bw() +
  theme(legend.key = element_blank())+
  #scale_color_discrete(name='Models GCM_HM') +
  ggtitle("Jony")

f <- function(x, cols, pal = rainbow) {
  stopifnot(names(cols) %in% x)
  pal <- pal(length(x)-length(cols))
  names(pal) <- setdiff(x, names(cols))
  pal <- c(pal, cols)
  return(pal)
}
p + scale_color_manual(
  values = f(levels(jony$variable), c("month_mean"="black", "q70"="red"))
)

f2 <- function(x, cols) {
  stopifnot(names(cols) %in% x)
  lin <- 1:(length(x)-length(cols)) # 0:12 are the linetypes available
  names(lin) <- setdiff(x, names(cols))
  lin <- c(lin, cols)
  return(lin)
}
p + scale_linetype_manual(values = as.numeric(f2(levels(jony$variable), c("month_mean"=9, "q70"=9))))

Notice: I gave the variables that are not month_mean or q79 the linetypes 1:5. From this example on, you can come up with your own function to generate linetypes I guess.

enter image description here