Making a line chart with non-numerical x axis

Basic example of creating a line chart with user defined x axis values using R.

opar=par(ps=18)
label = c('no_filter',9,8,7,6,5,4)
a <- c("0.4682953","0.466284","0.4587435","0.4095376","0.4444738","0.7144069","1.105043")
b <- c("0.9562088","0.953856","0.9104818","0.7554028","0.64136","0.877509","1.125698")
c <- c("0.7536005","0.7487367","0.7200604","0.6408311","0.5488365","0.6355055","1.051849")
d <- c("0.6601285","0.6566467","0.623516","0.5532256","0.5434039","0.6835916","1.047395")
e <- c("0.7536913","0.7511848","0.7338917","0.6548796","0.5129727","0.6585963","0.9883826")
f <- c("0.5596907","0.5595791","0.5512355","0.5178115","0.5014316","0.5900139","0.9123776")
g <- c("0.4868574","0.4866527","0.4776274","0.4359562","0.3950309","0.5714427","1.190739")
plot(a,axes=F,xlab="",ylab="",type="b",col="red")
lines(b,type="b",col="orange")
lines(c,type="b",col="yellow")
lines(d,type="b",col="green")
lines(e,type="b",col="blue")
lines(f,type="b",col="purple")
lines(g,type="b",col="violet")
axis(2)
axis(1,at=1:length(label),labels=label)
title(main = "main", xlab="xlab", ylab = "ylab")
legend(4,1.1,c("a","b","c","d","e","f","g"),col=c("red","orange","yellow","green","blue","purple","violet"),lty=c(1,1,1,1,1,1,1),lwd=c(1,1,1,1,1,1,1))

opar=par(ps=18)
label = c('no_filter',9,8,7,6,5)
data = read.table("file.tsv",header=F,sep="\t")
data = data[,-1]
a = as.vector(t(data[1,]))
b = as.vector(t(data[2,]))
c = as.vector(t(data[3,]))
d = as.vector(t(data[4,]))
e = as.vector(t(data[5,]))
f = as.vector(t(data[6,]))
g = as.vector(t(data[7,]))
h = as.vector(t(data[8,]))
i = as.vector(t(data[9,]))
j = as.vector(t(data[10,]))
k = as.vector(t(data[11,]))
l = as.vector(t(data[12,]))
m = as.vector(t(data[13,]))
n = as.vector(t(data[14,]))
range(as.vector(t(data))) #get the range
yrange = c(0.2,0.2,0.2,0.2,0.2,0.7)
plot(yrange,type="n",axes=F,ylab="",xlab="")
lines(a,type="b")
lines(b,type="b")
lines(c,type="b")
lines(d,type="b")
lines(e,type="b")
lines(f,type="b")
lines(g,type="b")
lines(h,type="b")
lines(i,type="b")
lines(j,type="b")
lines(k,type="b")
lines(l,type="b")
lines(m,type="b")
lines(n,type="b")
axis(2)
axis(1,at=1:length(label),labels=label)
title(main = "main", xlab="xlab", ylab = "ylab")

Print Friendly, PDF & Email



Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.