I want to generate a ggplot by adding different lines on a loop. Each line should have its own color. I have attempted this:
scenario_colors <- c(
"scenario1" = "black",
"scenario2" = "red",
"scenario3" = "green"
)
all_scenario_names <- c("scenario1", "scenario2", "scenario3")
p <- p + scale_color_manual(values = scenario_colors)
for (j in 1:3){
column <- column_names[j]
scenario <- all_scenario_names[j]
print(paste0(column, " ---- ", scenario, " ---- ", scenario_colors[scenario]))
p <- p + geom_line(data=rama_data, aes(y = .data[[column]], group = 1, color=scenario))
}
This however causes an erroneous final plot. All 3 lines are the color of scenario 3.
But if i try to hard-code it it works fine
for (j in 1:3){
column <- column_names[j]
scenario <- all_scenario_names[j]
print(paste0(column, " ---- ", scenario, " ---- ", scenario_colors[scenario]))
if (j == 1){p <- p + geom_line(data=rama_data, aes(y = .data[[column]], group = 1, color="scenario1"))}
else if(j == 2){p <- p + geom_line(data=rama_data, aes(y = .data[[column]], group = 1, color="scenario2"))}
else{p <- p + geom_line(data=rama_data, aes(y = .data[[column]], group = 1, color="scenario3"))}
}
Each line is shown in its corresponding color (black, red and green).
I have been stuck on this piece of code for a couple of days now and can't seem to figure out what is wrong with the first snippet. This second approach is unfeasible since in the future i will need to expand the amount of scenarios. Hardcoding them by hand would not be possible. What is wrong in the first approach?
Extra information: The shape of the data used is like this:
variable1 <- c(runif(5))
variable2 <- c(3*runif(5))
variable3 <- c(5 + runif(5))
period <- c("MM012021", "MM022021", "MM032021", "MM042021", "MM052021")
# Create the data.table
column_names <- c("variable1", "variable2", "variable3")
rama_data <- data.table(period, variable1, variable2, variable3)
I also add the results between both implementations: Thanks!
You can handle this in one ggplot2::geom_line()
call with any number of scenarios by mapping the color
aesthetic to each scenario. To do this, ggplot2
expects the data in long form. As you're using data.table
, let's do it that way.
rama_long <- melt(
rama_data,
id.vars = "period",
variable.name = "scenario"
)[
,
scenario := sub("variable", "", scenario)
][
,
period := as.IDate(
period,
format = "MM%m%d%y"
)
]
Note, I also made your period
into a date. Then we can just supply the scenario
column to the color
aesthetic:
ggplot(rama_long) +
geom_line(
aes(
x = period,
y = value,
color = scenario
),
linewidth = 2
)