r - Barplot show only specific names (dates) in x axis -
i new in using r , have issue x-axis in barplots.
some example
t<-seq(as.date("2014-04-09"), as.date("2014-10-09"), by="days") c<-sample(0:100,184, rep=true) x<-data.frame(t,c) rownames(x)<-x[,c(1)] x<-subset(x,select=c(2)) par(las=2) barplot(t(x))
as can see, date on x-axis not nice. want display dates true las=1 orientation of las=2. tried create new object dates names that, not able so. maybe there way in barplot or in making new object names.
thanks help
solution 1:
plot(t,x$c,type="h")
edit: plot() solution works fine. if need have stacked bars in new example?
t<-seq(as.date("2014-04-09"), as.date("2014-10-09"), by="days") c<-sample(0:100,184, rep=true) f<-sample(0:100,184, rep=true) x<-data.frame(t,c,f) rownames(x)<-x[,c(1)] x<-subset(x,select=c(2,3)) par(las=2) barplot(t(x))
welcome stackoverflow!
does want?
plot(t,x$c,type="h")
(i assume don't really want display time series barplot , example. time series, you'd type="o"
.)
edit: stacked barplot, can following - first plot lower parts of bars x$c
, keeping enough vertical space via ylim
, using thicker lines lwd=3
can see difference between colored bars:
plot(t,x$c,type="h",lwd=3,ylim=c(0,max(rowsums(x))))
then add upper parts of bars using lines()
function. this, need specify vector of x values. since each bar vertical, use 2 copies of t
. need vector of y values lower , upper end of vertical lines. lower end x$c
, , upper end x$c+x$f
. need intersperse both x , y vectors na
s vertical lines not joined diagonal lines. find easiest create three-row matrices using rbind()
, last row na
. (below, use as.vector()
clarity - it's not needed; lines()
convert matrices vectors itself). finally, specify line thickness , color:
lines(as.vector(rbind(t,t,na)), as.vector(rbind(x$c,x$c+x$f,na)), lwd=3,col="gray")
a word of advice: it's not idea give data structures names conflict r functions. instance, have vector of dates t
, there function t()
(which transposes matrices - use one). have c
, conflicts function c()
. confuse both , people here. call data structures foo
, bar
, baz
in time-honored fashion.
Comments
Post a Comment