R 在ggplot2堆叠条形图中保留缺失的数据

R 在ggplot2堆叠条形图中保留缺失的数据,r,ggplot2,R,Ggplot2,这是我的样本数据。ID 144包含6个位置,而ID AB01仅包含3个位置。在堆叠图中,我仍然希望在AB01中显示6个位置,缺失的位置以特定颜色显示 ID YEAR POS 144 2017 10 144 2017 12 144 2017 18 144 2017 15 144 2017 163 144 2017 200 AB01 2018 10 AB01 2018 15 AB01 2018 18 这就是

这是我的样本数据。ID 144包含6个位置,而ID AB01仅包含3个位置。在堆叠图中,我仍然希望在AB01中显示6个位置,缺失的位置以特定颜色显示

ID   YEAR  POS
    144 2017   10
    144 2017  12
    144 2017  18
    144 2017  15
    144 2017  163
    144 2017 200
    AB01 2018  10
    AB01 2018 15
    AB01 2018 18
这就是我试过的

ggplot(data1, aes(x = ID, y=1, fill = as.factor(POS))) +
 geom_bar(stat = "identity", position = "stack", exclude = NULL) +
 facet_wrap(~ data1$Year, ncol=1, scale="free") +
 labs(x="Year", y= "Number ", fill = "Position", Title= "Pos plot") +
 theme(text = element_text(size = 15, color = "Black"))

资料


data你能用
geom\u tile
代替吗

data <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("144", "AB01"), class = "factor"), YEAR = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L), POS = c(10L, 12L, 18L, 15L, 163L, 200L, 10L, 15L, 18L)), class = "data.frame", row.names = c(NA, -9L))

ggplot(data, aes(x = ID, y = as.factor(POS), fill = as.factor(POS))) +
  geom_tile(color = "black") +
  coord_cartesian(expand = F) + # get rid of space around tiles
  theme_classic() # make background white

data你能用
geom\u tile
代替吗

data <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("144", "AB01"), class = "factor"), YEAR = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L), POS = c(10L, 12L, 18L, 15L, 163L, 200L, 10L, 15L, 18L)), class = "data.frame", row.names = c(NA, -9L))

ggplot(data, aes(x = ID, y = as.factor(POS), fill = as.factor(POS))) +
  geom_tile(color = "black") +
  coord_cartesian(expand = F) + # get rid of space around tiles
  theme_classic() # make background white
data这个怎么样:


数据%
完成(ID,POS)%>%
突变(temp=fct\u explicit\u na(fct\u inseq(temp),na\u level=“缺失”))
col_map这个怎么样:


数据%
完成(ID,POS)%>%
突变(temp=fct\u explicit\u na(fct\u inseq(temp),na\u level=“缺失”))

col_map您可以使用ID列中的AB01将额外的人工条目添加到数据框中,使其达到6。您可以使用
dput
功能将数据复制/粘贴到堆栈中吗?这样,我就可以在我的电脑上试用了。你能给我一张你想要的图形的草图吗?友好的提示,
geom_col(…)
geom_bar(stat=“identity”,…)
@DanielFreeman structure(list)(ID=structure(c(1L,1L,1L,1L,1L,2L,2L,2L),的缩写。Label=c(“144”,“AB01”),class=“系数”),年份=c(2017L,2017L,2017L,2017L,2017L,2018L,2018L,2018L,2018L),位置=c(10L,12L,18L,15L,163L,200L,10L,15L,18L),class=“data.frame”,row.names=c(NA,-9L))您可以使用ID列中的AB01将额外的人工条目添加到数据框中,使其达到6。您可以使用
dput
功能将数据复制/粘贴到堆栈中吗?这样我可以在我的计算机上试用它。您可以提供一个预期图形的草图吗?友好提示
geom\u col(…)
是geom_bar(stat=“identity”,…)的缩写。
@DanielFreeman结构(列表(ID=structure(c(1L,1L,1L,1L,1L,1L,2L,2L,2L),Label=c(“144”,“AB01”),class=“factor”),年份=c(2017L,2017L,2017L,2017L,2018L,2018L,2018L,2018L),POS=c(10L,12L,18L,15L,163L,200L,10L,18L,15L),”data.frame“,row.names=c(NA,-9L))我可以,但我也需要显示数据的年份,这样我就可以使用刻面覆盖,但这将显示该年份的位置,或者根据年份对我的数据进行排序,并绘制一条垂直线来标记2017-2018边界。看起来年份和ID会一起变化。你们能不能将这些列粘贴在一起?
刻面覆盖
将留下奇怪的gapsI,但我也可以需要显示数据的年份,这样我就可以使用刻面换行,但这将显示该年份的位置,或者根据年份对我的数据进行排序,并绘制一条垂直线来标记2017-2018边界。看起来年份和ID是不同的。你们能将这些列粘贴在一起吗?
刻面换行
将保留一个奇怪的间隙,以包括两个之间的空格样本数据中的n个矩形。更新为包含样本数据中矩形之间的空格。
ggplot(data, aes(x = ID, y = as.factor(POS), fill = as.factor(POS))) +
geom_tile(color = "black") + facet_wrap(~ data1$Year, ncol=2, scale="free_x") + 
coord_cartesian(expand = F) + theme(strip.background = element_blank(), strip.text.x = element_blank())