我试图用堆积区域代表收入的不同组成部分。我的问题是我的变量没有堆叠在最后:
这是我的代码:
p3 <- ggplot(bdd, aes(x, y1))+
geom_area(aes(fill=y1))+
geom_area(data=bdd,aes(x, y2, fill=y2))+
geom_area(data=bdd,aes(x, y3, fill=y3))+
geom_area(data=bdd,aes(x,y4, fill=y4))+
geom_line(data=bdd,aes(x, y5, color="red"))+
theme(panel.background = element_rect(fill = "white",colour = "white",size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.25, linetype = 'solid',colour = "grey"),
panel.border = element_blank(),
panel.grid.minor = element_blank())+
scale_fill_manual(values = wes_palette(n=5, name="Moonrise3"))
print(p3)
我认为这个问题来自我的数据结构,因为我试图代表总收入的每个组成部分都有一列。
[编辑]我的数据样本:
x <- c(0,37,74,111,148,185,222,259,269,333,370,407,444,481,518,555,592,629,666,703)
y1 <- c(762,780,798,816,834,852,870,887,905,923,941,959,977,995,1013,1024,1032,1010,1017,1024)
y2 <- c(rep(272,15),265,255,245,235,225)
y3 <- c(477,448,420,391,363,334,305,277,248,219,191,162,133,105,76,47,19,0,0,0)
y4 <- c(rep(13,17),0,0,0)
bdd <- data.frame(x,y1,y2,y3,y4)
y1应该是“最终”收入,y2 y3 y4组成它的不同数量。
感谢您的帮助,
珂洛艾伊
答案 0 :(得分:0)
我也很难运行你的功能,但可能有一个更通用的解决方案。
require(reshape2)
require(ggplot2)
melted <- melt(data = bdd, id = "x", variable.name = "Type", value.name = "Value")
ggplot(melted, aes(x, y = Value, fill = Type)) + geom_area() +
theme(panel.background = element_rect(fill = "white",colour = "white",size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.25, linetype = 'solid',colour = "grey"),
panel.border = element_blank(),
panel.grid.minor = element_blank())
绘制多个geom_whatever()
图层适用于某些类型的数据,例如geom_density
,geom_line
或geom_point
。但是对于区域图,条形图等,您需要将填充分配给分类变量。