Line in R plot should start at a different timepoint - r

I have the following example data set:
date<-c(1,2,3,4,5,6,7,8)
valuex<-c(2,1,2,1,2,3,4,2)
valuey<-c(2,3,4,5,6)
now I plot the date and the valuex variable:
plot(date,valuex,type="l")
now, I want to add a line of the valuey variable, but it should start with the 4th day, so not at the beginning, therefore I add NA values:
valuexmod<-c(rep(NA,3),valuex)
and I add the line with:
lines(date,valuexmod,type="l",col="red")
But this does not work? R ignores the NA values and the valuexmod line starts with the first day, but it should start with th 4th day?

Given that date and valuex have the same length, I am assuming that you have a typo above.
Try this instead:
date <- c(1, 2, 3, 4, 5, 6, 7, 8)
valuex <- c(2, 1, 2, 1, 2, 3, 4, 2)
valuey <- c(2, 3, 4, 5, 6)
valueymod <- c(rep(NA, 3), valuey)
plot(date, valuex, type = "l", ylim = range(c(valuex, valuey)))
lines(date, valueymod, type = "l", col = "red")
Here's the resulting plot:
Related to your question is a point made in help("lines")...
The coordinates can contain NA values. If a point contains NA in either its x or y value, it is omitted from the plot, and lines are not drawn to or from such points. Thus missing values can be used to achieve breaks in lines.

Related

R: Exclude granges overlapped by another range

I would compute the remained part from a query range after excluding another range, is there a way to do it? Thanks.
query <- IRanges(1, 10)
ranges2exclude <- IRanges(c(2, 6), c(3, 7))
The output I want:
IRanges(c(1, 4, 8), c(1, 5, 10))

Linear graphics

I have a dataset in this way:
maximum <- c(10) #for each time
minim <- c(2) #for each time
Quantity c(4, 2, 10, 2, 10, 6, 2)
How can I structure my dataset to create a linear graphic like this? Time is always constant except when you go from 2 to 10 (they are at the same instant)
Using base R plot functions, just create a Time vector (x-axis):
maximum <- c(10) #for each time
minim <- c(2) #for each time
Time <- c(1, 2, 2, 3, 3, 4, 5)
Quantity <-c(4, 2, 10, 2, 10, 6, 2)
plot(Time, Quantity, type = "l", col = "lightblue", xaxt = "n")
abline(h = maximum, col = "darkblue")
abline(h = minim, col = "orange")

How do I manually assign the location of bars along the x-axis using barplot()?

I have a list of means.
means=c(anc3mean,l3mean,anc5mean,l5mean,anczmean,z12mean);means
[1] 0.07025897 0.42328670 0.05697524 0.53915431 0.01893219 0.10878638
I have created a barplot from those means.
bars=barplot(means,ylim=c(0,.8))
I would like to have the "bars" be centered at the values 1, 2, 4, 5, 7, 8 along the x axis.
I have tried the following:
bars=barplot(means,ylim=c(0,.8),at=c(1,2,4,5,7,8))
This does not work however because "at" is not an argument for "barplot()".
Any help would be greatly appreciated.
You could insert "empty" elements
# x position of the means
loc <- c(1, 2, 4, 5, 7, 8)
# Fill with empty elements
hght <- setNames(rep(0, max(loc)), 1:max(loc))
hght[loc] <- means
# Plot
x = barplot(hght, ylim = c(0, 0.8))
axis(1, at = x, labels = names(hght))
If you don't want the x-axis, simply remove the axis(...) line.
Sample data
means <- c(0.07025897, 0.42328670, 0.05697524, 0.53915431, 0.01893219, 0.10878638)

Looping for ggplot how to use the for loop variable i inside the loop

I have a dataframe (what is the dataframe? i,e is not important).
I am using that and plotting some point curves. like below
#EXP <- 3 (example)
#EXP_VEC <- c(1:EXP)
for (i in 1:EXP)
{
gg2_plot[i] <- ggplot(subset(gg2,Ei == EXP_VEC[i] ),aes(x=hours, y=variable, fill = Mi)) + geom_point(aes(fill = Mi,color = Mi),size = 3)
}
As you can see EXP_VEC = c(1,2,3.......) (Depends on user input Ex: if user inputs 2 then EXP_VEC = c(1,2))
Dataframe has Ei = 1,2,3,4,........
Now I have to do the plotting for all these Ei values depending on the user input.
Consider, EXP_VEC=3
now the for loop should produce three plots for Ei = 1 , Ei = 2 and Ei = 3
for this if the for loop I have written works then it would have been done and finished.
But obviously for loop is not working. I cant use aes_string because variable "i" is outside the aes().
Ex: consider the following dataset
dd<-data.frame(
Ei = c(1L, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
Mi = c(1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2),
hours = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3),
variable = c(0.1023488, 0.1254325, 0.1523245, 0.1225425, 0.1452354,
0.1853324, 0.1452369, 0.1241241, 0.0542232, 0.8542154, 0.021542,
0.2541254))
As you can see I have two sets of Ei, I want to plot 1st plot for Ei = 1 and then beside this plot I want to again plot for Ei = 2.
So I thought of saving the plots for Ei=1 and Ei=2 in two separate variables and then using then in some kind of cascade function which I am yet to find out.
How do I do it?
Is there a easy way to do this by just using ggplot without any loop?
If not then how can I call "i" value inside my for loop?
I would do something like this:
plot_exp <-
function(i){
dat <- subset(gg2,Ei == i )
if (nrow(dat) > 0)
ggplot(dat,aes(x=hours, y=variable, fill = Mi)) +
geom_point(aes(color = Mi),size = 3)
}
ll <- lapply(seq_len(EXP), plot_exp)
ll is a list of plot of ggplot objects.

Rename factors in a spineplot with R

Is it possible to rename factor's in a spineplot? The names of my factors are to long, so they overlap.
Thanks for your advices!
Reading the help for spineplot, it is clear that you can pass the parameters yaxlabels and xaxlabels to control the vectors for annotation of the axes.
One useful function is abbreviate which will shorten character strings.
Combining this information with the spineplot example gives:
treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),
labels = c("placebo", "treated"))
improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)),
levels = c(1, 2, 3),
labels = c("none", "some", "marked"))
spineplot(improved ~ treatment, yaxlabels=abbreviate(levels(improved), 2))
Not all of the plot functions in R have this type of parameter. For a more general solution, it might be necessary to rename the factors before passing to a plot function. You can access and modify factor names using the levels function:
levels(treatment) <- abbreviate(levels(treatment), 5)
plot(improved ~ treatment)

Resources