简体   繁体   English

R中的堆积条形图

[英]stacked bar plot in R

在此处输入图片说明

Hello!你好!

I have a table like it:我有一张这样的桌子:

A    B    
1   55
1   43
2   55
1   89
3   55
4   43
4   55

I would like to count for each column the frequency of each categorical value and plot them like in figure.我想为每一列计算每个分类值的频率,并如图所示绘制它们。

How can I do it?我该怎么做?

Thank you in advance先感谢您

I suggest ggplot2 to get your barplot.我建议使用ggplot2来获取您的条形图。

Check out this How to Make a Stacked Bar Chart in R Using ggplot2 .查看如何使用 ggplot2 在 R 中制作堆积条形图

Read the ggplot docs and have a try.阅读 ggplot 文档并尝试一下。

By the way ,it's good for you to read how to ask .顺便说一句,阅读如何提问对您有好处。

Hope this helps.希望这可以帮助。

I do not fully get how your table produces the plot you provide since both columns have equal values, and the plot has 9 vs 6 values.我不完全了解您的表格如何生成您提供的图,因为两列具有相等的值,并且该图有 9 对 6 值。

With R base you could try:使用 R 基础,您可以尝试:

df <- data.frame( A = c(1,1,2,1,3,4,4),
            B = c(55,43,44,89,55,43,55))

df2 <- data.frame(cat = rep(c("A","B"),each=nrow(df)), val = c(df$A,df$B))

barplot(as.matrix(table(df2$val,df2$cat)))

You would need some additional work on the data to get the colors between A and B similar.您需要对数据进行一些额外的工作才能使 A 和 B 之间的颜色相似。 Since A and B have different types of values, the plot now has 6 different colors.由于 A 和 B 具有不同类型的值,因此该图现在有 6 种不同的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM