Reputation: 1
I have a weather data set which measure temperatures inside a house and outside. I am trying to set the colors of the lines plotted and reflect those in a legend which indicates which color represents the inside and outside using ggplot2. I have not been able to set the colors as "red" and "blue" as appropriate. How can I achieve that. Below is some artificially generated data plus code.
datime <- seq(as.POSIXct("2021-01-01"), as.POSIXct("2021-01-31"), by="d")
set.seed(5)
tmpout <- round(runif(31, 20, 35), 1)
tmpin <- tmpout + round(runif(31, -6, -5), 1)
temps <- data.frame(datime, tmpout, tmpin)
theme_set(theme_bw())
xlabel <- "Time"
ylabel <- expression(paste("Temperature (", ~degree~C, ")", sep=""))
g1 <- ggplot(aes(x=datime, y=tmpout), data=temps) + geom_line(aes(colour="blue"), size=1) + geom_line(aes(y=tmpin, colour="red"), size=1) + scale_colour_manual(values=c(inside="red", outside="blue")) + scale_color_discrete(name="Location", labels=c("Outside", "Inside"))
g2 <- scale_x_datetime(date_breaks="1 weeks", date_labels="%d %b %Y")
g3 <- g1 + g2 + labs(title="Indoor & Outdoor Temperatures\n800 Mindarie Road Pyap West SA 5333", x="Dates", y=ylabel)
l1 <- theme(legend.position=c(0.05,0.9), legend.key.width=unit(3, "line"), legend.text=element_text(size=14, face="bold"), legend.title=element_text(size=16, face="bold"), legend.title.align=0.5, legend.text.align=0.5, legend.spacing = unit(1, "cm"))
g3 + l1 + theme(axis.text.x=element_text(size=rel(1.4)), axis.title.x=element_text(size=20), axis.text.y=element_text(size=rel(1.4)), axis.title.y=element_text(size=20), plot.title=element_text(size=15, hjust=0.5))
Upvotes: 0
Views: 212
Reputation: 2288
Tony, ggplot works "best" with long data frames. You can certainly add the layers (e.g. different lines like you did), but you will benefit from built-in support (e.g. colors, legends, labels) when supplying this in a long dataframe format.
You can turn your dataframe from wide to long with the tidyr::pivot_longer() function. I assign the colnames to a new variable name "key" and the temperature readings to "temp". (Obviously, you can pick names that make more sense to you.)
I provide the output immediately to the ggplot() call. Have a look at what pivot_longer does and how it helps you to define the ggplot mappings for your aesthetics.
temps %>%
tidyr::pivot_longer(cols = c("tmpout","tmpin")
, names_to = "key"
, values_to = "temp") %>%
ggplot() +
geom_line(aes(x = datime, y = temp, color = key))
This will produce a color legend "out of the box" because ggplot knows how you map each of the variables coming from one dataframe.
Add the layers for title, labs, etc. as you did to "fine-tune" the plot.
If you do not like the "out-of-the-box colors" of ggplot, you can set them with the separately with scale_color_manual().
Upvotes: 0
Reputation: 28676
Try this:
last_plot() + scale_color_manual(values=c("blue", "red"))
Here is one page where this is explained. It's also in the book and the regular documentation.
Upvotes: 1