我正在尝试为文件栏集成多个绘图,其中每个文件都有两个堆栈条形图,并且绘图很容易混淆,而不会将它们包装成一个单独的网格。但是,我打算改进这个图的结果,为整个图添加常见的图例和标签。我尝试了几个以更清晰的方式为每个文件集成多个绘图,因此将这些放入文件栏的一个网格可以更优雅,更容易理解输出。我对SO
中几个类似帖子的回答感到困惑,对ggplot2
的新帖有些困惑,我最终无法产生我想要的情节。任何人都可以提出以更好的方式改进现有情节的想法吗?如何为多个图形添加通用标签和图例?好吗?
可重复的data.frame:
Qualified <- list(
hotan = data.frame( begin=c(7,13,19,25,31,37,43,49,55,67,79,103,31,49,55,67),
end= c(10,16,22,28,34,40,46,52,58,70,82,106,34,52,58,70),
pos.score=c(11,19,8,2,6,14,25,10,23,28,15,17,6,10,23,28)),
aksu = data.frame( begin=c(12,21,30,39,48,57,66,84,111,30,48,66,84),
end= c(15,24,33,42,51,60,69,87,114,33,51,69,87),
pos.score=c(5,11,15,23,9,13,2,10,16,15,9,2,10)),
korla = data.frame( begin=c(6,14,22,30,38,46,54,62,70,78,6,30,46,70),
end=c(11,19,27,35,43,51,59,67,75,83,11,35,51,75),
pos.score=c(9,16,12,3,20,7,11,13,14,17,9,3,7,14))
)
unQualified <- list(
hotan = data.frame( begin=c(21,33,57,69,81,117,129,177,225,249,333,345,33,81,333),
end= c(26,38,62,74,86,122,134,182,230,254,338,350,38,86,338),
pos.score=c(7,34,29,14,23,20,11,30,19,17,6,4,34,23,6)),
aksu = data.frame( begin=c(13,23,33,43,53,63,73,93,113,123,143,153,183,33,63,143),
end= c(19,29,39,49,59,69,79,99,119,129,149,159,189,39,69,149),
pos.score=c(5,13,32,28,9,11,22,12,23,3,6,8,16,32,11,6)),
korla = data.frame( begin=c(23,34,45,56,67,78,89,122,133,144,166,188,56,89,144),
end=c(31,42,53,64,75,86,97,130,141,152,174,196,64,97,152),
pos.score=c(3,10,19,17,21,8,18,14,4,9,12,22,17,18,9))
)
我正在对数据进行分类,并以这种方式得到多个情节(mainly influenced by @Jake Kaupp's idea
):
multi_plot <- function(x) {
p1 <- ggplot(x, aes(x = group)) +
geom_bar(aes(fill = elm), color = "black")
p2 <- ggplot(distinct(x), aes(x = elm)) +
geom_bar(aes(fill = group), color = "black")
arrangeGrob(p1, p2,nrow = 1, top = unique(x$list))
}
singleDF <-
bind_rows(c(Qualified = Qualified, Unqualified = unQualified), .id = "id") %>%
tidyr::separate(id, c("group", "list")) %>%
mutate(elm = ifelse(pos.score >= 10, "valid", "invalid")) %>%
arrange(list, group, desc(elm))
plot_data <- singleDF %>%
split(.$list) %>%
map(~split_plot(.x))
grid.arrange(grobs = plot_data, nrow = 1)
我正在尝试将文件栏的多个绘图与常用标签和常见图例位置集成。就常见图例而言,我打算将X
轴称为sample
,将Y
轴称为observation
;就常见的传说位置而言,我打算在情节的右侧指出传说(只有四个常见的传说)。
修改:
在我想要的输出图中,group
和elm
的堆栈条形图必须放在文件栏的单个网格中。关于整个图形,需要追求共同的标签和图例。
如何实现所需的输出?在原始实施中需要做出哪些改变?对于SO
中的这个简单问题感到抱歉。提前致谢
答案 0 :(得分:3)
combinedDF <-
bind_rows(mutate(singleDF, x = group, fill = elm),
mutate(singleDF, x = elm, fill = group) %>% distinct()) %>%
mutate(x = factor(x, levels = c('invalid', 'valid', 'Unqualified', 'Qualified')),
fill = factor(fill, levels = c('invalid', 'valid', 'Unqualified', 'Qualified')))
ggplot(combinedDF, aes(x = x, fill = fill)) +
geom_bar() +
geom_text(aes(label = ..count..), stat = 'count', position = 'stack') +
facet_grid(~list)