use*_*377 27 r colors shape line ggplot2
我想使用离散颜色绘制具有六组以上数据的不同形状的线条.问题是1)为线条颜色和形状生成了不同的图例,但应该只有一个带有线条颜色和形状的图例,2)在修正线条颜色图例的标题时,颜色会消失.
t=seq(0,360,20)
for (ip in seq(0,10)) {
if (ip==0) {
df<-data.frame(t=t,y=sin(t*pi/180)+ip/2,sn=ip+100)
} else {
tdf<-data.frame(t=t,y=sin(t*pi/180)+ip/2,sn=ip+100)
df<-rbind(df,tdf)
}
}
head(df)
# No plot
# Error: A continuous variable can not be mapped to shape
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=sn))
gp <- gp + labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# No plot
# Error: A continuous variable can not be mapped to shape (doesn't like integers)
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=as.integer(sn)))
gp <- gp + labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# Gives warning about 6 shapes, and only shows 6 shapes, continous sn colors
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=as.factor(sn)))
gp <- gp + labs(title = "Only shows six shapes, and two legends, need discrete colors",
x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# This is close to what is desired, but correct legend title and combine legends
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=as.factor(sn),shape=as.factor(sn %% 6)))
gp <- gp + labs(title = "Need to combine legends and correct legend title", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# Correct legend title, but now the line color disappears
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=as.factor(sn),shape=as.factor(sn %% 6)))
gp <- gp + labs(title = "Color disappeard, but legend title changed", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
gp <- gp + scale_color_manual("SN",values=as.factor(df$sn))
print(gp)
# Add color and shape in geom_line / geom_point commands,
gp <- ggplot(df,aes(x=t,y=y,group=sn))
gp <- gp + labs(title = "This is close, but legend symbols are wrong", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line(aes(color=as.factor(df$sn)))
gp <- gp + geom_point(color=as.factor(df$sn),shape=as.factor(df$sn %% 6))
gp <- gp + scale_color_manual("SN",values=as.factor(df$sn))
print(gp)
Run Code Online (Sandbox Code Playgroud)
Cot*_*ood 48
首先,转换sn为因子会更容易.
df$sn <- factor(df$sn)
Run Code Online (Sandbox Code Playgroud)
然后,您需要使用scale_shape_manual指定要使用的形状.
gp <- ggplot(df,aes(x=t, y=y, group=sn,color=sn, shape=sn)) +
scale_shape_manual(values=1:nlevels(df$sn)) +
labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude") +
geom_line() +
geom_point(size=3)
gp
Run Code Online (Sandbox Code Playgroud)
这应该给你你想要的.您需要使用,scale_shape_manual因为即使sn作为一个因素,ggplot也只会自动添加最多6个不同的符号.之后,您必须手动指定它们.您可以通过多种方式更改符号.请查看这些页面以获取更多信息:http://sape.inf.usi.ch/quick-reference/ggplot2/shape
http://www.cookbook-r.com/Graphs/Shapes_and_line_types/

对我来说,关于6个形状的错误消息的关键是所说的部分Consider specifying shapes manually..
如果在添加values的scale_shape_manual,我相信你会得到你想要的.我sn首先在数据集中做了一个因素.
df$sn = factor(df$sn)
ggplot(df, aes(x = t, y = y, group = sn, color = sn, shape = sn)) +
geom_point() +
geom_line() +
scale_shape_manual(values = 0:10)
Run Code Online (Sandbox Code Playgroud)
编辑上面的示例显示添加11个符号,示例数据集中的符号数相同.您的注释表明sn变量的唯一值比示例中的值多得多.小心使用一长串数字values,因为并非所有数字都被定义为符号.
忽略在单个图形中是否有这么多形状是个好主意,您可以使用字母和数字以及符号作为形状.因此,如果您想要73个基于73个级别的因子的独特形状,您可以使用19个符号,所有大写和小写字母,以及数字0和1作为您的values.
scale_shape_manual(values = c(0:18, letters, LETTERS, "0", "1"))
Run Code Online (Sandbox Code Playgroud)