[英]Keep missing data in ggplot2 stacked barplot
This is my sample data.这是我的样本数据。 ID 144 contains 6 positions while ID AB01 contains only 3. In a stacked plot I still want to show 6 positions in AB01 with missing positions shown in a specific color. ID 144 包含 6 个位置,而 ID AB01 仅包含 3 个。在堆叠的 plot 中,我仍然想在 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
This is what I tried.这是我尝试过的。
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数据
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))
Can you use geom_tile
instead?你可以用geom_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
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())
How about this:这个怎么样:
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))
library(ggplot2)
library(forcats)
library(tidyr)
library(dplyr)
data_1 <-
data %>%
mutate(temp = as.character(POS)) %>%
complete(ID, POS) %>%
mutate(temp = fct_explicit_na(fct_inseq(temp), na_level = "Missing"))
col_map <- c("10" = "powderblue",
"12" = "red",
"18" = "orange",
"15" = "yellow",
"163" = "green",
"200" = "blue",
"Missing" = "White")
ggplot(data_1, aes(x = ID, y = fct_rev(factor(POS)), fill = temp)) +
geom_tile(color = "black", width = 0.5, height = 0.8) +
scale_fill_manual(values = col_map)+
coord_cartesian(expand = F) +
labs(x = NULL,
y = NULL,
fill = NULL)+
theme_classic()+
theme(axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size = 14),
axis.line = element_blank())
Created on 2020-07-08 by the reprex package (v0.3.0)由reprex package (v0.3.0) 于 2020 年 7 月 8 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.