6

我有以下数据:

Id = paste ("ID-", 1:5, sep = "")
position <- rep(seq (1, 100,10), each = 5)
group = rep (rep(rep (1:5, each = length (Id)), each = length(position)))
yvar <- rnorm (length(position), 0.5, 0.1)
ycat <- c(sample (c("A", "B"), length(yvar), replace = TRUE))
namevar <- rep(Id, length(group)/length(Id))
mydf <- data.frame (namevar, group, position, yvar, ycat)

group是一个刻面变量,position是一个连续的 x 变量。yvar用于填充瓷砖的颜色。ycat是每个图块的文本标签。我想为所有值创建一个空白空间,除了我选择用填充颜色和标签绘制的某些图块。

这是我到目前为止所拥有的:

  ggplot(mydf,aes(y=Id,x=position)) +
      facet_wrap(~group) +     
      geom_tile(aes(fill = yvar),colour = "black") +
      geom_text(aes(label = ycat)) +
      labs(x = NULL,y = NULL)

在此处输入图像描述

我希望情节看起来像这样,除了到处都有空格,例如,30-50 之间的第 1 组和 20-60 之间的第 5 组,有点像这样:

在此处输入图像描述

4

1 回答 1

2

这将生成您的最后一个图,但仅对选定区域进行着色:

ggplot(mydf,aes(y=Id,x=position)) +
  facet_wrap(~group) + 
  geom_blank() +    
  geom_tile(data = subset(mydf,(group == 1 & position >= 30 & position <= 50) | 
                                (group == 5 & position >= 20 & position <= 60)),aes(fill = yvar),colour = "black") +
  geom_text(data = subset(mydf,(group == 1 & position >= 30 & position <= 50) | 
                                (group == 5 & position >= 20 & position <= 60)),aes(label = ycat),size = 3) +
  labs(x = NULL,y = NULL)
于 2012-05-04T14:54:11.453 回答