简体   繁体   中英

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
  2. Add those correlations where the first component and the second component are different from 0 as arrows
  3. Add as dots those correlations having the first or the second component equal to 0

The colour used to draw each arrow/dot is integrated in the data.all all :

  • 1st column: X axis
  • 2nd column: Y axis
  • 3th column: feature (colour)

( 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 ).

+info

( UPDATE ) Fragment of my 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.

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")

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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