Hình ảnh cuối cùng trong this blog post.Làm cách nào để tạo biểu đồ thanh "lồng nhau"?
Tôi đã thử tìm kiếm "biểu đồ thanh lồng nhau" và "biểu đồ thanh phân cấp", nhưng chúng có thể không phải là từ cho nó.
Hình ảnh cuối cùng trong this blog post.Làm cách nào để tạo biểu đồ thanh "lồng nhau"?
Tôi đã thử tìm kiếm "biểu đồ thanh lồng nhau" và "biểu đồ thanh phân cấp", nhưng chúng có thể không phải là từ cho nó.
Sử dụng ggplot
và tạo lớp riêng biệt:
library(ggplot2)
set.seed(1)
stupid <- data.frame(
group= LETTERS[1:5],
men = sample(1:10, 5),
women = sample(1:10, 5)
)
# Melt the data and calculate totals
mstupid <- melt(stupid, id.vars="group")
stupidTotal <- ddply(mstupid, .(group), summarize, value=sum(value))
ggplot() +
geom_bar(data=stupidTotal, aes(x=group, y=value), fill="grey50") +
geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
stat="identity", position="dodge") +
theme_bw()
Look cho 'barNest' trong gói plotrix
Sử dụng này:
ggplot() +
geom_bar(data=stupidTotal, aes(x=group, y=value, fill="grey50"), stat="identity") +
geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
stat="identity", position="dodge") +
theme_bw()