在雷达情节中加入geom_ribbon

时间:2018-04-24 17:31:44

标签: r ggplot2

我希望制作一个雷达图,误差表示为平均值周围的阴影区域。 Geom_ribbon执行此操作但在第一个和最后一个数据点之间留下间隙。

geom_polygon函数将其换行;什么是丝带的等效解决方案?

数据

> summ

   peptide    target       kd   error
1       C1 Cdc42-GTP   0.4090  0.0442
2       C1 Cdc42-GDP   0.8760  0.1700
3       C1  Rac1-GTP   0.3550  0.0790
4       C1      RhoA   0.4750  0.1520
5       C1      KRas   3.4180  0.5810
6       C1      RalA  44.0100 12.5100
7       P1 Cdc42-GTP   0.0245  0.0086
8       P1 Cdc42-GDP   0.0984  0.0091
9       P1  Rac1-GTP   0.8960  0.1230
10      P1      RhoA   0.1660  0.0522
11      P1      KRas   3.9700  0.6140
12      P1      RalA  37.0400 13.4000
13      P7 Cdc42-GTP   0.0146  0.0034
14      P7 Cdc42-GDP   0.0320  0.0043
15      P7  Rac1-GTP   0.4360  0.1070
16      P7      RhoA 100.0000  0.0000
17      P7      KRas  19.6000  5.6000
18      P7      RalA 100.0000  0.0000

雷达图功能

coord_radar <- function (theta = "x", start = 0, direction = 1) 
{
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x") 
    "y"
  else "x"
  ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
          direction = sign(direction),
          is_linear = function(coord) TRUE)
}

图表

r <- ggplot(summ, aes(x = target, y = 1/kd))
r <- r + scale_x_discrete(limits = c("Cdc42-GTP", "Cdc42-GDP", "Rac1-GTP", "RhoA", "KRas", "RalA"))
r <- r + scale_y_log10(breaks = c(0.1, 1, 10))
r <- r  + geom_polygon(aes(group = peptide, colour = peptide), fill = NA, show.legend = F)
r <- r + geom_line(aes(group = peptide, colour = peptide)) 
r <- r + geom_ribbon(aes(ymin = 1/(kd + error), ymax = 1/(kd - error), fill = peptide, group = peptide, colour = peptide), alpha = 0.4)
r <- r + coord_radar()
r

enter image description here

由于

乔治

2 个答案:

答案 0 :(得分:1)

以下是两个想法:

1 - 使用不同的线型绘制没有填充的错误:

ggplot(df, aes(x = target, y = 1/kd))+
  scale_x_discrete(limits = c("Cdc42-GTP",
                              "Cdc42-GDP",
                              "Rac1-GTP",
                              "RhoA", "KRas",
                              "RalA")) +
  scale_y_log10(breaks = c(0.1, 1, 10)) +
  geom_polygon(aes(group = peptide, colour = peptide), fill = NA, show.legend = F) +
  geom_line(aes(group = peptide, colour = peptide)) +
  coord_radar() +
  geom_polygon(aes(y = 1/(kd + error),
                   color = peptide,
                   group = peptide),
               lty = 2,
               alpha = 0) +
  geom_polygon(aes(y = 1/(kd - error),
                   color = peptide,
                   group = peptide),
               lty = 2,
               alpha = 0)

enter image description here

2 - 创建手动多边形:

library(tidyverse)
df %>%
  mutate(ymin = 1/(kd + error),
         ymax = 1/(kd - error)) %>%
  gather(key, value, 6:7) -> df2


ggplot(df, aes(x = target, y = 1/kd))+
  scale_x_discrete(limits = c("Cdc42-GTP",
                              "Cdc42-GDP",
                              "Rac1-GTP",
                              "RhoA",
                              "KRas",
                              "RalA")) +
  scale_y_log10(breaks = c(0.1, 1, 10)) +
  geom_polygon(aes(group = peptide,
                   colour = peptide),
               fill = NA,
               show.legend = F) +
  geom_line(aes(group = peptide,
                colour = peptide)) +
  coord_radar() +
  geom_polygon(data = df2,
               aes(y = value,
                   fill = peptide,
                   group = peptide),
               alpha = 0.2)

enter image description here

数据:

> dput(df)
structure(list(rowname = 1:18, peptide = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c("C1", "P1", "P7"), class = "factor"), target = structure(c(2L, 
1L, 4L, 6L, 3L, 5L, 2L, 1L, 4L, 6L, 3L, 5L, 2L, 1L, 4L, 6L, 3L, 
5L), .Label = c("Cdc42-GDP", "Cdc42-GTP", "KRas", "Rac1-GTP", 
"RalA", "RhoA"), class = "factor"), kd = c(0.409, 0.876, 0.355, 
0.475, 3.418, 44.01, 0.0245, 0.0984, 0.896, 0.166, 3.97, 37.04, 
0.0146, 0.032, 0.436, 100, 19.6, 100), error = c(0.0442, 0.17, 
0.079, 0.152, 0.581, 12.51, 0.0086, 0.0091, 0.123, 0.0522, 0.614, 
13.4, 0.0034, 0.0043, 0.107, 0, 5.6, 0)), .Names = c("rowname", 
"peptide", "target", "kd", "error"), class = "data.frame", row.names = c(NA, 
-18L))

答案 1 :(得分:1)

您需要绘制带孔的多边形:

datapoly <- data.frame(
  peptide = c(summ$peptide, summ$peptide),
  x = c(summ$target, summ$target),
  y = c(1 / (summ$kd + summ$error), 1 / (summ$kd - summ$error))
)

ggplot(summ, aes(x = target, y = 1/kd)) + 
  scale_x_discrete(limits = c("Cdc42-GTP", "Cdc42-GDP", "Rac1-GTP", "RhoA", "KRas", "RalA")) +
  scale_y_log10(breaks = c(0.1, 1, 10)) +
  geom_polygon(aes(group = peptide, colour = peptide), fill = NA, show.legend = F) +
  geom_polygon(aes(x, y, fill = peptide, group = peptide), datapoly, alpha = 0.4) +
  coord_radar()

enter image description here

或者更普遍地重塑:

library(dplyr)
library(tidyr)
datapoly <- summ %>% 
  mutate(ymin = 1/(kd + error), 
         ymax = 1/(kd - error)) %>% 
  gather(which, y, ymin:ymax)