将图例添加到 ggplot 对象(为什么有两个图例?)

Add legend to ggplot object (why two legends?)

我创建了一个 ggplot2 对象:

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"))

我的问题如下:

当我创建图例时,有一个单独的颜色图例和一个单独的点图例。

如何为 8 个变量中的每一个创建一个图例?

摘自本页的想法:http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels

我编辑了你的代码使数据可见(你的 x 轴限制有问题。注意最后三行。这些命令告诉 ggplot 只创建一个图例。

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")

让我尽量减少您的代码并专注于图例问题。这就是你现在所拥有的。

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

您的数据框 b 具有 variable 作为因素。您在这里以两种方式使用它; variablefactor(variable)。你可以简单地使用 variable for shape in geom_point;使所有 variable 相同。

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

我看到了一些与颜色和其他内容相关的警告消息。你可能想照顾他们。但是,对于传奇来说,这是一种方式。