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

时间:2018-08-20 08:17:48

标签: r ggplot2

enter image description here

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

那么如何使用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)

2 个答案:

答案 0 :(得分:4)

这是将. search renvars, historical Search of official help files, FAQs, Examples, SJs, and STBs SJ-5-4 dm88_1 . . . . . . . . . . . . . . . . . Software update for renvars (help renvars if installed) . . . . . . . . . N. J. Cox and J. Weesie Q4/05 SJ 5(4):607 trimend() option added and help file updated STB-60 dm88 . . . . . . . . Renaming variables, multiply and systematically (help renvars if installed) . . . . . . . . . N. J. Cox and J. Weesie 3/01 pp.4--6; STB Reprints Vol 10, pp.41--44 renames variables by changing prefixes, postfixes, substrings, or as specified by a user supplied rule facet_wrapgeom_col一起使用的另一种选择。

theme_minimal

.pdf file


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

  1. 将小平面条移到底部,
  2. 旋转条形标签,
  3. 以匹配的颜色添加“零线”,
  4. 删除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() 图例,然后
  5. 摆脱x和y轴刻度/标签/标题。

fill

enter image description here

答案 1 :(得分:3)

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

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

enter image description here

编辑:

要加入特殊设计,您可以添加

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

到您的情节。

enter image description here

EDIT2:

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

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