条形组的 R 条形图颜色

编程入门 行业动态 更新时间:2024-10-26 20:21:15
本文介绍了条形组的 R 条形图颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

R 的新手很抱歉,如果这是一个愚蠢的问题.

Fairly new to R so sorry if this is a dumb question.

我想绘制一个包含大量数据的条形图 - 可能是 100 个条形图.

I want to plot a bar chart of a lot of data - maybe 100 bars.

我想使用颜色和间距来突出显示组",所以我可能有前 10 个蓝色条,一个小间隙,接下来的 20 个红色,一个小间隙等等.

I want to use colours and spacing to highlight the "groups", so I might have the first 10 bars in blue, a small gap, the next 20 in red, a small gap and so on.

我可以很好地绘制数据,但是如何以这种方式进行着色和间隙?

I can plot the data fine, but how can I do the colouring and gaps in this way?

推荐答案

这可以通过@Arun 链接中提供的 ggplot2 轻松完成.

This can be done quite easily with ggplot2 as provided in links by @Arun.

使用基本图形来设置条之间的空间,您可以使用参数 space=(在每个条之前设置空间)和参数 col= 将改变函数 中的颜色barplot().

With base graphics to set space between bars you can use argument space= (sets space before each bar) and argument col= will change color in function barplot().

这是一个示例,其中有 20 个条形,每 5 个条形之间有空格.

Here is a example with 20 bars and space between each 5 bars.

df<-sample(1:10,20,replace=T) barplot(df,space=c(0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0), col=rep(c("red","blue","green","yellow"),each=5))

如果每组中的观察数相同,那么您可以将值向量转换为矩阵,然后绘制它(参数 beside=TRUE).在这种情况下,您只需要提供颜色,条形就会自动分组.

If the number of observations in each group is identical then you can convert vector of values to matrix and then plot it (with argument beside=TRUE). In this case you just need to supply colors but bars will be grouped automatically.

df2<-matrix(df,ncol=4) barplot(df2,beside=TRUE,col=rep(c("red","blue","green","yellow"),each=5))

更多推荐

条形组的 R 条形图颜色

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

发布评论

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

>www.elefans.com

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