我想將兩個圖重疊放在另一個之上。這兩個圖共享相同的X軸和Y軸。唯一的區別是第一個圖是一個barplot,第二個是折線圖。合併兩個圖,以便圖例中的着色系列重合
library("ggplot2")
p1 <- ggplot(result_a, aes(x=type,y=as.numeric(num_excluded),fill=as.factor(year),width=.5)) +
geom_bar(position = "stack", stat="identity")+
coord_flip()
p1 <- p1 + guides(fill=guide_legend(title="Legend:")) +
scale_fill_brewer(palette="Set1")
p2 <- ggplot(result_a, aes(x=type,y=as.numeric(total),
group=as.factor(year),color=as.factor(year),
width=.5)) +
geom_line()+geom_point()+
coord_flip()
我怎樣才能把它們放在一起,這樣的線條和酒吧的顏色一致(即在一個給定的傳奇色彩的解釋是指兩條線和酒吧)?
更新:
dput(result_a)
structure(list(year = c(2011, 2011, 2011, 2012, 2012), type = c("AAA", "BBB", "AAA","BBB", "AAA"), num_excluded = c(5, 2, 4, 15, 2), total = c(100,20,500,300,100)))
您可以發佈一個'dput(result_a)'? –
[This](http://stackoverflow.com/questions/40048002/represent-geom-line-and-geom-bar-in-the-same-plot)可能會有所幫助。對於顏色,您應該能夠將它們手動設置爲相同的調色板。 – Haboryme
@MikeyMike:完成。 – FiofanS