首页 文章

从空图中添加图例到ggplot2

提问于
浏览
3

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

  • 两个轴中从-1到1的圆

  • 添加第一个组件和第二个组件与0不同的那些相关性作为箭头

  • 以点为单位添加第一个或第二个分量等于0的相关性

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

  • 第1列:X轴

  • 第2列:Y轴

  • 第3栏:功能(颜色)

UPDATED )我的代码是:

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

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

UPDATE )我想在传说中看到的是解决方案的红色子弹,媒体的绿色子弹,背景的黄色子弹和背景的蓝色子弹( Headers 来自功能,第3列 all ) .

信息

UPDATE )我的 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")

绘制图的示例:

Plot of the two components

1 回答

  • 0

    当我运行你的代码时,我没有得到你正在展示的完全相同的数字,但是如果你运行这个代码,我会得到与运行你的代码但你想要的传奇相同的数字 .

    而不是在绘图之前使用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")
    

    enter image description here

相关问题