繁体   English   中英

如何在R中使用ggplot2创建这样的图形?

[英]How to create such a figure using ggplot2 in R?

在此处输入图片说明

我有一个包含许多零元素的矩阵。 列名称在水平轴上标记。 我想明确地显示非零元素作为每列垂直线的偏差。

那么如何使用ggplot2构建一个像示例这样的图形呢?

可以如下生成示例数据:

set.seed(2018)
N <- 5
p <- 40
dat <- matrix(0.0, nrow=p, ncol=N)
dat[2:7,   1] <- 4*rnorm(6)
dat[4:12,  2] <- 2.6*rnorm(9)
dat[25:33, 3] <- 2.1*rnorm(9)
dat[19:26, 4] <- 3.3*rnorm(8)
dat[33:38, 5] <- 2.9*rnorm(6)
colnames(dat) <- letters[1:5]

print(dat)

这是将facet_wrapgeom_coltheme_minimal使用的另一种选择。

library(tidyverse)
dat %>%
    as.data.frame() %>%
    rowid_to_column("row") %>%
    gather(key, value, -row) %>%
    ggplot(aes(x = row, y = value, fill = key)) +
    geom_col() +
    facet_wrap(~ key, ncol = ncol(dat)) +
    coord_flip() +
    theme_minimal()

在此处输入图片说明


为了进一步增加与原始帖子中情节的美学相似性,我们可以

  1. 将小平面条移到底部,
  2. 旋转条形标签,
  3. 在匹配的颜色中添加“零线”,
  4. 删除fill图例,然后
  5. 摆脱x和y轴刻度/标签/标题。

library(tidyverse)
dat %>%
    as.data.frame() %>%
    rowid_to_column("row") %>%
    gather(key, value, -row) %>%
    ggplot(aes(x = row, y = value, fill = key)) +
    geom_col() +
    geom_hline(data = dat %>%
        as.data.frame() %>%
        gather(key, value) %>%
        count(key) %>%
        mutate(y = 0),
        aes(yintercept = y, colour = key), show.legend = F) +
    facet_wrap(~ key, ncol = ncol(dat), strip.position = "bottom") +
    coord_flip() +
    guides(fill = FALSE) +
    theme_minimal() +
    theme(
        strip.text.x = element_text(angle = 45),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

在此处输入图片说明

如果您可以提供一些示例数据,将会更加容易。 因此,我需要创建它们,并且不能保证这会满足您的目的。

set.seed(123)

# creating some random sample data
df <- data.frame(id = rep(1:100, each = 3),
                 x = rnorm(300),
                 group = rep(letters[1:3], each = 100),
                 bias = sample(0:1, 300, replace = T, prob = c(0.7, 0.3)))

# introducing bias
df$bias <- df$bias*rnorm(nrow(df))              

# calculate lower/upper bias for errorbar
df$biaslow <- apply(data.frame(df$bias), 1, function(x){min(0, x)})
df$biasupp <- apply(data.frame(df$bias), 1, function(x){max(0, x)})

然后,我使用了一种技巧,能够以足够的距离打印组,以使它们不会重叠。 基于第I组,偏移偏差变量以及上下偏差都发生了偏移。

# I want to print groups in sufficient distance
df$bias <- as.numeric(df$group)*5 + df$bias
df$biaslow <- as.numeric(df$group)*5 + df$biaslow
df$biasupp <- as.numeric(df$group)*5 + df$biasupp

现在可以绘制它:

library(ggplot2)

ggplot(df, aes(x = x, col = group)) + 
  geom_errorbar(aes(ymin = biaslow, ymax = biasupp), width = 0) + 
  coord_flip() + 
  geom_hline(aes(yintercept = 5, col = "a")) +
  geom_hline(aes(yintercept = 10, col = "b")) +
  geom_hline(aes(yintercept = 15, col = "c")) +
  theme(legend.position = "none") + 
  scale_y_continuous(breaks = c(5, 10, 15), labels = letters[1:3])

在此处输入图片说明

编辑:

要合并特殊设计,您可以添加

theme_bw() + 
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) 

到你的情节。

在此处输入图片说明

编辑2:

要合并多条水平线,可以创建不同的数据集:

df2 <- data.frame(int = unique(as.numeric(df$group)*5), 
                  gr = levels(df$group))

并使用

geom_hline(data = df2, aes(yintercept = int, col = gr))

而不是为每个组级别复制/粘贴geom_hline

暂无
暂无

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

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