r - 将ggplot2和ggstar中2个图形层的图例与geom_point和geom_star合并

标签 r ggplot2

我花了一整天的时间试图解决这个愚蠢的图形问题,但没有成功......

我有一个包含 10 个人口的 pcoa 数据集,比如说“特殊”(我想将它们显示为实心符号)和 4 个“非特殊”(4 个非实心符号)。

我想要一个图例,其中每个符号与一个物种相关,每种颜色与“特殊”和“非特殊”种群的种群相关。

一个例子


df <- data.frame(Species=sort(rep(letters[1:10], 14)), Locality=sort(rep(LETTERS[1:10], 14)), Axis.1=rnorm(140,0,0.7), Axis.2=rnorm(140,0,0.7))

#NOTE:in the original dataset, 2 population may share the same species, thus the same symbol

special<- ("A","B","C","D",E","F","G","H","I","J") #this must be solid symbol
non_special<-("K","M","N","O") # this must be empty symbols

df.special<–subset(df, Species== special)
df.non.special<–subset(df, Species == non_special)

library(ggplot)
library(ggstar)


#solid symbols for special population
ggplot(data= df.special, aes(x=Axis.1,y=Axis.2, fill= Locality)) + 
      geom_star(aes(starshape=Species), size=2)+
scale_starshape_manual(values=c(1,5,9,15,14,13,11,23,21,28))+

#empty symbols for non special population
geom_point(data= df.non.special, aes(x = Axis.1,  y = Axis.2, shape=Species, color=Locality), size=2)+
scale_shape_manual(name = "Legend", values=c(8,11,13,14))+

#additional style
theme_bw()+ xlab("Axis.1=30%") + 
 ylab("Axis.2=20%")+ 
theme(panel.grid = element_blank(),
legend.title = element_text(hjust = 0.5), axis.text = element_text(size = 15, colour = "black"), 
 axis.title = element_text(size = 10, face = "bold"))


所需的输出应该是一个散点图,其中有一个图例,其中实心符号和空心符号分别表示特殊和非特殊人群,并以不同的物种着色。

因此我需要以某种方式实现 scale_fill_manual 和scale_shape_manual(需要空符号)并将scale_fill_manual 添加到scale_starshape_manual。

提前谢谢

最佳答案

实现您想要的结果的一个选择是

  1. 使用 override.aes guide_legend 的参数设置shape s/starshape s 为 color/fill传说。
  2. 摆脱shapestarshape图例
  3. 去掉颜色图例的图例标题,在我的例子中是第二个或底部图例。
  4. 删除 spacingmargin对于图例,使它们作为一个整体出现。

注意:我稍微调整了您的示例数据和代码,因为它不起作用,例如不包含任何非特殊字符。见下文。

library(ggplot2)
library(ggstar)

ggplot(data = df.special, aes(x = Axis.1, y = Axis.2)) +
  geom_star(aes(starshape = Species, fill = Locality), size = 2) +
  scale_starshape_manual(values = c(1, 5, 9, 15, 14, 13, 11, 23, 21, 28)) +
  geom_point(data = df.non.special, aes(shape = Species, color = Locality), size = 2) +
  scale_shape_manual(values = c(8, 11, 13, 14)) +
  theme_bw() +
  xlab("Axis.1=30%") +
  ylab("Axis.2=20%") +
  theme(
    panel.grid = element_blank(),
    legend.title = element_text(hjust = 0.5), axis.text = element_text(size = 15, colour = "black"),
    axis.title = element_text(size = 10, face = "bold")
  ) +
  guides(
    color = guide_legend(
      override.aes = list(shape = c(8, 11, 13, 14)),
      order = 2
    ),
    shape = "none",
    fill = guide_legend(
      override.aes = list(starshape = c(1, 5, 9, 15, 14, 13, 11, 23, 21, 28)),
      order = 1
    ),
    starshape = "none"
  ) +
  labs(color = NULL) +
  theme(legend.spacing.y = unit(0, "pt"), legend.margin = margin())

数据

df <- data.frame(
  Species = sort(rep(LETTERS[1:14], 10)),
  Locality = sort(rep(letters[1:14], 10)),
  Axis.1 = rnorm(140, 0, 0.7),
  Axis.2 = rnorm(140, 0, 0.7)
)

special <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
non_special <- c("K", "L", "M", "N")

df.special <- subset(df, Species %in% special)
df.non.special <- subset(df, Species %in% non_special)

关于r - 将ggplot2和ggstar中2个图形层的图例与geom_point和geom_star合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74031326/

相关文章:

r - 堆积面积图未正确显示负值

r - 在ggplot中绘制 "known"点的模拟覆盖范围

r - ggplot2 : How to write mean value as text on a plot?

r - 将 color brewer 应用于 ggplot 中的一行

r - 与dplyr小组第一名的差异

R - 矩阵中非对角线元素的最小值、最大值和平均值

r - ggplotly 和 facet_wrap 的 X 轴错误

r - 在 Rstudio 中使用操作包动态标题和 slider

r - 如何按顺序遍历 r data.frame 中的每一行,然后遍历每一列?

r - 具有自由刻度但保持纵横比固定的刻面