I can create a dendrogram using
x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))
x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend)
However I want to delete the scale of height information in the left as shown in Figure below.
My guess is that it should be extremely trivial but I am not able to find a way to do this. One solution which I don't want is using the ggplot2 as below:
ggplot(as.ggdend(dend2))
This is because I will loose some of the formatting like color_bars()
The graphical parameter 'axes = FALSE" can be used to remove the distance measure for the plot.dendogram command:
plot(x.clust.dend, axes=F)
This will produce the following dendogram without distance axis:
You can just set yaxt = "n"
plot(x.clust.dend, yaxt = "n")
You can add another axis with
axis(side = 2, labels = FALSE)
Related
I am using vegan library to make some plots, with this code:
raremax <- min(colSums(mydata))
col <- palette()
lty <- c("solid", "dashed", "longdash", "dotdash")
pars <- expand.grid(col = col, lty = lty, stringsAsFactors = FALSE)
out <- with(pars[1:18, ], rarecurve(mydata, step = 100, sample = raremax,
cex =0.6, ylab="OTUs", label=F, col=col, lty=lty, lwd=2))
Then I add a legend using this code:
legend("bottomright", names(mydata), col=pars[1:18,1], lty= pars[1:18,2],
lwd=2, cex=0.5, xjust=1, ncol=2, x.intersp=0.5, y.intersp=0.5, bg="white")
The resulting graph looks like this:
I would like to reduce the space between legend columns, also reducing the size of the legend box, but I can't find a way to do that.
Anyone could provide me some help?
A combination of the legend() parameters "x.intersp" and "text.width" should be helpful.
Decreasing "x.intersp" (default value = 1, for me 0.25 looked good) should move your the legend labels closer to their respective points. Decreasing "text.width" (default value=NULL, for me 0.045 looked good) moves the columns closer together.
I am using R for plotting. When my graph plots the legend appears where I want it to be but the colors are missing. mtcars 2 is a modified version of mtcars (one of the pre-loaded data sets) that adds a model and country of origin to the data set. mtcars.pca is what I named my redundance analysis (rda function under vegan), and mtcars.clust is titled for hierarchical clustering of the continuous factors of mtcars (hclust function of vegan) Below is the code I am using with mtcars2.
pca.fig = ordiplot(mtcars.pca, type = "none", las=1, xlim=c(-15,15), ylim = c(-20,10))
points(pca.fig, "sites", pch = 19, col = "green", select = mtcars2$origin =="domestic")
points(pca.fig, "sites", pch = 19, col = "blue", select = mtcars2$origin =="foreign")
ordiellipse(mtcars.pca, mtcars2$origin, conf = 0.95, label = FALSE)
ordicluster(mtcars.pca, mtcars.clust, col = "gray")
legend("bottomright", title="Car Origin", c("domestic", "foreign"), col = "origin")
You need to specify a vector of colours in legend and also a pch:
library("vegan")
data(dune, dune.env)
ord <- rda(dune)
plot(ord, type = "n")
cols <- c("red","blue","green")
points(ord, col = cols[dune.env$Use], pch = 19)
legend("bottomright", legend = levels(dune.env$Use), bty = "n",
col = cols, pch = 19)
If you don't add pch but just use col = cols legend() doesn't display any points. Because you used pch = 19 in your points() calls, use the same in the legend() call.
Also, note how to plot points of different colours in a single pass. I have some examples and explanation that go through the indexing trick I used in my code above to achieve this in a blog post of mine from a few years ago: http://www.fromthebottomoftheheap.net/2012/04/11/customising-vegans-ordination-plots/
I came to this question having the next problem in xts object:
I wanted to plot all time-series in xts object with legend. Moreover, there were around 20.
I used (wrong):
plot(returns_xts)
addLegend(...)
Correct version:
plot(returns_xts, legend.loc = "bottomright", col=1:20, lty = 1)
There is legend.loc parameter
col = 1:20 generates colors for you
Result:
I made 13 parallel coordinate plots lines, where each plot has x lines, each of 5 points. There are three things that I would like to change:
I would like to remove very long vertical x-axis ticks that protrude below out of the graph
I would like to change the x-axis labels of each plot to be "N", "1", "2", "3", "4"
I would like the y-axis to be labelled for each plot. It currently is not. The maximum y-value for each plot is max(input). So, I like four y-axis labels: max(input), 3/4 max(input), 1/2 max(input), and 1/4 max(input) (all to the nearest integer to keep it neat).
I would like a main title over all the graphs (I'll just call it "Main Title" for now)
Here is my code currently:
par(mfrow = c(3,5))
par(mar=c(0.1,0.1,0.1,0.1))
# For each color (cluster) in the random network
for (i in 1:max(net$colors)){
color = mergedColors[which(net$colors == i)[1]]
input = countTable[which(net$colors==i),]
parcoord(input, lty = 1, var.label = FALSE, col = color)
}
where the str(input) is a data.frame of x observations of 5 variables.
I tried to add things like x.label = c("N","1","2","3","4"), but that did not work.
Edit:
Here is some sample data, as per suggestions. Please let me know if I should include anything else:
net <- data.frame(colors=as.numeric(sample(1:15, 100, replace = T)))
mycols <- c("brown", "blue", "turquoise", "greenyellow", "red",
"pink", "green", "yellow", "magenta", "black","purple",
"tomato1","peachpuff","orchid","slategrey")
mergedColors = mycols[net$colors]
countTable <- data.frame(matrix(sample(1:100,100*5, replace=T),
ncol=5, dimnames=list(NULL, c("Norm","One","Two","Three","Four"))))
OK. I'm not sure I understand request 1, but here's what I came up with so far
library(MASS)
opar<-par(no.readonly=T)
par(mfrow = c(3,5))
par(oma=c(1.2,2,2,0))
par(mar=c(2,2,0.1,0.1))
# For each color (cluster) in the random network
for (i in 1:max(net$colors)){
color = mergedColors[which(net$colors == i)]
input = countTable[which(net$colors==i),]
colnames(input)<-c("N",1:4)
parcoord(input, lty = 1, var.label = FALSE, col = color)
axis(2,at=seq(0,1,length.out=5),labels=seq(min(input),max(input), length.out=5))
}
mtext("Main Title",3, outer=T)
par(opar)
Consider the following vector:
vec <- c(-0.137042293280008 ,-0.0085530023889108 ,7.696986350237e-05 ,9.85275557252565e-05 ,0.000246261331270769 ,-0.0013658222244989 ,0.00117046787783182 ,-0.000423648394606887 ,-0.000112607126438433 ,0.00212185051472275 ,-0.000110104526782098)
names(vec) <- paste("var", 1:length(vec), sep = " ")
I would like to plot vec using a bar plot in R. However, as you can see, there is one or two values that are extreme compared to the rest of the vector. When the bar plot is drawn, the small values barely show on the graph.
par(xaxs='i',yaxs='i', mai = c(0.5,2,0.5,1.5))
bp2 <- barplot(vec, horiz = TRUE, col = "lightblue4", border = "lightblue4", yaxt = 'n', cex.axis = 0.7)
axis(2, at = bp2, labels = names(vec), tick = FALSE, las = 2, cex.axis = 0.7)
Is there a way to better display the chart? For example, is there a way to eventually split the x-axis? The graph below is an (unrelated) example, but it shows how the y-axis in this case is split to allow for all values to show on the graph.
P.S: Plotting with a log-scale is not an option in my case, as some of the vector values are negative.
Thank you!
You need gap.barplot from plotrix package. Take a look at this:
library(plotrix)
gap.barplot(vec,gap=c(-0.12,-0.04),xlab="Index",ytics=c(-0.04,-0.02,0),
ylab="",main="Barplot with gap", horiz=TRUE)
Modify gap and ytics argument to get the desired aesthetic for your plot.
It is easy to change the default strip height for lattice plots: the par.strip.text argument is all that one needs. But is there a simple way to to have strips of different heights within one multi-panel lattice plot?
I have in mind a plot with two rows of panels. The height of the strips in the first row would differ from the height of the strips in the second row.
I think that I can create such a figure by creating two plots –– one for the first row, another for the second row –– and then using grid.layout to position them. But I'd like to know if there is a more straightforward way to create such a figure.
I modified an example from this question (which is a much closer duplicate) and managed to achieve this:
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
stripHt <- rep(c(-1,0),each = 3)
# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
panel.rect(0, stripHt[which.panel], 1, 1,
col = bgColors[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = txtColors[which.panel])
}
xyplot(yield ~ year | site, data = barley, strip=myStripStyle)
Ignore the horrible colors. You get the point, we're just using a custom strip function.