ggplot2中带有未绘制变量的图例

时间:2019-12-13 19:36:56

标签: r ggplot2 plot

我有数据,每个点都位于两个质心之间的光谱上。通过为每个质心指定颜色,然后根据每个点在其两个质心之间的位置来设置颜色,我为每个点生成了颜色。我用它来手动指定每个点的颜色,并通过以下方式绘制数据:

lb.plot.dat <- data.frame('UMAP1' = lb.umap$layout[,1], 'UMAP2' = lb.umap$layout[,2],
                          'sample' = as.factor(substr(colnames(lb.vip), 1, 5)), 
                          'fuzzy.class' = color.vect))
p3 <- ggplot(lb.plot.dat, aes(x = UMAP1, y = UMAP2)) + geom_point(aes(color = color.vect)) +
  ggtitle('Fuzzy Classification') + scale_color_identity() 
p3 + facet_grid(cols = vars(sample)) + theme(legend.) + 
  ggsave(filename = 'ref-samps_bcell-vip-model_fuzzy-class.png', height = 8, width = 16)

enter image description here

({color.vect是图中图中每个点的颜色矢量)

我想生成此图的图例,以提供每个质心所用的颜色。我有一个命名向量class.cols,其中包含每个质心使用的颜色,并根据相应的类进行命名。

即使未在出图调用中明确使用矢量,有没有办法将此矢量转换为图的图例?

1 个答案:

答案 0 :(得分:1)

您可以通过设置$scope.toggleSubNav(item) { if (item.subItems) { $scope.states.activeItem = item.title; } } 来打开scale_color_identity()中的图例绘图。您必须在scale函数中指定中断和标签,以便图例正确说明每种颜色代表的含义,而不仅仅是颜色的名称。

guide = "legend"

library(ggplot2)

df <- data.frame(x = 1:3, y = 1:3, color = c("red", "green", "blue"))

# no legend by default
ggplot(df, aes(x, y, color = color)) +
  geom_point() +
  scale_color_identity()

reprex package(v0.3.0)于2019-12-15创建