How to display a bar and a section of a sheet in a graph

Is there a way to output line and sheet charts to a graphics device, such as window() / quartz() ? There are at least two ways to get stem and leaf charts in R :? Stem , in a graphics package and ? stem.leaf , in the aplpack package. Both print text to the console. For example:

 > set.seed(1) > stem(rbinom(10, size=10, prob=.5)) The decimal point is at the | 3 | 0 4 | 000 5 | 0 6 | 00 7 | 000 

It would be nice if it were convenient to output to a graphics device, where it could be combined with other graphs (say, with a histogram) in a multi-line layout and / or saved as a png file. I know that you can output LaTeX and compile it into pdf (for example, see Stem and Leaf from R to LaTeX ), but it is not very convenient and in fact it is not what I need. Is there an R function that can do this? Is there a simple manual solution?

+7
r plot
source share
2 answers

Here is one simple example:

 plot.new() tmp <- capture.output(stem(iris$Petal.Length)) text( 0,1, paste(tmp, collapse='\n'), adj=c(0,1), family='mono' ) 

enter image description here

If you want to overlay a histogram, you probably want to use the text function for each of the tmp elements, rather than paste ing. Functions such as strheight and strwidth will be useful for finding coordinates.

There are also functions in the gplots and plotrix blocks for building text and adding tables to graphs (other functions in other packages probably exist on these lines as well).

+10
source share

The following is equivalent:

 set.seed(1) xx = rbinom(10, size=10, prob=.5) barplot(t(table(xx)), horiz=T) 

enter image description here

For even more similar ones:

 set.seed(1) xx = rnorm(10) xxch = as.character(xx) ff = sapply(strsplit(xxch, '\\.'), function(x) x[1]) ss = sapply(strsplit(xxch, '\\.'), function(x) x[2]) first = sapply(strsplit(ss, ''), function(x) x[1]) second = sapply(strsplit(ss, ''), function(x) x[2]) third = sapply(strsplit(ss, ''), function(x) x[3]) dd = data.frame(ff, first, second, third) dd = cbind(dd[1], sapply(dd[-1], as.numeric)) ddt = data.table(dd) gg = ddt[order(ff,first)][,paste(first, collapse=""),by=ff] gg$rr = rownames(gg) ggplot(gg)+geom_text(aes(x=rr, y=1, label=paste(ff,'|',V1))) + theme(axis.text = element_blank(),axis.title = element_blank(), axis.ticks=element_blank()) + coord_flip()+ labs(title="Decimal is at |") 

enter image description here

Perhaps the code may be changed for different sets.

Using capture.output (as suggested by @Greg) and plotting with ggplot:

 tmp <- capture.output(stem(iris$Petal.Length)) stemdf = data.frame(tmp, rr=1:length(tmp)) ggplot(stemdf)+ geom_text(aes(x=rr, y=0, label=tmp), hjust=0) + coord_flip()+ theme_classic() + scale_x_discrete(breaks=NULL)+ scale_y_discrete(breaks=NULL, limits=c(0,1))+ theme(axis.text = element_blank(), axis.title = element_blank(), axis.ticks=element_blank(), panel.grid=element_blank(), axis.line=element_blank()) 

enter image description here

+3
source share

All Articles