且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在ggplot2堆叠栏中放置堆叠之间的空间?

更新时间:2023-11-26 13:09:34

这是我能最接近您的示例图的位置.除了您已经排序的内容之外,这并没有什么太大的改进,而只强调灰色背景上的白色条形边框.

This is the closest I could get to your example figure. It is not much of an improvement beyond what you've already sorted but puts less of an emphasis on the white bar borders on the grey background.

library(ggplot2)
p <- ggplot(DF1, aes(x = Rank, y = value, group = variable))
p <- p + geom_bar(stat = "identity", position = "stack", lwd = 1.5,
                  width = 0.5, colour = "white", fill = "black")        
p <- p + theme_classic()
p <- p + theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
p

会产生:

如果要保留灰色背景,则可以准确地找出它是什么灰色阴影,并在删除背景网格的同时使用该颜色作为线条(这不是正确的阴影).

If you want to keep the grey background you can find out exactly what shade of grey it is and use that colour for the line while removing the background grids (this is not the right shade).

p <- ggplot(DF1, aes(x = Rank, y = value))
p <- p + geom_bar(stat = "identity", position = "stack", lwd = 1.5,
                  width = 0.5, colour = "grey", fill = "black")        
p <- p + theme(panel.grid = element_blank())
p

此解决方案的一个问题是将看不到很小的组(例如,当Rank = 4变量F3 = 10时;这个很小的值完全被白色条形轮廓所覆盖).

An issue with this solution is that very small groups will not be seen (e.g., when Rank = 4 variable F3 = 10; this small value is completely covered by the white bar outline).

您的样本数据:

DF1 <- structure(list(Rank = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L), variable = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L), .Label = c("F1", "F2", "F3"), class = "factor"), 
    value = c(500L, 400L, 300L, 200L, 250L, 100L, 155L, 90L, 
    50L, 30L, 100L, 10L)), row.names = c(NA, -12L), .Names = c("Rank", 
"variable", "value"), class = "data.frame")