user1723765
user1723765

Reputation: 6409

Add legend to ggplot object (why two legends?)

I created a ggplot2 object:

a <- replicate(8,rnorm(100))
colnames(a) <- letters[1:8]
b < -melt(a,id.vars=1:1)
colnames(b) <- c("c","variable","value")

ggplot(b,aes(x = c,y = value, colour = variable, linetype = variable)) + 
geom_line()+
geom_point(aes(shape = factor(variable)), size = 1.7) +
scale_x_continuous(limits = c(-1, 1),
                   breaks = seq(-1, 1, 0.1),
                   expand=c(0.01, 0.01)) +
scale_y_continuous(limits = c(-1, 1),
                   breaks = seq(-1, 1, 0.1),
                   expand = c(0.01, 0.01))+ 
theme_bw(base_size = 12, base_family = "Helvetica") +
theme(axis.text=element_text(size = 10),
      axis.title=element_text(size = 10),
      text = element_text(size = 10),
      axis.line = element_line(size = 0.25),
      axis.ticks=element_line(size = 0.25),
      panel.grid.major = element_blank(),
     #panel.grid.minor = element_blank(),
      panel.border = element_rect(colour = "black", fill = NA, size = 0.5),
      panel.background = element_blank(),
      legend.position = "top" ,
      legend.direction = "vertical", 
      legend.title = element_blank(),
      legend.text = element_text(size = 13),           
      legend.background = element_blank(), 
      legend.key = element_blank()) +
labs(x = '', y = '', title = "") +
theme(plot.title = element_text(size=10)) +
theme(strip.text.x = element_text(size = 8,color="black"),
      strip.background = element_blank()) +
theme(strip.text.x = element_text(size = 8, colour = "black"))

My problem is the following:

when I create the legend, there is a separate legend for the colors and a separate one for the points.

How can I create a single legend for each of the 8 variables?

Upvotes: 1

Views: 626

Answers (2)

jazzurro
jazzurro

Reputation: 23574

Let me minimise your code and focus on the legend issue. This is what you have now.

ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) + 
geom_line() +
geom_point(aes(shape = factor(variable)),size=1.7)

enter image description here Your data frame, b has variable as factor. You use this in two ways here; variable and factor(variable). You can simply use variable for shape in geom_point; make all variable identical.

ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) + 
geom_line()+ 
geom_point(aes(shape = variable),size = 1.7)

enter image description here

I saw some warning messages related to colours and other things. You may want to take care of them. But, for legend, this is one way to go.

Upvotes: 2

Cyrus
Cyrus

Reputation: 131

Take from the ideas on this page: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels

I edited your code to make the data visible (you had problems with your x-axis limits. Note the final three lines. These commands tell ggplot to create only one legend.

a<-replicate(6,rnorm(100))
colnames(a)<-letters[1:6]
b<-melt(a,id.vars=1:1)
colnames(b)<-c("c","variable","value")

ggplot(b,aes(x=c,y=value,colour=variable,linetype=variable)) + 
geom_line() + geom_point(aes(shape=factor(variable)),size=1.7)+
scale_x_continuous(limits=c(0,100))+
scale_y_continuous(limits=c(-2,2),breaks=seq(-2,2,0.1),expand=c(0.01,0.01))+ 
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=10),
      axis.title=element_text(size=10),
      text = element_text(size=10),
      axis.line = element_line(size=0.25),
      axis.ticks=element_line(size=0.25),
      panel.grid.major = element_blank(),
      #panel.grid.minor = element_blank(),
      panel.border = element_rect(colour="black",fill=NA,size=0.5),
      panel.background = element_blank(),
      legend.position="top" ,
      legend.direction="vertical", 
      legend.title=element_blank(),
      legend.text=element_text(size=13),           
      legend.background=element_blank(), 
      legend.key=element_blank())+
labs(x='', y='',title="")+
theme(plot.title=element_text(size=10))+
theme(strip.text.x = element_text(size = 8,color="black"),strip.background=element_blank())+
theme(strip.text.x = element_text(size = 8,color="black"))+
scale_colour_discrete(name  ="Factor")+
scale_linetype_discrete(name  ="Factor") +
scale_shape_discrete(name  ="Factor")

Upvotes: 0

Related Questions