【问题标题】:More than six shapes in ggplotggplot中超过六个形状
【发布时间】:2014-12-01 03:54:39
【问题描述】:

我想使用离散颜色绘制具有超过六组数据的不同形状的线条。问题是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)

【问题讨论】:

标签: r colors ggplot2 line shape


【解决方案1】:

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

df$sn <- factor(df$sn)

然后,您需要使用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

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

【讨论】:

  • 这更接近,但它并不完全适用于真实数据。我还需要mod功能,真实数据的层次太多了。似乎 scale_shape_manual(values=(1:nlevels(df$sn))%%Nmax) 有效。
  • 其中 Nmax 是要显示的形状数。
  • 在我包含的第一个链接的最后一个示例中,它告诉您更多关于可用形状的信息。请注意,形状 21-25 是空心的,并采用 fill 参数,因此这是添加不同符号的另一种方式。很好地整理出解决方案。
  • 这正是我想要的。 非常感谢 @Cotton.Rockwood
  • 如果你想保留一个字符串(ggplot 自动使它成为一个因素),然后使用 1:length(unique(df$sn))
【解决方案2】:

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

如果你在scale_shape_manual中加上values,相信你会得到你想要的。我首先将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)

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

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

忽略在一个图形中包含这么多形状是否是个好主意,您可以使用字母和数字以及符号作为形状。因此,如果您想要基于具有 73 个级别的因子的 73 个独特形状,您可以使用 19 个符号、所有大小写字母以及数字 0 和 1 作为您的values

scale_shape_manual(values = c(0:18, letters, LETTERS, "0", "1"))

【讨论】:

  • 感谢您的意见。我认为值要么必须等于行数(序列号),要么等于行因子的数量。实际数据的行数多于符号数,所以它通过字母表,然后我认为没有显示符号。这些值仍然需要一个 mod 函数来缩小到符号范围。
  • @user3969377 哇,sn 有多少级?如果你真的想要这么多不同的形状,如果你使用符号 0:18 以及所有大写和小写字母,你可以获得 71 个独特的形状。如果您开始添加数字和特殊字符,还有更多内容,但不确定您将如何将所有内容都告诉情节中的一部分。
  • 有些人物有400多个级别。我知道这不是一个好情节,我只是在寻找模式......
  • 我怎样才能用整数代替字母而不是字母?我意识到字母和字母是内置在常量中的。感谢您提供有用的答案 - 比我自己得到的要远得多!
  • @gbrlrz017 你可以使用,例如,values = as.character(1:11)。只需确保您要使用的整数在 scale_shape_manual 中以字符形式给出。
【解决方案3】:

如果需要,您可以获得大约一百种不同的形状。 good.shapes 是在我的屏幕上呈现的形状编号的向量,没有任何填充参数。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多