是否可以在ggplot2堆叠栏中放置堆叠之间的空间? [英] Is it possible to put space between stacks in ggplot2 stacked bar?

查看:141
本文介绍了是否可以在ggplot2堆叠栏中放置堆叠之间的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处进行了此示例:

DF <- read.table(text="Rank F1     F2     F3
1    500    250    50
2    400    100    30
3    300    155    100
4    200    90     10", header=TRUE)

library(reshape2)
DF1 <- melt(DF, id.var="Rank")

library(ggplot2)
ggplot(DF1, aes(x = Rank, y = value, fill = variable)) + 
geom_bar(stat = "identity")

是否可以使用ggplot2创建一个堆叠的条形,如下图所示?我不想用不同的颜色来区分纸叠.

Is it possible to create a stacked bar such as the following graph using ggplot2? I do not want to differentiate stacks by different colors.

编辑:根据Pascal的评论,

Based on Pascal's comments,

ggplot(DF1, aes(x = Rank, y = value)) + 
geom_bar(stat = "identity",lwd=2, color="white")

酒吧的边框仍然是白色的.

推荐答案

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

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")

这篇关于是否可以在ggplot2堆叠栏中放置堆叠之间的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆