首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ggplot中有六个以上的形状

ggplot中有六个以上的形状
EN

Stack Overflow用户
提问于 2014-10-07 03:55:01
回答 3查看 34.4K关注 0票数 40

我想用六组以上的数据,用离散的颜色绘制不同形状的线条。问题是1)为线条颜色和形状生成了不同的图例,但应该只有一个具有线条颜色和形状的图例,2)更正线条颜色图例的标题时,颜色消失。

代码语言:javascript
运行
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-07 04:42:17

首先,将sn转换为因子会更容易。

代码语言:javascript
运行
复制
df$sn <- factor(df$sn)

然后,您需要使用scale_shape_manual来指定要使用的形状。

代码语言:javascript
运行
复制
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

这应该会给你想要的东西。您需要使用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/

票数 65
EN

Stack Overflow用户

发布于 2014-10-07 04:30:09

对我来说,关于这6个形状的错误消息的关键是显示Consider specifying shapes manually.的部分。

如果你在scale_shape_manual中添加values,我相信你会得到你想要的。我首先让sn成为数据集中的一个因子。

代码语言:javascript
运行
复制
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)

当我需要记住哪些数字对应哪些形状时,我会使用Cookbook for R site

编辑上面的示例显示了添加11个符号,即示例数据集中相同数量的符号。您的注释表明,与示例中相比,sn变量的唯一值要多得多。在values中使用一长串数字时要小心,因为并非所有数字都定义为符号。

不管在一个图形中有这么多形状是不是一个好主意,你可以使用字母和数字以及符号作为形状。因此,如果你想,比方说,基于一个因子的73个级别的73个独特的形状,你可以使用19个符号,全是大小写字母,数字0和1作为你的values

代码语言:javascript
运行
复制
scale_shape_manual(values = c(0:18, letters, LETTERS, "0", "1"))
票数 13
EN

Stack Overflow用户

发布于 2019-04-27 11:40:06

如果你需要的话,你可以得到大约100种不同的形状。good.shapes是在我的屏幕上渲染的形状数字的向量,没有任何填充参数。

代码语言:javascript
运行
复制
library(ggplot2)
N = 100; M = 1000
good.shapes = c(1:25,33:127)
foo = data.frame( x = rnorm(M), y = rnorm(M), s = factor( sample(1:N, M, replace = TRUE) ) )
ggplot(aes(x,y,shape=s ), data=foo ) +
    scale_shape_manual(values=good.shapes[1:N]) +
        geom_point()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26223857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档