forestplot x-axis omit labels but draw tickmarks - r

I have a plot made with forestplot in the rmeta package. Notice the horizontal axis has no tick marks and labels between 0.2 and 7. How could I add tick marks without labels at 1,2,3,4,5 and 6 without labelling them? I just want the tick marks here. Here is the plot:
How do I have the ticks at 0.2,1,2,3,4,5,6 and 7, but I labelled only at c(0.2,7)? This the code:
library(rmeta)
tabletext<-rbind(c("A","3.77"),
c("B","1.33"),
c("C","1.32"),
c("D","1.12"),
c("E","1.58"),
c("F","0.9"))
m=c(3.77,1.33,1.32,1.12,1.58,0.9)
l=c(0.6144,0.644,0.6536,0.4536,1.0116,0.7236)
u=c(6.9256,2.016,1.9864,1.7864,2.1484,1.0764)
#overview datafile:
cbind(tabletext, m,l,u)
m l u
[1,] "A" "3.77" "3.77" "0.6144" "6.9256"
[2,] "B" "1.33" "1.33" "0.644" "2.016"
[3,] "C" "1.32" "1.32" "0.6536" "1.9864"
[4,] "D" "1.12" "1.12" "0.4536" "1.7864"
[5,] "E" "1.58" "1.58" "1.0116" "2.1484"
[6,] "F" "0.9" "0.9" "0.7236" "1.0764"
forestplot(tabletext,m,l,u, zero=1, xticks=c(0.2,7),col=meta.colors(box="royalblue",line="darkblue", summary="royalblue"))
I could extend the xticks=c(0.2,7) to xticks=c(0.2,1,2,3,4,5,6,7), but then all the labels at 2,3,4,5,6 would also be printed, which I dont want to.

Thanks for the suggestion. The rmeta does not have this option but I've added this to the forestplot-package (currently in the develop branch 1.2.1):
tabletext<-rbind(c("A","3.77"),
c("B","1.33"),
c("C","1.32"),
c("D","1.12"),
c("E","1.58"),
c("F","0.9"))
m=c(3.77,1.33,1.32,1.12,1.58,0.9)
l=c(0.6144,0.644,0.6536,0.4536,1.0116,0.7236)
u=c(6.9256,2.016,1.9864,1.7864,2.1484,1.0764)
#overview datafile:
xticks <- seq(from = 0.2, to = 7, by = .5)
xlabels <- rep(TRUE, length.out = length(xticks))
xlabels[xticks > 2] <- FALSE
xlabels[length(xlabels)] <- TRUE
attr(xticks, "labels") <- xlabels
forestplot(tabletext,new_page = TRUE,
m,l,u,
zero=1,
xticks=xticks,
col=fpColors(box="royalblue",line="darkblue", summary="royalblue"))
Download the develop version using devtools:
devtools::install_github("gforge/forestplot", ref="develop")

Related

Non numeric to binary operator error in R console

I am trying to add the % symbol i front of my perc.of.adult function in a bar diagram but the non numeric to binary code keeps appearing for some reason. How do I accomplish this without this error?
perc.of.adults<- c(1.8,36.7,35.3,26.2)
labels1 = paste0(" (", perc.of.adults, "%)")
perc.of.adults=labels1
barplot(
perc.of.adults, names.arg=c("Underweight", "Healthy weight" , "Overweight" , "Obese"),
xlab="Weight category", ylab="Percentage" ,
main="Bar Graph of Weight Category Against Percent of Adults")
Keep the numeric form of perc.of.adults and add the labels second.
perc.of.adults <- c(1.8, 36.7, 35.3, 26.2)
labels1 <- paste0(" (", perc.of.adults, "%)")
bp <- barplot(
perc.of.adults, names.arg=c("Underweight", "Healthy weight" , "Overweight" , "Obese"),
xlab="Weight category", ylab="Percentage" ,
main="Bar Graph of Weight Category Against Percent of Adults")
bp
# [,1]
# [1,] 0.7
# [2,] 1.9
# [3,] 3.1
# [4,] 4.3
text(bp[,1], perc.of.adults + ifelse(perc.of.adults < 5, 4, -4), labels1)
(It's worth noting that barplot returns a matrix with the centers of the x positions of the bars. What's really noteworthy is that with four bars, they are not centered on 1:4, but a little spread out. Whatever the history or rationale for this, it's easy to capture and use.)

How can I create a boxplot with whiskers?

I have created a plot with 3 boxplots, but my whiskers are not showing for one of them.
How can I make them show?
This is my data:
Class 3: 5.055052 3.028838 3.423485 6.434745 6.396239 4.114418
3.687380 2.633139 7.356185 5.736677 4.462504 7.137034
Class 4: 4.738094 21.736701 6.716363 10.306583 4.757640 6.265024
My code is as follows:
boxplot(hvol.concentration,class.3, class.4, ylab="8-OHdG Concentration (ng/ml)", main="Boxplot Distribution of 8-OHdG", ylim=c(0,25), pch=16, names=c("Control", "NYHA III", "NYHA IV"))
boxplot(c(4.738094, 21.736701, 6.716363, 10.306583, 4.757640, 6.265024), plot = FALSE)$stats
## [,1]
## [1,] 4.738094 <<== It's definitely there but the lower bound of the IQR is almost the same as min val
## [2,] 4.757640
## [3,] 6.490694
## [4,] 10.306583 <<== Upper bound of IQR == max val
## [5,] 10.306583
If you make the plot window bigger the grid size will be sufficient to see the lower IQR:
boxplot(
c(4.738094, 21.736701, 6.716363, 10.306583, 4.757640, 6.265024),
horizontal = TRUE
)

Use customized order of axis with heatmap.2 function from gplots

I need to plot a heatmap using axis ordered as is in table.
My data is is csv format:
"X" "Mescla" "HCL" "HSL" "Kmeans" "soma"
"1" "DR" 15.33559 14.7499 14.7556 14.32343 89.78054
"2" "DA" 16.59264 14.764 14.9968 14.36513 91.08672
"3" "UMR80" 16.28646 15.88403 14.01783 15.96327 94.55977
"4" "UMR" 16.46229 15.87505 14.34763 15.87903 94.83926
"5" "MR50" 16.61305 16.04243 14.85003 16.15599 96.20576
> data
X merge A1 A2 K sum
1 DR 15.33559 14.74990 14.75560 14.32343 89.78054
2 DA 16.59264 14.76400 14.99680 14.36513 91.08672
3 UMR80 16.28646 15.88403 14.01783 15.96327 94.55977
4 UMR 16.46229 15.87505 14.34763 15.87903 94.83926
5 MR50 16.61305 16.04243 14.85003 16.15599 96.20576
My solution is to use arguments Rowv=FALSE and Colv=FALSE:
library(gplots)
library(RColorBrewer)
rnames <- data[,1]
mat_data <- data.matrix(data[,3:ncol(data)-1])
rownames(mat_data) <- rnames
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
col_breaks = c(seq(-1,0,length=100), # for red
seq(0,0.8,length=100), # for yellow
seq(0.81,1,length=100)) # for green
heatmap.2(mat_data,
main = "Rank", # heat map title
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(7,5), # widens margins around plot
col=my_palette, # use on color palette defined earlier
Rowv=FALSE,
Colv=FALSE,
dendrogram="none") # turn off column clustering
The heatmap is:

Plot R matrix columns according to column name

I would like to plot the following matrix x, so the column data are plotted according to their column name (i.e. 0.1, 0.2, etc.) on the x-axis.
> x
0.1 0.2 0.3 0.4 0.5
[1,] 5.000000e-01 5.000000e-01 5.000000e-01 5.000000e-01 0.5000000000
[2,] 2.500000e-02 5.000000e-02 7.500000e-02 1.000000e-01 0.1250000000
[3,] 2.437500e-03 9.500000e-03 2.081250e-02 3.600000e-02 0.0546875000
[4,] 2.431559e-04 1.881950e-03 6.113802e-03 1.388160e-02 0.0258483887
[5,] 2.430967e-05 3.756817e-04 1.822927e-03 5.475560e-03 0.0125901247
[6,] 2.430908e-06 7.510810e-05 5.458812e-04 2.178231e-03 0.0062158067
[7,] 2.430902e-07 1.502049e-05 1.636750e-04 8.693947e-04 0.0030885852
[8,] 2.430902e-08 3.004053e-06 4.909445e-05 3.474555e-04 0.0015395229
[9,] 2.430902e-09 6.008089e-07 1.472761e-05 1.389339e-04 0.0007685764
[10,] 2.430902e-10 1.201617e-07 4.418219e-06 5.556585e-05 0.0003839928
But when I use
plot(x, pch=20, ylim=c(0, 1))
I get the following: Plot of R matrix.
I want a plot, where x[1, 1] (i.e. 5.000000e-01) is plotted as a point on 0.1 on the x-axis and 0.5 on the y-axis.
set.seed(123)
mat<-matrix(rnorm(25),5,5)
colnames(mat)<-seq(0.1,0.5,length.out=5)
plot(x=matrix(rep(as.numeric(colnames(mat)),5), 5,5,byrow=T),y=mat)
here the first argument x will repeat the number on the x axis by 5, so 5 x 5 I'll get a matrix which will give the right x position to each y column.
matplot(x=matrix(rep(as.numeric(colnames(mat)),5), 5,5,byrow=T),y=mat)
Can also be used

plot vertical color line in R

I would like to plot multiple color vertical line for a time series as below,
[1,] 4.698478 0
[2,] 4.698205 1
[3,] 4.698569 0
[4,] 4.697385 -1
...
to plot a blue color vertical line when [,2] is 1, and a red line when [,2] is -1, wonder if someone can help, thanks!
You can do the following, assuming data[, 1] is the x-value for the vertical line:
abline(v = data[data[, 2] == 1, 1], col = 'blue')
abline(v = data[data[, 2] == -1, 1], col = 'red')
Nothing wrong with #ChristopherLouden's answer, but this would be a way to do it in a single call to abline:
abline(v=m[,1], col=c('red', NA, 'blue')[as.numeric(as.factor(m[,2]))])
I ended up using quantmod's charting function, pretty good results.
chartSeries(price[,1])
addTA(price[,2]==1,pch=1, on=1,col="blue")
addTA(price[,2]==-1,pch=1, on=1,col="red")

Resources