情节与块(Plot With Blocks)

编程入门 行业动态 更新时间:2024-10-10 04:28:45
情节与块(Plot With Blocks)

我一直在寻找几个小时,但我找不到这样做的功能。

如何生成类似的图

假设我有一个数组x1 = c(2,13,4)和y2=c(5,23,43) 。 我想创建3块高度为2-5,13-23的块...

我该如何处理这个问题? 我希望我可以指出正确的方向,看看内置函数是什么?

I have been searching for hours, but I can't find a function that does this.

How do I generate a plot like

Lets say I have an array x1 = c(2,13,4) and y2=c(5,23,43). I want to create 3 blocks with height from 2-5,13-23...

How would I approach this problem? I'm hoping that I could be pointed in the right direction as to what built-in function to look at?

最满意答案

我没有使用过您的数据,因为您说您正在使用数组,但是您给了我们两个向量。 而且,您向我们展示的数据是重叠的。 这意味着如果您绘制三个条形图,则只能看到两个条形图。

根据您提供的小图片,您需要为每个人或日期绘制三个范围。 使用时间序列,我们通常会看到这样绘制最小值/最大值,标准偏差和当前数据。

诀窍是将系列图表为图层。 第一个系列是范围最大的系列(本例中为米色带)。 在下面的示例中,我首先绘制一个空图,然后添加三层矩形,一个用于米色,一个用于灰色,一个用于红色。

#Create data.frame n=100 df <-data.frame(1:n,runif(n)*10,60+runif(n)*10,25+runif(n)*10,40+runif(n)*10,35-runif(n)*10,35+runif(n)*10) colnames(df) <-c("id","beige.min","beige.max","gray.min","gray.max","red.min","red.max") #Create chart plot(x=df$id,y=NULL,ylim=range(df[,-1]), type="n") #blank chart, ylim is the range of the data rect(df$id-0.5,df[,2],df$id+0.5,df[,3],col="beige", border=FALSE) #first layer rect(df$id-0.5,df[,4],df$id+0.5,df[,5],col="gray", border=FALSE) #second layer rect(df$id-0.5,df[,6],df$id+0.5,df[,7],col="darkred", border=FALSE) #third layer

层

I have not used your data because you say you are working with an array, but you gave us two vectors. Moreover, the data you showed us is overlapping. This means that if you chart three bars, you only see two.

Based on the little image you provided, you have three ranges you want to plot for each individual or date. Using times series, we usually see this to plot the min/max, the standard deviation and the current data.

The trick is to chart the series as layers. The first series is the one with the largest range (the beige band in this example). In the following example, I chart an empty plot first and I add three layers of rectangles, one for beige, one for gray and one for red.

#Create data.frame n=100 df <-data.frame(1:n,runif(n)*10,60+runif(n)*10,25+runif(n)*10,40+runif(n)*10,35-runif(n)*10,35+runif(n)*10) colnames(df) <-c("id","beige.min","beige.max","gray.min","gray.max","red.min","red.max") #Create chart plot(x=df$id,y=NULL,ylim=range(df[,-1]), type="n") #blank chart, ylim is the range of the data rect(df$id-0.5,df[,2],df$id+0.5,df[,3],col="beige", border=FALSE) #first layer rect(df$id-0.5,df[,4],df$id+0.5,df[,5],col="gray", border=FALSE) #second layer rect(df$id-0.5,df[,6],df$id+0.5,df[,7],col="darkred", border=FALSE) #third layer

layers

更多推荐

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

发布评论

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

>www.elefans.com

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