2

I have read many questions here and in google about legends in ggplot. However, I still can not understand why the following code produces 2 different legends and how to control them seperately.

Basically what I need is to format texts on the one on the left because it produces correct visuals about linetypes and remove the one on the right.

I would really appreciate if you could help me on this and explain me why the latter is even existed and why it is producing wrong visuals for linetypes and why it is the only one that gets affected when I attempt to format the legend.

P.S. Before my attempt to format legend the second one was not even existed.

If they aren't yet, these packages need to be installed.

install.packages(ggplot2)
install.packages(reshape2)
install.packages(scales)

Now the code.

library(ggplot2)
library(reshape2)
library(scales)

data(economics)
dataset <- economics[, c("date", "psavert", "uempmed")]
dataset <- melt(dataset, id = "date")

ch <- ggplot(data=dataset, aes(x=date, y=value, group = variable ))+
  geom_line(aes(linetype=variable, color=variable))+
  scale_linetype_manual(values=c("solid", "longdash"))+
  scale_color_manual(values=c('#005493','#666666'), labels = c("Personal savings rate (%)", "Median 
duration of unemployment (weeks)"))+
  theme(legend.position="bottom" , plot.title = element_text(face = "bold", size = (14), colour = 
"#9E0010"),
    axis.title.y = element_text(size = (12), face = "italic"))+
  labs( title= "Pointless economic plot", y="Value", x=" ", colour= "Indicator")

print(ch)

Image Output Here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66

2 Answers2

2

It looks like adding ,guide=FALSE to your scale_linetype_manual will get this done.

#install.packages(ggplot2)
library(ggplot2)
#install.packages(reshape2)
library(reshape2)
#install.packages(scales)
library(scales)
data(economics)
dataset <- economics[, c("date", "psavert", "uempmed")]
dataset <- melt(dataset, id = "date")

ch <- ggplot(data=dataset, aes(x=date, y=value, group = variable ))+
  geom_line(aes(linetype=variable, color=variable))+
  scale_linetype_manual(values=c("solid", "longdash"), guide = FALSE)+
  scale_color_manual(values=c('#005493','#666666'), labels = c("Personal savings rate (%)", "Median 
                                                               duration of unemployment (weeks)"))+
  theme(legend.position="bottom" , plot.title = element_text(face = "bold", size = (14), colour =   "#9E0010"),
        axis.title.y = element_text(size = (12), face = "italic"))+
  labs( title= "Pointless economic plot", y="Value", x=" ", colour= "Indicator") 


Created on 2019-11-02 by the reprex package (v0.3.0)

phiggins
  • 248
  • 1
  • 7
2

This removes the color legend and adds colors to the linetype legend. It also changes the legend title to My title, makes it larger and red and makes the legend text larger, blue and bold.

ch + 
  guides(color = FALSE, 
         linetype = guide_legend(
                      title = element_text("My title"), 
                      override.aes = list(color = c('#005493', '#666666')))) +
  theme(legend.text = element_text(color="blue", size = 16, face = "bold"),
        legend.title = element_text(color = "red", size = 16))

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341