Plotting vectors with different spacing - r

Have a list of vectors with different length (of which first value represents the start and last value represents the end).
A 1.1005 1.1084 1.177 1.2024 1.2085 1.1588 1.0868 1.1584 1.0892
B 1.079 1.1086 1.1741 1.184 1.118 1.1068 1.1655
C 1.1684 1.1693 1.2885 1.326 1.3069 1.2584 1.1163 1.1841
D 1.2462 1.1749 1.2304 1.2039 1.2162 1.2933 1.1998 1.1422
Tried to manually space them in excel to produce this plot in the picture.
http://imgur.com/8Qyen76
Is there a way to plot the aggregate of n (beyond only 4 sets) number of theses values (with std dev) of these values on a graph (with y axis being values in the vectors, x axis being from start to end) using R?

Sets up plot area tehn draws splines with the values at equally spaced intervals on a line by line basis:
plot(NA, xlim=c(0,12), ylim=range(dat[-1],na.rm=TRUE) )
apply( dat[-1],1,function(x) lines(spline( x=seq(0,10,length=length(x[!is.na(x)])+1)[-1],
y=x[!is.na(x)])))
If you need the colors you can just save the values from the spline calls and plot with a for loop.

Related

Barplot/histogram using frequencies in R

Suppose you have two vectors, where the first vector is a vector of values a variable takes, and second is the probability (frequency) of this value. It should look like this:
a <- c(2.1,7.5,5.2,6,5.8)
b <- c(1/8,1/8,1/3,5/24,5/24)
I have a lot of those combinations of vectors. Suppose values in all a vectors are in range from 1 to 10.
What i need is two different graphs:
First is a barplot/histogram of values of a based on frequencies b (so heights of bars are equal to frequencies), and values on x axis include only the ordered values of a,
second is a similar graph, but on x axis should be all possible values of a vectors - so the axis should be from 1 to 10 and the bars should only be above the relevant values of a.
In both cases bars should have same width. My question is: How to plot these graphs?
I really think that what I want is called histogram but before I posted this question I made research on this web, and I became very confused about bar plots and histograms.

Select particular objects/rows from heatmap in R

I have mixed data type that contain numeric and categorical attributes to which I am planning to apply cluster algorithms.
As a first step, I produced a distance matrix using the daisy() function and Gower distance measure. I have displayed the distance matrix using a heatmap and a levelplot function in R.
It seems as if there is strong similarity between some of the objects in my data and I want to check some of the similar/dissimilar objects to satisfy myself that the measure is working well on my data.
How do I select the similar/dissimilar objects from the heatmap and link them to the original data set to be able to evaluate them?
This is how I plot my heatmap using R. IDX is my distance Matrix.
new.palette=colorRampPalette(c("black","yellow","#007FFF","white"),space="rgb")
levelplot(IDX_as[1:ncol(IDX_as),ncol(IDX_as):1],col.regions=new.palette(20))
quartz(width=7,height=6) #make a new quartz window of a given size
par(mar=c(2,3,2,1)) #set the margins of the figures to be smaller than default
layout(matrix(c(1,2),1,2,byrow=TRUE),widths=c(7,1)) #set the layout of the quartz window. This will create two plotting regions, with width ratio of 7 to 1
image(IDX_as[1:ncol(IDX_as),ncol(IDX_as):1],col=new.palette(20),xaxt="n",yaxt="n") #plot a heat map matrix with no tick marks or axis labels
axis(1,at=seq(0,1,length=20),labels=rep("",20)) #draw in tick marks
axis(2,at=seq(0,1,length=20),labels=rep("",20))
#adding a color legend
s=seq(min(IDX_as),max(IDX_as),length=20) #20 values between minimum and maximum values of m
l=matrix(s,ncol=length(s),byrow=TRUE) #coerce it into a horizontal matrix
image(y=s,z=l,col=new.palette(20),ylim=c(min(IDX),max(IDX)),xaxt="n",las=1) #plot a one-column heat map
heatmap(IDX_as,symm=TRUE,col=new.palette(20))

Wireframe plot with small values in R

I have data with very small values between -1 to 1 in X, Y and Z values between -1 to 1 like below
X,Y,Z
-0.858301,-1,1.00916
-0.929151,-1,1.0047
-0.896405,-0.940299,1.00396
-0.960967,-0.944075,1.00035
wireframe(Z~X+Y,data=sol)
Seems wireframe works only with larger values (1, 2, 3...) , How do I plot small values?
wireframe might be use in one of two ways -
With a rectangular data matrix where the values of x and y are implied by the shape of the matrix.
wireframe(matrix(rnorm(100),ncol=5),drape=TRUE)
Or with a dataframe, where the values of x and y are explicit, and here you can use a formula for the relationships between the columns.
df<-expand.grid(x = seq(0,.1,.01), y = seq(0,.1,.01))
df$z<-rnorm(121)
wireframe(z~x*y,data=df,drape=TRUE)
I've found that if you include the line defining the z axis limits, then you can't draw it below 1. But if you take out the defined axis limits, and let R graph it itself, then it works and you can graph small numbers.

R: Grouping data in a plot

I have a time series and I have reduced this serie with a transformation. For example
The original time serie:
T=(12,13,14,20,65,78,85,35)
and transformed one is:
T'=(17.22009 27.96722 111.16376 71.33732)
and now I want to have such a diagram, in its x-axis I have 8 values but for each 2 values one value from T' . I can do sme thing like this in R:
but in the second Plot I want to extend the the diagram on 8 values too
Assuming T' is called Tc in R you fix the lower one by
plot(0:length(Tc)*2, c(Tc, tail(Tc,1)), type="s")
The additional element added by tail is needed for drawing the last segment, from 6 to 8.
Update
If you just want to stretch the second plot to go between 1 and 8, you can do
plot(seq(1, 2*length(Tc), length.out=length(Tc)+1), c(Tc, tail(Tc,1)), type="s")
However, I take it that each value of the second plot corresponds to two values of the upper plot, so perhaps the best way to visualize it then would be
barplot(Tc, width=2, space=0)
lines(seq(Tb)-.5, Tb, type="b", lwd=2)

R code: calls to rnorm for x, y axes in plotting hierarchical clusters

This is a random data set being generated here for understanding and plotting a hierarchical cluster in R. I need to know the logic behind why the difference in the calls to rnorm for the x and y axis of the plot. Why y<-rnorm(12, mean=rep(c(1,2,1) when I would have expected mean=rep(c(1,2,3). Perhaps just the literal translation would help me.
set.seed(1234); par(mar=c(0,0,0,0)) ## par sets parameter mar (sets margin)
x<-rnorm(12, mean=rep(1:3,each=4),sd=0.2) ## repeat the vector 3 times
y<-rnorm(12, mean=rep(c(1,2,1),each=4),sd=0.2) ## ?????
plot(x,y,col="blue",pch=19,cex=2)
text(x+0.05,y+0.05,label=as.character(1:12))
Any help appreciated!
If you run your code, you get graphical output that looks something like this:
You can see that there are three clusters at three distinct mean x values (1, 2 and 3) but only two distinct y values (1 and 2, then 1 again). That's because the code for the y values has mean=rep(c(1,2,1),each=4). i.e. the rnorm function is generating twelve random y values, the first four of which have a mean of 1, the second four of which have a mean of 2 and the third four of which have a mean of 1.

Resources