如何设置GGR绘图中用于不同组的形状?

如何设置GGR绘图中用于不同组的形状?,r,ggplot2,R,Ggplot2,我喜欢有不同的形状,这取决于R中一些变量的值。实际上,是变量的组合。我喜欢给组合一个精确的形状编号(存储为s) 我已经试着生成一个新的变量来求和我想要的形状 #gen some example code c <- c('a', 'a', 'b', 'b') d <- c('firstsecond', 'firstfirst', 'lowerupper', 'lowerlower') e <- c(0.2, 0.3, 0.4, 0.5) f <- c('w', 'v','w

我喜欢有不同的形状,这取决于R中一些变量的值。实际上,是变量的组合。我喜欢给组合一个精确的形状编号(存储为s)

我已经试着生成一个新的变量来求和我想要的形状

#gen some example code
c <- c('a', 'a', 'b', 'b')
d <- c('firstsecond', 'firstfirst', 'lowerupper', 'lowerlower')
e <- c(0.2, 0.3, 0.4, 0.5)
f <- c('w', 'v','w', 'v')
df <- cbind(c,d,e,f)
df<- as.data.frame(df)
df$e <- as.numeric(df$e)
orderd <- rev(c( 'firstfirst', 'firstsecond', 'lowerupper', 'lowerlower' ))
df<- within(df, d <- factor(d, levels=orderd))
#生成一些示例代码

c下面的代码使用预先存在的
c
f
列生成形状映射,而不创建单独的
s
列。 图书馆(GG2)

更新:关于您的评论,要为文本标签设置单独的颜色比例,我们可以使用。请注意,在下面的代码中,我们现在将颜色美学放在它们应用的几何图形中,而不是放在对
ggplot
的主调用中:

library(ggnewscale)

ggplot(df, aes(x = d, y = e, shape=interaction(c,f))) +
  geom_linerange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e), color = f), 
                 size=1) +
  geom_point(aes(size=interaction(c,f), color = f)) +
  facet_wrap(c ~ ., scales = "free", nrow = 5, strip.position = "left") +
  coord_flip() +
  scale_colour_viridis_d(name="Colour Title", begin = 0.75 , end = 0) +
  scale_shape_manual(values=c(0, 1, 17, 18)) +
  scale_size_manual(values=c(5,6,5,7)) +
  new_scale_colour() +
  geom_text(aes(label = f, colour=f), size = 5, vjust=0.4, show.legend=FALSE) +
  scale_colour_manual(values=c("black", "white")) +
  theme_bw() +
  labs(shape="Shape Title") +
  guides(shape=guide_legend(override.aes=list(size=3)),
         size=FALSE)


具有多个颜色比例的另一个选项是。请注意,ggplot并非天生设计为具有给定美学的多个尺度,并且这两个软件包都是实验性的。

您需要在
aes
中指定
形状
(作为
因子
,因为它将与
数值
发生错误),而不是外部边界球,这在美学上是有意义的。但现在它给出了一个奇怪的图像,我还需要添加最后一行:
p您可以将您的问题包含额外的代码,因为在注释中很难读取或格式化如果变量
s
表示您要绘制的形状,请使用
scale\u shape\u identity()
(如果使用此函数,您实际上不想首先将事物变成一个因子,尽管形状通常是这样)。谢谢!但我确实需要4种不同的形状,即使颜色提供了形状尺寸的附加信息!简短的跟进:如果使用我的形状,我有填充和未填充的形状。我可以以某种方式键入吗(如果是“全领形状”(16,17),则“geom_文本”为白色,如果是“空”形状(1,2),则为黑色)?我有一个简短的后续问题:是否有可能以一种方式更改图例,即我有一个绿色三角形和一个代表颜色的紫色圆点?
#plotting it:
library(ggplot2)
    p<- ggplot(df, aes(x = d, y = e, color = f)) +
      geom_pointrange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), shape = s) +
      theme_bw() + 
      facet_wrap(c ~ ., scales = "free", nrow = 5, strip.position = "left") +
      coord_flip() +
      scale_colour_viridis_d(begin = 0.75 , end = 0) +
      geom_text(aes(label = f), colour = "black", size = 2.5, hjust=1.05, vjust=1.2)
    p
#gen some example code
c <- c('a', 'a', 'b', 'b')
d <- c('firstsecond', 'firstfirst', 'lowerupper', 'lowerlower')
e <- c(0.2, 0.3, 0.4, 0.5)
f <- c('w', 'v','w', 'v')
df <- data.frame(c,d,e,f)

orderd <- rev(c( 'firstfirst', 'firstsecond', 'lowerupper', 'lowerlower' ))
df$d <- factor(df$d, levels=orderd)

ggplot(df, aes(x = d, y = e, color = f, shape=interaction(c,f))) +
  geom_linerange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), size=1) +
  geom_point(aes(size=interaction(c,f))) +
  geom_text(aes(label = f), colour="white", size = 5, vjust=0.4) +
  facet_wrap(c ~ ., scales = "free", nrow = 5, strip.position = "left") +
  coord_flip() +
  scale_colour_viridis_d(begin = 0.75 , end = 0) +
  scale_shape_manual(values=15:18) +
  scale_size_manual(values=c(5,6,5,7)) +
  theme_bw() +
  labs(shape="Shape Title", colour="Colour Title") +
  guides(shape=guide_legend(override.aes=list(size=3)),
         colour=guide_legend(override.aes=list(size=3, linetype=0)),
         size=FALSE)
geom_text(aes(label = f), colour = "black", size = 3, nudge_x = -0.2) +
library(ggnewscale)

ggplot(df, aes(x = d, y = e, shape=interaction(c,f))) +
  geom_linerange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e), color = f), 
                 size=1) +
  geom_point(aes(size=interaction(c,f), color = f)) +
  facet_wrap(c ~ ., scales = "free", nrow = 5, strip.position = "left") +
  coord_flip() +
  scale_colour_viridis_d(name="Colour Title", begin = 0.75 , end = 0) +
  scale_shape_manual(values=c(0, 1, 17, 18)) +
  scale_size_manual(values=c(5,6,5,7)) +
  new_scale_colour() +
  geom_text(aes(label = f, colour=f), size = 5, vjust=0.4, show.legend=FALSE) +
  scale_colour_manual(values=c("black", "white")) +
  theme_bw() +
  labs(shape="Shape Title") +
  guides(shape=guide_legend(override.aes=list(size=3)),
         size=FALSE)