I know this question has been asked before, and I've looked at many of the links, but none of them seem to be helping my case. I'm plotting a line graph for 4 lines of different colors. But I can't get the legend to appear.
I've read that I need to put the color attribute in the aes part of the graph. That hasn't been successful either.
I have a data frame of four column, and 1000 rows. Here's a small reproducible example of what my data looks like, and how I'd like to plot it.
library(ggplot2)
vec1 <- c(0.1, 0.2, 0.25, 0.12, 0.3, 0.7, 0.41)
vec2 <- c(0.5, 0.4, 0.3, 0.55, 0.12, 0.12, 0.6)
vec3 <- c(0.01, 0.02, 0.1, 0.5, 0.14, 0.2, 0.5)
vec4 <- c(0.08, 0.1, 0.54, 0.5, 0.1, 0.12, 0.3)
df <- data.frame(vec1, vec2, vec3, vec4)
df_plot <- ggplot() +
geom_line(data = df, color = "black", aes(x = c(1:7), y = df[,1], color =
"black")) +
geom_line(data = df, color = "blue", aes(x = c(1:7), y = df[,2], color =
"blue")) +
geom_line(data = df, color = "green", aes(x = c(1:7), y = df[,3], color =
"green")) +
geom_line(data = df, color = "yellow", aes(x = c(1:7), y = df[,4], color
= "yellow")) +
xlab("x axis") +
ylab("y axis") +
ggtitle("A random plot") +
theme(legend.title = element_text("Four lines"), legend.position =
"right")
(Also, did SO change the process of indenting code? Before, I could just press Ctrl + K to indent the entire block of code. But I can't do that anymore. Ctrl+K puts the cursor in my URL box for some reason)
I'd like ti it print the legend to the right of the graph.