构建带有离散变量轮廓的面积图(即带有步骤)
Constructing an area plot with outlines for discrete variable (i.e. with steps)
与geom_area plot with areas and outlines ggplot类似,我正在尝试构建带有轮廓的堆积面积图。由于我的变量是离散的,所以我使用 geom_bar() 来堆叠它们。代码如下:
require(ggplot2)
require(reshape)
x = 0:4
y1 = c(3,2,2,1,0)
y2 = c(1,1,0,0,0)
data = data.frame(x,y1,y2)
data.plot <-melt(data, id.vars = "x")
cols = c(y1="darkgrey",y2="lightgrey")
p = ggplot(data.plot,aes(x=x,y=value,fill=variable))
p + geom_bar(aes(width=1),stat = "identity") + theme_bw() + scale_fill_manual(values=cols)
这给出了
我现在的问题是按照我提到的示例添加轮廓。我可以在 geom_bar()
中使用 colour="black"
但这会在条形之间添加垂直线,看起来很难看。
有没有人建议获取这些大纲?解决方案不必基于 geom_bar
.
如果可能的话,我也对只有深灰色部分有轮廓的解决方案感兴趣,因为这个轮廓有重要的解释。或许这可能是基于 geom_line()
的某个转移版本?
你的绘图代码(我不想使用 c
因为那是一个函数):
p <- ggplot(data.plot, aes(x = x, y = value, fill = variable))
p <- p + geom_bar(aes(width = 1), stat = "identity") + theme_bw() + scale_fill_manual(values = cols)
现在沿着条形图添加一条步进线:
p <- p + geom_step(aes(x = x - 0.5), position = "stack")
沿着轴固定一条线需要更多的工作:
library (dplyr)
y.max <- data.plot %>% group_by(x) %>% summarize(s = sum(value))
y.max <- max(y.max$s)
p + geom_step(aes(x = x - 0.5, ymax = value), position = "stack") +
annotate('segment',
x = min(data.plot$x) - 0.5,
xend = min(data.plot$x) - 0.5,
y = 0,
yend = y.max) +
annotate('segment',
x = min(data.plot$x) - 0.5,
xend = max(data.plot$x) - 0.5,
y = 0,
yend = 0)
我会对更简单的解决方案感兴趣!
这是另一种方法,使用 annotate("path")
。这个建议有一些路径组件的硬编码值,但我怀疑有一种方法可以通过算法填充这些值(可能使用 gg_build()
.
p <- ggplot(data.plot,aes(x=x, y=value, fill=variable))
p <- p + geom_bar(aes(width=1), stat = "identity") + theme_bw() + scale_fill_manual(values=cols)
p <- p + annotate(x=c(-.5, 0.5, 0.5, 2.5, 2.5, 3.5, 3.5),
y=c(3, 3, 2, 2, 1, 1, 0 ), group = 1, "path", color = "black", size = 2)
p <- p + annotate(x=c(min(x)-.5, min(x)+0.5, min(x)+0.5, min(x)+2.5, min(x)+2.5, min(x)+3.5, min(x)+3.5),
y=c(max(value), max(value), max(value)- 1, max(value)- 1, max(value)- 2, max(value)- 2, min(value)), group = 1, "path", color = "black", size = 2)
p
与geom_area plot with areas and outlines ggplot类似,我正在尝试构建带有轮廓的堆积面积图。由于我的变量是离散的,所以我使用 geom_bar() 来堆叠它们。代码如下:
require(ggplot2)
require(reshape)
x = 0:4
y1 = c(3,2,2,1,0)
y2 = c(1,1,0,0,0)
data = data.frame(x,y1,y2)
data.plot <-melt(data, id.vars = "x")
cols = c(y1="darkgrey",y2="lightgrey")
p = ggplot(data.plot,aes(x=x,y=value,fill=variable))
p + geom_bar(aes(width=1),stat = "identity") + theme_bw() + scale_fill_manual(values=cols)
这给出了
我现在的问题是按照我提到的示例添加轮廓。我可以在 geom_bar()
中使用 colour="black"
但这会在条形之间添加垂直线,看起来很难看。
有没有人建议获取这些大纲?解决方案不必基于 geom_bar
.
如果可能的话,我也对只有深灰色部分有轮廓的解决方案感兴趣,因为这个轮廓有重要的解释。或许这可能是基于 geom_line()
的某个转移版本?
你的绘图代码(我不想使用 c
因为那是一个函数):
p <- ggplot(data.plot, aes(x = x, y = value, fill = variable))
p <- p + geom_bar(aes(width = 1), stat = "identity") + theme_bw() + scale_fill_manual(values = cols)
现在沿着条形图添加一条步进线:
p <- p + geom_step(aes(x = x - 0.5), position = "stack")
沿着轴固定一条线需要更多的工作:
library (dplyr)
y.max <- data.plot %>% group_by(x) %>% summarize(s = sum(value))
y.max <- max(y.max$s)
p + geom_step(aes(x = x - 0.5, ymax = value), position = "stack") +
annotate('segment',
x = min(data.plot$x) - 0.5,
xend = min(data.plot$x) - 0.5,
y = 0,
yend = y.max) +
annotate('segment',
x = min(data.plot$x) - 0.5,
xend = max(data.plot$x) - 0.5,
y = 0,
yend = 0)
我会对更简单的解决方案感兴趣!
这是另一种方法,使用 annotate("path")
。这个建议有一些路径组件的硬编码值,但我怀疑有一种方法可以通过算法填充这些值(可能使用 gg_build()
.
p <- ggplot(data.plot,aes(x=x, y=value, fill=variable))
p <- p + geom_bar(aes(width=1), stat = "identity") + theme_bw() + scale_fill_manual(values=cols)
p <- p + annotate(x=c(-.5, 0.5, 0.5, 2.5, 2.5, 3.5, 3.5),
y=c(3, 3, 2, 2, 1, 1, 0 ), group = 1, "path", color = "black", size = 2)
p <- p + annotate(x=c(min(x)-.5, min(x)+0.5, min(x)+0.5, min(x)+2.5, min(x)+2.5, min(x)+3.5, min(x)+3.5),
y=c(max(value), max(value), max(value)- 1, max(value)- 1, max(value)- 2, max(value)- 2, min(value)), group = 1, "path", color = "black", size = 2)
p