【问题标题】:Add legend to a ggplot2 from empty plot从空图中将图例添加到 ggplot2
【发布时间】:2016-01-01 18:01:28
【问题描述】:

我正在创建一个相关性的圆形图,我想绘制两种类型的信息:

  1. 两个轴上从 -1 到 1 的圆
  2. 将第一个分量和第二个分量不为 0 的相关性添加为箭头
  3. 将第一个或第二个分量等于 0 的相关性添加为点

用于绘制每个箭头/点的颜色集成在 data.all all:

  • 第 1 列:X 轴
  • 第二列:Y轴
  • 第 3 列:特征(颜色)

(已更新) 我的代码是:

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

现在我想为图中使用的颜色添加一个图例,但我无法添加它。 ¿ 一些想法?

(更新) 我想在图例中看到的是解决方案的红色子弹,媒体的绿色子弹,背景的黄色子弹和背景的蓝色子弹(带有来自 feature 的标签,all 的第 3 列)。

+信息

(更新)我的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")

绘图示例:

【问题讨论】:

  • 我们无法访问“全部”,您希望在您的图例中使用什么?
  • 我用我的alldata.frame 的一部分更新了这个问题,所以你可以使用它。我还解释了我想要的图例的内容:一个彩色项目符号,带有我在图中使用的每种颜色的标签,对应于 all 的第 3 列(名为 feature 的列)。
  • 我可能遗漏了一些东西,但是当我运行您的代码时,我在层中收到此错误(数据 = 数据,映射 = 映射,stat = stat,geom = GeomSegment,:找不到对象'fColor'
  • fColour 是一个颜色向量,每个特征一个。我更新了问题以包含它(前两行)。我还清理了代码,因为我在以前的版本中打错了+。

标签: r plot ggplot2


【解决方案1】:

当我运行您的代码时,我得到的数字与您显示的不完全相同,但如果您运行此代码,我会得到与运行代码时相同的数字,但带有您想要的图例。

而不是在绘图之前使用 aes_string() 子集。

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

【讨论】:

  • 你的情节是正确的。是从all 的片段中得到的。我提出的问题来自完整的all。谢谢你的回答。
猜你喜欢
  • 2018-05-11
  • 2019-08-04
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
相关资源
最近更新 更多