I'm trying to add a legend to my plot using ggplot in R. Everything OK so far. My case is special because I'm trying to deal with three variables, but not in order to draw a 3D plot but draw a 2D plot facing v1 vs. v2 and v1 vs. v3.
I get my plot in a correct way but I dont get the legend.
This is my code:
colfuncWarmest <- colorRampPalette(c("orange","red"))
colfuncColdest <- colorRampPalette(c("green","blue"))
plot <- ggplot(data=temperatures_Celsius, aes(x=temperatures_Celsius$Year))
params <- labs(title=paste("Year vs. (Warmest minimum temperature\n",
"and Coldest minimum temperature)"),
x="Year",
y="Coldest min temp / Warmest min temp")
theme <- theme(plot.title = element_text(hjust = 0.5)) #Centering title
wmtl<-geom_line(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Warmest.Minimum.Temperature..C.,
color="red"
),
colour=colfuncWarmest(length(temperatures_Celsius$Year))
)
wmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Warmest.Minimum.Temperature..C.),
color="green",
method = "loess")
cmtl<- geom_line(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Coldest.Minimum.Temperature..C.,
color="blue"
),
colour=colfuncColdest(length(temperatures_Celsius$Year))
)
cmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=temperatures_Celsius$Coldest.Minimum.Temperature..C.),
color="orange",
method = "loess")
plot + theme + params + wmtl + wmtt + cmtl + cmtt
(Not all code was added because I did a lot of changes. It is only to get an idea) I get this:
If I add
+ scale_color_manual(values=c("red","blue"))
(for example) in order to add the legend, I get no error, but nothing different happens. I get the same plot.
What I want is only two lines. A red one that says "Warmest minimum" and another blue line that says "Coldest minimum". What could I do to get my legend in this way?
Thanks in advance.
Generally I would say that the correct way to apply a legend to a ggplot is to map a variable to an aesthetic (such as fill, color, size, alpha). Usually this consists of transforming the data to long format (key ~ value pair) and mapping the key variable to color or other aestetic.
In the current case this is not desirable since there is next to no chance the color gradient ( colorRampPalette
) on the line could be achieved. So I suggest a hacky way where a dummy layer (layer which will not be seen on the plot) is used to create the legend.
Here is some data
temperatures_Celsius = data.frame(year = 1900:2000,
Warmest = rnorm(100, mean = 20, sd = 5),
Coldest = rnorm(100, mean = 10, sd = 5))
Your plot:
colfuncWarmest <- colorRampPalette(c("orange","red"))
colfuncColdest <- colorRampPalette(c("green","blue"))
plot <- ggplot(data=temperatures_Celsius, aes(x=year))
params <- labs(title=paste("Year vs. (Warmest minimum temperature\n",
"and Coldest minimum temperature)"),
x="Year",
y="Coldest min temp / Warmest min temp")
theme <- theme(plot.title = element_text(hjust = 0.5)) #Centering title
wmtl<-geom_line(data=temperatures_Celsius,
aes(y=Warmest),
colour=colfuncWarmest(length(temperatures_Celsius$year)))
wmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=Warmest),
color="green",
method = "loess")
cmtl<- geom_line(data=temperatures_Celsius,
aes(y=Coldest),
colour=colfuncColdest(length(temperatures_Celsius$year)))
cmtt<-stat_smooth(data=temperatures_Celsius,
aes(y=Coldest),
color="orange",
method = "loess")
plot1 <- plot + theme + params + wmtl + wmtt + cmtl + cmtt
Now add a dummy layer:
plot1+
geom_line(data = data.frame(year = c(1900, 1900),
group = factor(c("Coldest", "Warmest"), levels = c("Warmest", "Coldest")),
value = c(10, 20)), aes(x=year, y = value, color = group), size = 2)+
scale_color_manual(values=c("red","blue"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.