[英]How to create a stacked bar in R?
I am an R beginner. 我是R初学者。 What's the best way to create a stacked bar graph (summing to 100%)? 创建堆积条形图(总计为100%)的最佳方法是什么?
Below is my date sample. 以下是我的约会示例。 I have two variables, would like to use Var 1 for x axis, Var 2 for y axis, and Freq for data. 我有两个变量,想将Var 1用于x轴,将Var 2用于y轴,并将Freq用于数据。
Var1 Var2 Freq
1 1 01 ONE FAMILY HOMES 127
2 2 01 ONE FAMILY HOMES 633
3 3 01 ONE FAMILY HOMES 1859
4 4 01 ONE FAMILY HOMES 4722
5 5 01 ONE FAMILY HOMES 2144
6 1 02 TWO FAMILY HOMES 107
7 2 02 TWO FAMILY HOMES 833
8 3 02 TWO FAMILY HOMES 3503
9 4 02 TWO FAMILY HOMES 2859
10 5 02 TWO FAMILY HOMES 799
I'm not exactly sure what kind of bar chart you have in mind,but ggplot2 is my go-to plotting library for research and exploration. 我不确定您想使用哪种条形图,但是ggplot2是我进行研究和探索的首选绘图库。
I think this is what you need and at the very least should point you in the right direction. 我认为这是您所需要的,至少应该为您指明正确的方向。
Example: 例:
library(ggplot2)
df = data.frame(year = rep(c(1,2,3,4,5), 2),
type = c(rep("ONE FAMILY", 5), rep("TWO FAMILY", 5)),
Freq = c(127, 633, 1859, 4722, 2144, 107, 833, 3503, 2859, 799))
ggplot(df, aes(x = year, y = Freq, fill = type )) +
geom_bar(stat = "identity")
Produces: 产生:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.