使用 R 中的 ggplot 将图例添加到多线图
在本文中,我们将了解如何使用 R 编程语言中的 ggplot 为多条线图添加图例。
对于包含多个线图的图,如果使用 col 属性,则默认创建图例。对线图外观所做的所有更改也将反映在图例中。
Syntax: ggplot(df, aes(x, y, col=”name of the column to differentiate on the basis of”))
让我们首先想象曲线将如何显示为默认值。
示例:默认绘图
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2","function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line()
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line()+
scale_color_manual(values=c("green","yellow","red","grey"))
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line(size=3)
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values, group=fun, color=fun, linetype=fun))+
geom_line(size=1)+
scale_linetype_manual(values = c("solid","dotted","dashed","twodash"))+
scale_color_manual(values=c("red","green","blue","black"))
输出:
对线条所做的各种更改也将出现在图例中,让我们看看如何:
改变颜色
默认情况下,要更改颜色,将生成上面的图,现在假设我们要手动为线条添加颜色,为此任何给定的函数 - scale_color_manual()、scale_color_brewer() 和 scale_color_grey()。
示例:更改线条的颜色
电阻
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line()+
scale_color_manual(values=c("green","yellow","red","grey"))
输出:
改变线条粗细
为此,将 size 属性与特定值一起使用。这些更改也将反映在图例中。
例子:
电阻
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line(size=3)
输出:
更改线型
可以使用任何给定的函数 scale_linetype_manual() 或默认情况下通过使用 linetype 关键字来更改绘图的线型。
例子:
电阻
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values, group=fun, color=fun, linetype=fun))+
geom_line(size=1)+
scale_linetype_manual(values = c("solid","dotted","dashed","twodash"))+
scale_color_manual(values=c("red","green","blue","black"))
输出: