Stacked barplot is opposite order to legend? - r

A minor question about plotting stacked barplot in R.
The stacked bars represent the series bottom-to-top.
But the legend always shows the series top-to-bottom. I think that is also true with ggplot2::geom_bar
Is there any nicer idiom than using rev(...) twice inside either legend() or barplot() as in:
exports <- data.frame(100*rbind('Americas'=runif(6),'Asia'=runif(6),'Other'=runif(6)))
colnames(exports) <- 2004:2009
series_we_want <- c(1,2,3)
barplot( as.matrix(exports[series_we_want,]), col=mycolors, ...)
legend(x="topleft", legend=rev(rownames(exports)[series_we_want]), col=rev(mycolors) ...)
(If you omit one of the rev()'s the output is obviously meaningless. Seems like an enhance case for adding a single flag yflip=TRUE or yreverse=TRUE)

This is what I got using your code:
exports <- data.frame(100*rbind('Americas'=runif(6),'Asia'=runif(6),'Other'=runif(6)))
colnames(exports) <- 2004:2009
series_we_want <- c(1,2,3)
barplot( as.matrix(exports[series_we_want,]))
legend(x="topleft", legend=rev(rownames(exports)[series_we_want]))
try this:
exports <- data.frame(100*rbind('Americas'=runif(6),'Asia'=runif(6),'Other'=runif(6)))
colnames(exports) <- 2004:2009
series_we_want <- c(1,2,3)
test_data<-as.matrix(exports[series_we_want])
barplot( test_data,
legend.text=as.character(rev(rownames(exports)[series_we_want])),
args.legend = list(x="topleft"))
seems to produce the legend in the opposite order of what you have

Related

Dotchart with secondary axis

I'm trying to produce a dotchart with a secondary axis on top. However once I plot the second dotchart (with a par(new=T)), I can't figure out how not to display the axis ticks over the previous ones in axis side=1. Here's my code with mock data:
y1_i <- c(2,8,2,14,2)
y2_i <- c(15,17,28,22,30)
y1_f <- c(4,9,11,16,7)
y2_f <- c(13,11,16,11,21)
y=c(y1_i,y2_i,y1_f,y2_f)
x <- c("AAEG","AALO","AGAM","ACHR","AALB")
y1=c(y1_i,y1_f)
y2=c(y2_i,y2_f)
dotchart(y1_i,labels=x,xlab="N50 length",xlim = c(0,max(y1)))
par(new=T)
dotchart(y2_i,labels=x,xlim = c(0,max(y2)))
axis(side=3)
Also, if possible, I would like to add a second data set which would be slightly pushed vertically above the first dataset (to not overlap it), but still corresponding to the same y-axis categories.
Thank you for any suggestion :)
Found it, by using dotchart2 from the Hmisc package
library(Hmisc)
y1_i <- c(2,8,2,14,2)
y2_i <- c(15,17,28,22,30)
y1_f <- c(4,9,11,16,7)
y2_f <- c(13,11,16,11,21)
y=c(y1_i,y2_i,y1_f,y2_f)
x <- c("AAEG","AALO","AGAM","ACHR","AALB")
y1=c(y1_i,y1_f)
y2=c(y2_i,y2_f)
y1_i <- c(2,8,2,14,2)
y2_i <- c(15,17,28,22,30)
y1_f <- c(4,9,11,16,7)
y2_f <- c(13,11,16,11,21)
y=c(y1_i,y2_i,y1_f,y2_f)
x <- c("AAEG","AALO","AGAM","ACHR","AALB")
y1=c(y1_i,y1_f)
y2=c(y2_i,y2_f)
dotchart2(y1_i,labels=x,xlab="N50 length",xlim = c(0,max(y1)))
par(new=T)
dotchart2(y2_i,labels=x,xlim = c(0,max(y2)),xlab="Scaffold number",lines=F,xaxis=F)
axis(side=3,xlab="Scaffold number")

Plot cut dendrogram with class labels

In the following example:
hc <- hclust(dist(mtcars))
hcd <- as.dendrogram((hc))
hcut4 <- cutree(hc,h=200)
class(hcut4)
plot(hcd,ylim=c(190,450))
I'd like to add the labels of the classes.
I can do:
hcd4 <- cut(hcd,h=200)$upper
plot(hcd4)
Besides the fact labels are oddly shifted, does the numbering
of the branches from cut() always correspond to the classes in hcut4?
In this case, they do:
hcd4cut <- cutree(hcd4, h=200)
hcd4cut
But is this the general case?
The example using dendextend (Label and color leaf dendrogram in r) is nice
library(dendextend)
colorCodes <- c("red","green","blue","cyan")
labels_colors(hcd) <- colorCodes[hcut4][order.dendrogram(hcd)]
plot(hcd)
Unfortunately, I always have many individuals, so plotting individuals is rarely a useful option for me.
I can do:
hcd <- as.dendrogram((hc))
hcd4 <- cut(hcd,h=200)$upper
and I can add colors
hcd4cut <- cutree(hcd4, h=200)
labels_colors(hcd4) <- colorCodes[hcd4cut][order.dendrogram(hcd4)]
plot(hcd4)
but the following does not work:
plot(hcd4,labels=hcd4cut)
Is there a better way to plot the cut dendrogram labelling branches
according to the classes (consistent with the result of cutree())?
This is an example of what I would need (class labels edited on the picture),
but note that the problem is that I do not know if the labels are actually at the right branch:

How to adjust x labels in R boxplot

This is my code to create a boxplot in R that has 4 boxplots in one.
psnr_x265_256 <- c(39.998,39.998, 40.766, 38.507,38.224,40.666,38.329,40.218,44.746,38.222)
psnr_x264_256 <- c(39.653, 38.106,37.794,36.13,36.808,41.991,36.718,39.26,46.071,36.677)
psnr_xvid_256 <- c(33.04564,33.207269,32.715427,32.104696,30.445141,33.135261,32.669766, 31.657039,31.53103,31.585865)
psnr_mpeg2_256 <- c(32.4198,32.055051,31.424819,30.560274,30.740421,32.484694, 32.512268,32.04659,32.345848, 31)
all_errors = cbind(psnr_x265_256, psnr_x264_256, psnr_xvid_256,psnr_mpeg2_256)
modes = cbind(rep("PSNR",10))
journal_linear_data <-data.frame(psnr_x265_256, psnr_x264_256, psnr_xvid_256,psnr_mpeg2_256)
yvars <- c("psnr_x265_256","psnr_x264_256","psnr_xvid_256","psnr_mpeg2_256")
xvars <- c("x265","x264","xvid","mpeg2")
bmp(filename="boxplot_PSNR_256.bmp")
boxplot(journal_linear_data[,yvars], xlab=xvars, ylab="PSNR")
dev.off()
This is the image I get.
I want to have the corresponding values for each boxplot in x axis "x265","x264","xvid","mpeg2".
Do you have any idea how to fix this?
There are multiple ways of changing the labels for your boxplot variables. Probably the simplest way is changing the column names of your data frame:
colnames(journal_linear_data) <- c("x265","x264","xvid","mpeg2")
Even simpler: you could do this right at the creation of your data frame too:
journal_linear_data <- data.frame(x265=psnr_x265_256, x264=psnr_x264_256, xvid=psnr_xvid_256, mpeg2=psnr_mpeg2_256)
If you run into the problem of your labels not being shown or overlapping due to too few space, try rotating the x labels using the las parameter, e.g. las=2 or las=3.

R contour levels don't match filled.contour

Hopefully a straightforward question but I made a simple figure in R using filled.contour(). It looks fine, and what it should like given the data. However, I want to add a reference line along a contour for 0 (level = 0), and the plotted line doesn't match the colors on the filled.contour figure. The line is close, but not matching with the figure (and eventually crossing over another contour from the filled.contour plot). Any ideas why this is happening?
aa <- c(0.05843150, 0.11300040, 0.15280030, 0.183524400, 0.20772430, 0.228121000)
bb <- c(0.01561055, 0.06520635, 0.10196237, 0.130127650, 0.15314544, 0.172292410)
cc <- c(-0.02166599, 0.02306650, 0.05619421, 0.082193680, 0.10334837, 0.121156780)
dd <- c(-0.05356592, -0.01432910, 0.01546647, 0.039156660, 0.05858709, 0.074953650)
ee <- c(-0.08071987, -0.04654243, -0.02011676, 0.000977798, 0.01855881, 0.033651089)
ff <- c(-0.10343798, -0.07416114, -0.05111547, -0.032481132, -0.01683215, -0.003636035)
gg <- c(-0.12237798, -0.09753544, -0.07785126, -0.061607548, -0.04788856, -0.036169540)
hh <-rbind(aa,bb,cc,dd,ee,ff,gg)
z <- as.matrix(hh)
y <- seq(0.5,1.75,0.25)
x <- seq(1,2.5,0.25)
filled.contour(x,y,z,
key.title = title(main=expression("log"(lambda))),
color.palette = topo.colors) #This works
contour(x,y,z, level=0,add=T,lwd=3) #This line doesn't match plot
This is completely answered in the ?filled.contour help page. In the Notes section it states
The output produced by filled.contour is actually a combination of two plots; one is the filled contour and one is the legend. Two separate coordinate systems are set up for these two plots, but they are only used internally – once the function has returned these coordinate systems are lost. If you want to annotate the main contour plot, for example to add points, you can specify graphics commands in the plot.axes argument. See the examples.
And the examples given in that help page show how to annotate on top of the main plot. In this particular case, the correct way would be
filled.contour(x,y,z,
key.title = title(main=expression("log"(lambda))),
color.palette = topo.colors,
plot.axes = {
axis(1)
axis(2)
contour(x,y,z, level=0,add=T,lwd=3)
}
)
which produces

Changing the legend in chartSeries to display values - Quantmod addTA

Is it possible to change the legend on the plot displayed in Quantmod so that values are displayed rather than the variable name? For example:
library("quantmod")
getSymbols("YHOO")
temp1 <- 6
temp2 <- "SMA"
barChart(YHOO)
addTA(ADX(YHOO, n=temp1, maType=temp2))
The legend that is displayed in the plot is ADX(YHOO, n=temp1, maType=temp2). I would like it to display the specific values instead i.e. ADX(YHOO, n=6, maType='SMA').
There isn't a way to do this automatically with addTA, because it would need to know which of the the parameters of the TA call it needs to evaluate. But you can do it manually by setting the legend= argument yourself.
One way to do it is to use paste (or paste0).
barChart(YHOO)
Legend <- paste0('ADX(YHOO, n=',temp1,', maType=',temp2,')')
addTA(ADX(YHOO, n=temp1, maType=temp2), legend=Legend)
Or you could create and manipulate the call to get what you want.
barChart(YHOO)
callTA <- call("ADX",quote(YHOO),n=temp1,maType=temp2)
eval(call("addTA", callTA, legend=deparse(callTA)))
The following is a partial solution which displays the values rather than variable names in the legend as well as the relevant output values for the TA. However, unlike the default settings of addTA, the text for each output value doesn't match the colour of the line on the addTA plot. Unfortunately I haven't worked out how to get the text of the output values to match the colour of its relevant line on the addTA plot. Any suggestions?
library("quantmod")
getSymbols("YHOO")
barChart(YHOO, subset="last 4 months")
col <- c("red", "blue", "green", "orange")
temp1 <- 8
temp2 <- "SMA"
temp <- ADX(HLC(YHOO), n=temp1, maType=temp2)
legend <- rep(NA, NCOL(temp)+1)
legend[1] <- paste("ADX(HLC(YHOO)", "n=", temp1, "maType=", temp2)
for(x in 2:(NCOL(temp)+1)){
legend[x] <- (paste(colnames(temp[,(x-1)]),": ", round(last(temp[,(x-1)]),3), sep=""))
}
addTA(temp, legend = legend, col=col)

Resources