How to create a rose plot with lines - r

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)

Related

Stacked bar plot in R with the positive and negative values

I would like to plot a stacked bar plot in R and my data looks as such:
This table is the values against date and as it can be seen, there are repetitive dates with different sides. I would like to plot a bar plot using this data.
combined = rbind(x,y)
combined = combined[order(combined$Group.1),]
barplot(combined$x,main=paste("x vs y Breakdown",Sys.time()),names.arg = combined$Group.1,horiz = TRUE,las=2,xlim=c(-30,30),col = 'blue',beside = True)
Want a stacked plot where I can see the values against dates. How do change my code?
You can easily create this figure with ggplot2.
Here a piece of code for you using a data frame similar to what you have:
library(ggplot2)
my_data <- data.frame(
date = factor(c(1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8)),
x = c(-2, 14, -8, -13, 3, -4, 9, 8, 3, -4, 8, -1)
)
g <- ggplot(my_data, aes(x = date, y = x)) +
geom_bar(
stat = "identity", position = position_stack(),
color = "white", fill = "lightblue"
) +
coord_flip()
This is the output:
Obviously, the official documentation is a good way to start to understand a bit better how to improve it.

How to make the length of y axis longer in histogram in R

I have used the following code to generate the histogram. However, I want to make the y axis longer since it is too short, may I know how to do it?
> h<-hist(data, breaks=c(0,1, 2, 3, 4), col="grey",xlim = c(0,4), xlab="",xaxt="n",main="")
> axis(side=1, at=0.5:3.5, labels=c(1,2,3,4))
You could use ylim parameter to hist:
hist(mtcars$mpg, ylim=c(0,15))
c(0,15) gives the range to y axis.
A graph will fill whole available area (i.e. window). You could limit that by using layout.
layout(matrix(c(1, 0, 0, 1, 0, 0, 1, 2, 3), 3, 3, byrow = TRUE))
hist(mtcars$mpg, ylim=c(0,15))

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 remove only the left axis labels in R cdplot?

The attached example produces a conditional density plot. In the example, I can use ylab = NA to remove the "x" axis label and if I use yaxlabels = "n" both the left and right tick labels are removed.
How can I suppress only the left labels (i.e. "0" and "1") and keep the right-hand probability scale?
# Example
Forest = c(1,0,1,1,1,0,1,1,0,1)
change = c(-1, 3, 1, 4, 1, 1, -1, 1, -1, 6)
mydata = data.frame(Forest,change)
x = factor(mydata$Forest)
cdplot(x~mydata$change, ylab = NA, xlab = NA)
As you said, you can use yaxlabels = "n" to remove the y-labels on both sides.
Than just add the right side axis back via axis(4).
Forest = c(1,0,1,1,1,0,1,1,0,1)
change = c(-1, 3, 1, 4, 1, 1, -1, 1, -1, 6)
mydata = data.frame(Forest,change)
x = factor(mydata$Forest)
cdplot(x~mydata$change, ylab = NA, xlab = NA, yaxlabels = "n")
axis(4)

Trajectory Tree

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?

Resources