How to output a chart from Nielsen? - r

I saw an interesting chart on engadget today made by Nielsen:
http://www.engadget.com/2011/07/28/nielsen-android-leads-us-smartphone-market-with-39-percent-shar/
original source: http://blog.nielsen.com/nielsenwire/online_mobile/in-u-s-smartphone-market-android-is-top-operating-system-apple-is-top-manufacturer/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+NielsenWire+%28Nielsen+Wire%29
I'd love for someone to replicate it if possible and show the R code. Basic packages or ggplot2 would be great.
I like that the boxes are proportional, that's a key feature :)
Thanks!

You can find several implementations in R under the name of 'mosaic chart'. E.g.:
require("vcd")
data(HairEyeColor)
mosaic(HairEyeColor, shade = TRUE)
Se some examples on e.g. quickR, but searching the R graph gallery is also a good option.
In ggplot2, you can find a sample on learnr's blog.
I have also done some tweaks in ggplot2, please find the attached plot below. It is in Hungarian, but if you are interested, I could clean up the code and post is somewhere.
UPDATE: I have searched for my old script based on comment and uploaded it to pastebin. Sorry, no code clean up and it is quite messy, as I had to make it up for mass reporting from SPSS data files, but I hope you could use it. The usage is simple: load all functions (e.g.: run all lines in R with the source(...) function), and you could generate a mosaic chart of any data frame by specifying two variable names in the parameters of ggMosaicChart(). The plot will be saved to a png file in the working directory (no easy resize in R of the plot as lots of manual tweaks are done to arrange text nicely).
I have translated the strings to English, a basic example (included in the above code) of the mtcars data set:
Count, row- and column percent and also Pearson residuals are shown for each cell.

It's called treemap. R project has packages named "treemap" or "portfolio" for it. Here is how to do: http://flowingdata.com/2010/02/11/an-easy-way-to-make-a-treemap/

Related

How to make a nice looking table in base r (not markdown)

I’ve been looking for an hour, but everything I can find about how to make a nice looking table out of a data frame mentions that it’s for rmarkdown, html, or latex.
Is it not possible to make a nice looking table in base r?
plot(x, y) makes a graph.
Is there no function like: printTable(df)?
Broadly speaking over what you can get from a normal print in base::print there is not much else you can do. You could try to twist plot function to plot values from selected cells in a data frame but that would be very onerous to develop and impractical in the light of currently available and maintained solutions. There is a number of packages that let you achieve what you need. For instance you can try formattable by renkun-ken.
Example
For a simple example you can try formattable::formattable(mtcars[1:10,])
Creating Images
For a solution creating images from tables, have a look at this discussion. As discussed, in the linked answer if you insist on generating a static image you can use grid.table function offered via gridExtra: tbl <- grid.table(mtcars[1:5,]).
You may be interested in the flextable package that is very easy to use with multiple options to create nice tables.
You can also have multiple word, pdf, or html output types.
I invite you to check the manual : https://cran.r-project.org/web/packages/flextable/vignettes/overview.html

Saving sequence tree from GraphViz

I'm new to TraMineR and sequence analysis in general. I am working on a project related to retention and recruitment of educational leaders and finding TraMineR to be very useful. This may be a simple thing (and somewhat unimportant), but I cannot seem to figure out how to name or direct the sequence tree created by GraphViz within the TraMinR package. Right now, my code is:
wardTree=as.seqtree(wardCluster,seqdata=retain.seq,diss=retain.dst,ncluster=15)
seqtreedisplay(wardTree,type="d",border=NA,showdepth=TRUE)
It produces a great graphic, but with a random file name that I cannot relocate if I accidentally close the graphic.
My main goal is to be able to uniquely name and save these graphs and pull into an R Markdown for the full project. To this point, the only thing I cannot pull in is the seqtree graphic.
Well, guess I figured it out! I didn't realize that the order in which filename command mattered...so...what seems to work out beautifully is:
wardTree=as.seqtree(wardCluster,seqdata=retain.seq,diss=retain.dst,ncluster=15)
seqtreedisplay(wardTree,type="d",border=NA,showtree=TRUE,showdepth=TRUE,filename="retaintree.png",imageformat="png")
And to incorporate the file in the Markdown i use the command
```
![](C:\Users\myname\Desktop\retaintree.png)
```{r fig.width=8.5, fig.height=11}
Hope this helps someone else along the way!

Is it possible to include ggplot command or data argument as a footnote to the plot?

...of course it can be done manually using annotation and the typing the whole command again, but I would prefer something like:
qplot(...)+annotation_command()
(because the manual option means that I have to change the code at two places)
This would be especially useful when creating lots of plots in a data exploration and one wants to keep it reproducible.
If it is not possible it would be the next best, to include the argument of "data", because it is a common hassle that it cannot be reproduced which subset is shown in a certain plot.
Thanks in advance!
Elisabeth

How to show gaps in R/quantmod's chartSeries/candleChart plots

I am trying to show "gaps" in financial data using the plotting functions in the excellent quantmod package for R.
Normally R allows you to show gaps in plots using NA values, as with:
x<-1:10
y<-2*x
y[4:7]<-NA
plot(x,y,type="l")
I would like to do something similar with R/quantmod's candleChart plots. However, rows of data containing NA's are removed before plotting (there is a na.omit command in the chartSeries code that does this) so I cannot see how to do this.
An example is:
require(quantmod)
#Make some pretend data
x<-0:30
y<-100+20*sin(x)
y.open<-y[-length(y)]
y.close<-y[-1]
val<-as.xts(cbind(y.open,y.open+5,y.close-5,y.close,1000),order.by=as.POSIXct(paste("2011-01-",x[-1],sep='')))
colnames(val)<-c("Open","High","Low","Close","Volume")
#Plot this pretend data
candleChart(val,theme="white")
#Now try and make a "gap" in the middle of the data and plot it
val2<-val
val2[5:20,]<-NA
candleChart(val2,theme="white")
What is the "correct" way to do this? I guess I could overwrite chartSeries with my own version of this function (identical but without the na.omit() call), but that seems quite drastic.
Is there perhaps an option to do this kind of thing available? I have been unable to google anything useful...
Thanks,
fttb
The answer is not to use chartSeries, but rather the newer variant (still in development technically) chart_Series. Note the underscore.
chart_Series(val2)
If you're looking for more details on quantmod and using R in finance, we are hosting a large conference in Chicago at the end of this month. More info can be found here: R/Finance 2011
Hope that helps, and hope to see you in Chicago!!

Convert a Graph to a Data Frame in R

So a while back (6 months+) I saw a blog post where the author took a line graph someone had posted on the internet, fed the image into R, and used a function to convert the image into a data frame.
I've looked everywhere, and I can't seem to find this blog post (even though I'm sure I bookmarked it). So I was wondering if any of you had also read said blog post, or if someone knew of a quick and easy way to convert a line graph to a data frame in R?
Was this it? I searched for "R digitize plot". The package used is "ReadImages". For completeness, the steps listed were (see link):
library(ReadImages) #Load package
mygraph <- read.jpeg('plot.jpg') #Import image
plot(mygraph) # Plot the image
calpoints <- locator(n=4,type='p',pch=4,col='blue',lwd=2) # Calibrate the plot by selecting known coordinates
data <- locator(type='p',pch=1,col='red',lwd=1.2,cex=1.2) # Collect the data points in a dataframe
When you say 'the image as a data frame', do you mean you want to get back to the original data that made the line?
It's not R, but I've used Engauge Digitizer for this sort of thing:
http://digitizer.sourceforge.net/
Also look at the updateusr function in the TeachingDemos package. Once you have the image displayed as in Benjamin's post, you can use the updateusr function with the known points to change the user coordinates so that then the results from the locator function do not need any additional transformation.
As i write this, the digitize package and the ReadImages package are no longer available for R 3.0.2. Engauge Digitizer is a good option but if you still want to do this sort of thing in R, take a loook at http://rscriptsandtips.blogspot.no/
You can also use im2graph to convert graphs to data. It's free and available of Windows and Linux (http://www.im2graph.co.il).

Resources