如何使用 R 创建分组条形图

编程入门 行业动态 更新时间:2024-10-27 07:17:08
本文介绍了如何使用 R 创建分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 R 创建分组条形图.我尝试了以下代码来创建一个简单的条形图.

x=c(99,9,104,67,86,53,83,29,127,31,179,86,74,80,100,150,68,18,81,47)名称(x)= c(A",C",E",D",G",F",I",H",K",M",L","N","Q","P","S","R","T","W","V","Y")条形图(x)y= c(105673,18140,92426,76776,93974,53470,75155,30700,77847,28863,124602,55703,50160,60685,78693,69581,70846,18285,92789,45728)名称(y)= c(A",C",E",D",G",F",I",H",K",M",L","N","Q","P","S","R","T","W","V","Y")条形图(y)

我必须结合上面两个条形图.我不知道如何组合它们.

我尝试使用 gplot.

require(ggplot2)数据(我的数据)头(我的数据)ggplot(mydata, aes(aminoacid, fill=cut)) + geom_bar(position="dodge") +选择(标题=氨基酸分析")数据帧错误(x = c(2L, 3L, 5L, 4L, 7L, 6L, 9L, 8L, 10L, 12L, 11L, :参数意味着不同的行数:21、228

我也尝试了以下代码.

counts <- table(mydata)barplot(counts, main="氨基酸分析",`xlab="氨基酸代码", col=c("darkblue","re​​d")`legend = rownames(counts), beside=TRUE))barplot.default(counts, main = "氨基酸分析", 中的错误:高度"必须是向量或矩阵

我该如何解决这些错误?

请帮我用 R 创建一个分组的条形图.

解决方案

欢迎来到 SO.

您可能想查看 ggplot2 ,在

# 分组条形图计数 <- 表(mtcars$vs,mtcars$gear)barplot(counts, main="Gears 和 VS 的汽车分布",xlab="齿轮数", col=c("darkblue","re​​d"),图例 = 行名(计数),旁边 = TRUE)

I am trying to create grouped bar plot with R. I tried the following code to create a simple barplot.

x=c(99,9,104,67,86,53,83,29,127,31,179,86,74,80,100,150,68,18,81,47) names(x)= c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y") barplot(x) y= c(105673,18140,92426,76776,93974,53470,75155,30700,77847,28863,124602,55703, 50160,60685,78693,69581,70846,18285,92789,45728) names(y)= c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y") barplot(y)

I have to combine the above two bar plots. I can't figure out how to combine them.

I tried with gplot.

require(ggplot2) data(mydata) head(mydata) ggplot(mydata, aes(aminoacid, fill=cut)) + geom_bar(position="dodge") + opts(title="aminoacid analysis ") Error in data.frame(x = c(2L, 3L, 5L, 4L, 7L, 6L, 9L, 8L, 10L, 12L, 11L, : arguments imply differing number of rows: 21, 228

I tried the following code also.

counts <- table(mydata) barplot(counts, main="amino acid analysis",`xlab="aminoacid codes", col=c("darkblue","red")`legend = rownames(counts), beside=TRUE)) Error in barplot.default(counts, main = "aminoacid analysis", : 'height' must be a vector or a matrix

How can I solve these errors?

Please help me to create a grouped barplot with R.

解决方案

Welcome to SO.

You might want to look at ggplot2 , on Hadley's page you will find detailed examples how to do it. Here's an example:

# if you haven't installed ggplot, if yes leave this line out install.packages("ggplot2") # choose your favorite mirror require(ggplot2) data(diamonds) # check the dataset head(diamonds) # plot it ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") + opts(title="Examplary Grouped Barplot")

What's nice about the ggplot2 package is that you can change the visualization of some parameter (aesthetic,aes) easily. For example you could look into facects or stacked barcharts instead of grouping them. Plus, it's well documented on Hadley's page.

For the sake of completeness, here's also a non ggplot2 example found @quickR

# Grouped Bar Plot counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts), beside=TRUE)

更多推荐

如何使用 R 创建分组条形图

本文发布于:2023-07-18 00:46:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   条形图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!