R 按主题选项将项目添加到图例

R 按主题选项将项目添加到图例,r,ggplot2,themes,legend,R,Ggplot2,Themes,Legend,我有一个数据帧d,如下所示: d <- data.frame("name" = c("pippo","pluto","paperino"), "id" = c(1,2,3),"count" = c(10,20,30), "pvalue"=c(0.01,0.02,0.05), geneRatio=c(0.5,0.8,0.2), type=c("KEGG","Reactome","Reactome")) 这就是目前的情节 我想在图例中添加数据帧d中包含的“类型”信息。 我想在图例中添加一

我有一个数据帧d,如下所示:

d <- data.frame("name" = c("pippo","pluto","paperino"), 
"id" = c(1,2,3),"count" = c(10,20,30),
"pvalue"=c(0.01,0.02,0.05),
geneRatio=c(0.5,0.8,0.2),
type=c("KEGG","Reactome","Reactome"))
这就是目前的情节

我想在图例中添加数据帧d中包含的“类型”信息。
我想在图例中添加一个新项目,颜色为红色=Reactome,颜色为黑色=KEGG

您可以使用
注释
来完成此操作,但它有点手动

ggplot(data = d, aes(geneRatio, name, size = count, colour = pvalue)) + 
  geom_point() + 
  ggtitle("Significantly Pathways") + 
  xlab("Gene Ratio") + 
  ylab("Pathways")+ 
  theme(axis.text.y = element_text(color=d$type)) +
  annotate("text", x = 0.25, y = 3.5, label = "Reactome", color = "red") +
  annotate("text", x = 0.25, y = 3.4, label = "KEGG", color = "black")

并不是说这是个好主意,但您可以添加一个无意义的几何图形来强制添加指南:

d <- data.frame("name" = c("pippo","pluto","paperino"), 
                "id" = c(1,2,3),
                "count" = c(10,20,30),
                "value"=c(0.01,0.02,0.05),
                geneRatio=c(0.5,0.8,0.2),
                type=c("KEGG","Reactome","Reactome")
                )

library(ggplot2)

ggplot(data = d, aes(geneRatio,name,colour = pvalue)) + 
    geom_point(aes(size=count))+ 
    geom_polygon(aes(geneRatio,name,fill = type)) +
    ggtitle("Significantly Pathways") + 
    xlab("Gene Ratio") + 
    ylab("Pathways") + 
    scale_fill_manual(values = c('Reactome'='red', 'KEGG'='black')) +
    theme(axis.text.y = element_text(color=d$type))

从表面上看似乎不可能。为什么不在Reactome/KEGG上刻面?如何?我想使用独特图形中的所有信息可以尝试使用
myplot+labs(title=“xxx”,subtitle=“yy”,caption=“zzz”)
添加更多信息。
d <- data.frame("name" = c("pippo","pluto","paperino"), 
                "id" = c(1,2,3),
                "count" = c(10,20,30),
                "value"=c(0.01,0.02,0.05),
                geneRatio=c(0.5,0.8,0.2),
                type=c("KEGG","Reactome","Reactome")
                )

library(ggplot2)

ggplot(data = d, aes(geneRatio,name,colour = pvalue)) + 
    geom_point(aes(size=count))+ 
    geom_polygon(aes(geneRatio,name,fill = type)) +
    ggtitle("Significantly Pathways") + 
    xlab("Gene Ratio") + 
    ylab("Pathways") + 
    scale_fill_manual(values = c('Reactome'='red', 'KEGG'='black')) +
    theme(axis.text.y = element_text(color=d$type))
ggplot(data = d, aes(geneRatio,name,colour = pvalue)) + 
    geom_point(aes(size=count)) + 
    ggtitle("Significantly Pathways") + 
    xlab("Gene Ratio") + 
    ylab("Pathways") + 
    facet_grid(type ~ ., scales = 'free_y', switch = 'y')