我一直在寻找几个小时,但我找不到这样做的功能。
如何生成类似的图
假设我有一个数组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 layerI 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更多推荐
发布评论