简体   繁体   English

从空图中添加图例到ggplot2

[英]Add legend to a ggplot2 from empty plot

I am creating a circular plot of correlations and I want to draw two type of information: 我正在创建一个关联的圆形图,我想绘制两种类型的信息:

  1. A circle from -1 to 1 in both axis 两个轴中从-1到1的圆
  2. Add those correlations where the first component and the second component are different from 0 as arrows 添加第一个组件和第二个组件与0不同的那些相关性作为箭头
  3. Add as dots those correlations having the first or the second component equal to 0 添加具有第一或第二分量等于0的相关性作为点

The colour used to draw each arrow/dot is integrated in the data.all all : 用于绘制每个箭头/点的颜色集成在data.all all

  • 1st column: X axis 第1列:X轴
  • 2nd column: Y axis 第2列:Y轴
  • 3th column: feature (colour) 第3栏: 功能 (颜色)

( UPDATED ) What I code is: 更新 )我的代码是:

## Colors for datasets
fColor <- c("yellow", "blue", "green", "red")
names(fColor) <- c("background", "isolation_p", "media", "solution")
## /

## Filters for 0s
f1 <- all[ , 1] != 0 & all[ , 2] != 0
f2 <- all[ , 1] == 0 | all[ , 2] == 0
## /

## Draw Circle
theta <- c(seq(-pi, pi, length = 50), seq(pi, -pi, length = 50))
circle <- data.frame(xcircle = cos(theta), ycircle = sin(theta))
p <- ggplot2::ggplot(data = circle, ggplot2::aes_string("xcircle", "ycircle")) +
  ggplot2::geom_path(ggplot2::aes_string("xcircle", "ycircle")) +
  ggplot2::geom_hline(yintercept = 0, linetype = "dashed") +
  ggplot2::geom_vline(xintercept = 0, linetype = "dashed")
## /

## Draw arrows
p <- p + ggplot2::geom_segment(data = all[f1, ],
   ggplot2::aes_string(x = 0, y = 0, xend = "x", yend = "y"),
   arrow = grid::arrow(length = grid::unit(0.2, "cm")),
   color = fColor[all$feature[f1]], linetype = "solid")
## /

## Draw points
p <- p + ggplot2::geom_point(data = all[f2, ],
   ggplot2::aes_string("x", "y"),
   color = fColor[all$feature[f2]], shape = 19, size = 2)
## /

## Add axis labels
p <- p + ggplot2::scale_y_continuous("First Component") +
  ggplot2::scale_x_continuous("Second Component")
## /

Now I want to add a legend for the colours used in the plots, but I am not capable of adding it. 现在我想为图中使用的颜色添加图例,但我无法添加它。 ¿Some ideas? ¿有些想法?

( UPDATE ) What I would like to see into the legend is a red-bullet for solution, a green-bullet for media, a yellow-bullet for background and a blue-bullet for background (with the labels from feature , 3th column of all ). 更新 )我想在传说中看到的是解决方案的红色子弹,媒体的绿色子弹,背景的黄色子弹和背景的蓝色子弹(带有标签的功能 ,第3列all )。

+info +信息

( UPDATE ) Fragment of my all data.frame: 更新 )我all data.frame的片段:

all <- read.table(text = "x y   feature
yc01002711  -0.1755657  0.0000000000    background
yc02001111  0.0000000   -0.0006935916   background
yc03001287  0.0000000   -0.3966829792   background
yc04001667  0.0000000   -0.0575593341   background
xx00000091  -0.4205095  0.0000000000    isolation_p
xx00000092  0.0000000   -0.0085758016   isolation_p
sc78    0.03099176  -0.1425365  media
sc88    0.03897109  0.0000000   media
sc09    -0.05278946 0.0000000   media
inm10058    -0.05277376 0.0000000   solution
inm10099    -0.05286904 0.0000000   solution
inm10101    -0.08174610 -0.1315094  solution")

Example for the drawn plot: 绘制图的示例:

两个组成部分的情节

when I run your code I don't get exactly the same figure you are showing but if you run this code I get the same figure I get when I run your code but with a legend as you wanted. 当我运行你的代码时,我没有得到你正在展示的完全相同的数字,但是如果你运行这个代码,我会得到与运行你的代码但你想要的传奇相同的数字。

instead of using aes_string() I subset prior to plotting. 而不是在绘图之前使用aes_string()I子集。

f2_s <- all[f2, ]
p <- p + geom_point(data = f2_s, aes(x, y, color=feature), shape = 19, size = 2)
p
p + scale_color_manual(values=c("yellow", "blue", "green", "red"))
p + scale_color_manual(values=c("yellow", "blue", "green", "red")) + scale_y_continuous("First Component") + scale_x_continuous("Second Component")

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM