Trajectory Tree - r

I'm looking to plot something similar to this in R. Can this be this be done with ggplot or some other package?
Found on the following blog:
http://intelligenttradingtech.blogspot.com/2011/07/pattern-recognition-forward-boxplot.html

Here is how to construct the graph using ggplot2.
I constructed the data manually, by specifying the coordinates of each line start and end position. An improvement would obviously be to automate this using an algorithm. Since this wasn't the question, I didn't attempt to solve this too.
Create the data:
arrowdata <- c(
0, 0, 1, 0,
1, 1, 2, 1,
1, -1, 2, -1,
2, 1.5, 3, 1.5,
2, 0.5, 3, 0.5
)
linesdata <- c(
1, 0, 1, 1,
1, 0, 1, -1,
2, 1, 2, 1.5,
2, 1, 2, 0.5
)
labeldata <- data.frame(
x = c(0.5, 1.5, 2.5),
y = c(0, 1, 1.5),
labels=c("Label 1", "Label2", "Label 3")
)
adat <- as.data.frame(matrix(arrowdata, ncol=4, byrow=TRUE))
ldat <- as.data.frame(matrix(linesdata, ncol=4, byrow=TRUE))
Load the ggplot2 and grid packages, then plot:
library(ggplot2)
library(grid) # For arrow() function
ggplot() +
geom_segment(
data=adat,
aes(x=V1, y=V2, xend=V3, yend=V4),
arrow=arrow(length = unit(0.05, "npc"), type="closed"),
col="blue"
) +
geom_segment(
data=ldat,
aes(x=V1, y=V2, xend=V3, yend=V4),
col="blue"
) +
geom_text(data=labeldata, aes(x, y, label=labels),
size=8, vjust=-0.2, col="blue"
) +
theme_bw() +
opts(
axis.text.x=theme_blank(),
axis.text.y=theme_blank(),
axis.ticks=theme_blank(),
axis.title.x=theme_blank(),
axis.title.y=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank()
) +
coord_cartesian(ylim=c(-1.5, 2)) # Create some additional space for labels

You might find something at http://addictedtor.free.fr/graphiques/ . There's an amazing variety of graphs and charts there. Now, it's easy enough to write a little code using the base plot and graphics::arrow functions that will draw lines between the vertices. E.g.,
arrows(0,1,0,0)
lines(c(1,1),c(-.5,.5))
arrows(1,2,.5,.5)
and so on. Do you have a requirement to size or place the branches based on data, or is this a purely qualitative tree?

Related

label annotation on top of bar using barplot in r

Need to move rotated labels on the top of the bar using barplot. What parameter am I missing here?
code:
df<- data.frame(a=strrep(letters[1:20], 10) , b=runif(20, min=1, max=30))
df<- df[order(df$b, decreasing = TRUE),]
row.names(df)<- df$a
par(mar=c(15, 5, 3, 2)+ 0.2)
x<- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics", col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=-1.25, label, xpd=TRUE, srt=45)
You need to change the position of y-axis in text with a suitable offset. (I have used + 1 here).
x <- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics",
col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=df$b + 1, label, xpd=TRUE, srt=45)

x-y Plot based on 2 column table

I'm really new to R and I'm trying to convert a 2 column table into an xy-Plot.
Here's my .csv:
x [cm];y [cm]
0.5;0
2.6;9
0.5;1
0.6;2
0.7;3
0.8;4
1;5
1.2;6
1.5;7
1.9;8
Now: plot(data$`x [cm]`,data$`y [cm]`, type="b").
However I get this result:
I'm not quite sure why (0.5/y) and (2.6/y) are connected..
What I want is a simple line connecting all the dots since they are representing electric field lines. Is there an easy way of doing that?
Sort your data first:
data <- data[order(data[,1]),]
plot(data[,1], data[,2], type="b", xlab="x [cm]", ylab="y [cm]")
The points are connected like this because the connection is created based on their order in the matrix.
m <- matrix(c(
0.5, 0,
0.5, 1,
0.6, 2,
0.7, 3,
0.8, 4,
1, 5,
1.2, 6,
1.5, 7,
1.9, 8,
2.6, 9), ncol = 2, byrow = TRUE)
colnames(m) <- c("x", "y")
plot(m, type = "b")
Simly regrouping the matrix solves your problem.
You can use
library(ggplot2)
ggplot(data, aes(x=`x [cm]`, y=`y [cm]`)) + geom_point() + geom_line()
Or using base R plot
plot(data$`x [cm]`, data$`y [cm]`,
xlim=range(data$`x [cm]`), ylim=range(data$`y [cm]`),
xlab="x [cm]", ylab="y [cm]")
lines(data$`x [cm]`[order(data$`x [cm]`)], data$`y [cm]`[order(data$`y [cm]`)],
xlim=range(data$`x [cm]`), ylim=range(data$`y [cm]`))

How can I individually change the decimal place of a select few axis labels in ggplot?

I have a simple plot below. I log scaled the x-axis and I want the graph to show 0.1, 1, 10. I can't figure out how to override the default of 0.1, 1.0, 10.0.
Is there a way I could change only two of the x-axis labels?
library(ggplot2)
x <- c(0.1, 1, 10)
y <- c(1, 5, 10)
ggplot()+
geom_point(aes(x,y)) +
scale_x_log10()
You could specify labels and breaks in scale_x_log10
library(ggplot2)
x <- c(0.1, 1, 10)
y <- c(1, 5, 10)
ggplot() + geom_point(aes(x,y)) + scale_x_log10(labels = x, breaks = x)

offsetting the mean on a scatter plot?

the figure wth offset points but mean in the middle
I'm plotting two sets of data on the same plot, distinguishing the two sets by using different pch and by offsetting them. I also want to plot the mean of both sets of data but so far I've only been able to offset the data points, not the means. This is my code
points(jitter(as.numeric(gen$genord)-0.1,0.1),ai$propaiacts, pch=15,col="dimgray",cex=1)
points(jitter(as.numeric(ugen$genord)+0.1,0.1),uai$propuaiacts, pch=6)
s=split(gen$propaiacts,gen$gencode)
points(jitter(sapply(s, mean)+0.5,0.5),pch="__", cex=2)
s=split(ugen$propuaiacts,ugen$gencode)
points(jitter(sapply(s, mean)-0.1,0.1),pch="__", cex=2)
this is the relevant data:
dput(c(gen$genord,gen$propaiacts))
c(3, 1, 2, 3, 3, 1, 1, 2, 1, 2, 1, 2, 13.5986733, 6.6115702,
9.2198582, 0.6001775, 1.0177719, 6.4348071, 10.0849649, 16.5116934,
11.00971, 14.2514897, 4.366077, 7.3884464)
> dput(c(ugen$ugenord,ugen$propuaiacts))
c(3, 1, 2, 3, 3, 1, 1, 2, 1, 2, 1, 2, 1, 9.4512195, 6.3064133,
7.2121554, 0.6486974, 1.0140406, 5.9735066, 10.076442, 12.5423729,
9.6563923, 13.3744272, 4.4930535, 5.3341665, 21.0191083)
using your code and dataset was difficult, so I will use the iris dataset and hopefully it will help you started. As an alternative to your base R, I used ggplot2. I only converted the data from wide to long. And then I just added position = position_dodge(width = 1) to the geom_point() expression. To add the mean for each group (black dot), I summarised the dataset iris_melt. Hope it will help you to get what you want.
iris_melt <- melt(iris, id.vars=c("Species"))
iris_melt_s <- ddply(iris_melt, c("Species", "variable"), summarise,
meanv = mean(value))
iris_melt <- melt(iris, id.vars=c("Species"))
ggplot(data=iris_melt, aes(x=variable, y=value, group=Species, color=Species, shape=Species)) +
geom_point(position = position_dodge(width = 0.5)) +
geom_point(data=iris_melt_s, aes(x=variable, y=meanv, group=Species, color=Species), color="black", position = position_dodge(width = 0.5))
i realised that I could simply specify the number of categories on the x and then shift it. It's a bit manual, but it worked for now. s=split(ai$propaiacts,ai$recallord) points(c(1,2,3)-0.1,sapply(s, mean), pch="__", cex=2)

How to create a rose plot with lines

I am trying to create a rose plot of chromosome data as follows
structure(list(chr = c(11, 11, 11, 12, 12, 12, 13, 13, 13, 14,
16, 16, 18, 2, 2, 20, 20, 3, 4, 4), leftPos = c(17640000, 2880000,
29040000, 19680000, 6120000, 6480000, 14880000, 16200000, 17760000,
13560000, 21240000, 7080000, 10440000, 16800000, 49080000, 12240000,
8280000, 13320000, 12000000, 13560000), Means.x = c(218.523821652113,
256.117545073851, 343.541494875886, 348.237645885147, 426.983644179467,
228.568161732039, 283.269605668063, 440.686146437743, 218.674798674891,
264.556139561699, 232.068688576066, 226.877793789348, 224.282711224934,
215.935347248385, 253.472008896076, 230.683794652539, 305.788038763088,
285.805349707436, 644.897029710454, 485.630136931446), Def.x = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Means.y = c(236.188586172547,
345.958953367193, 250.194040077771, 304.25175004754, 336.629006416052,
221.495167672412, 231.719055660279, 231.252826188427, 334.254524914754,
271.392526335334, 236.848569235568, 261.62635228236, 246.090793604293,
370.773978424351, 242.493276055677, 245.097715487835, 280.225103337613,
370.736474095631, 1014.42590543955, 236.718929160423), Def.y = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("chr",
"leftPos", "Means.x", "Def.x", "Means.y", "Def.y"), row.names = c(NA,
20L), class = "data.frame")
Initially I was pretty pleased with myself because I could get these plots
using this code:
ggplot(ZoutliersM)+
geom_point(aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x)),stat="identity",fill="magenta",size=2,colour="red")+
geom_bar(aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x)),stat="identity",fill="purple",size=1,colour="red")+
ylim(0, 1)+
ggtitle("Shared")+
#geom_hline(aes(yintercept=0))+
coord_polar(theta = "x", start = 0)+
facet_wrap(~ chr)
However I have a problem with using geom_bar as I constantly get the error
position_stack requires constant width: output may be incorrect
and I think the output is incorrect as it doesn't plot all of the points.
So I spent ages searching for an answer but really didn't get much. I think it's an error related to the fact that geom_bar thinks that the bar widths are all different sizes and doesn't like it. I've tried changing to stat='bin' but I don't want a frequency plot, I want to just have a line from the point to the x-axis.
So the question is how can I do this and avoid the geom_bar all together. Is there, for example a way of having vline drawn for each point down to the y=0 point?
Edit
so then I tried this
ggplot(ZoutliersM)+
geom_point(aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x)),stat="identity",fill="magenta",size=2,colour="red")+
geom_vline(xintercept = ZoutliersM$leftPos/1000000, linetype= 1, colour = "#919191")+
ylim(0, 1)+
ggtitle("Shared")+
#geom_hline(aes(yintercept=0))+
coord_polar(theta = "x", start = 0)+
facet_wrap(~ chr)
and I got this:
but now all the vlines are plotted on one graph and then replicated per chromosome. so not working still
Try geom_segment(), which allows you to use two coordinates to specify a line segment: (x,y) and (xend,yend). The (x,y) coordinates are the same as your point, while the (xend,yend) coordinate represent the other end of the line segment. In this case, since we want the line to extend from the point to the x-axis, xend should be the same as x and yend should be 0. I've consolidated all of your aes() variables into one, but everything else not related to geom_segment() I've kept the same:
ggplot(ZoutliersM,aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x),
xend=ZoutliersM$leftPos/1000000,yend=0))+
geom_point(stat="identity",fill="magenta",size=2,colour="red")+
geom_segment(linetype= 1, colour = "#919191")+
ylim(0, 1)+
ggtitle("Shared")+
coord_polar(theta = "x", start = 0)+
facet_wrap(~ chr)

Resources