List of ggplot2 theme options? - r

After some research I found the way to prevent an uninformative legend from displaying
... + theme(legend.position = "none")
Where can I find all of the available theme options and their default values for ggplot2?

The closest thing to a comprehensive list that I have been able to find is in the ggplot2 wiki on github. I think that most of the options are covered there with examples.
Update
If you would like to see the options that are in use on a current plot, you can use plot_theme(x) to see all of the options that are currently set for the plot named x. It will not be a comprehensive list, but should help if you want to change something like the font size for an axis label.
Update 2
With the transition to version 0.9.0 it's worth noting that the built in documentation has been dramatically improved, and the transition guide from 0.8.9 to 0.9.0 is also worth checking out.
Update 3
There is now a ggplot2 documentation website. Look at the documentation for theme for a complete list. Also, ?theme has a pretty complete list as of 0.9.3.
Update 4
There is now a ggthemes package that has some nice themes and scales to choose from. It might save you from having to create your own. See their github page for more information.

Entering in
theme_get()
will show a comprehensive listing of theme values and options. You can then follow the syntax to modify these attributes in opts().

The ggplot2 package does not contain much reference information-- this is probably because Hadley has put a lot of work into developing and polishing the package, creating a website full of examples and writing an excellent book that describes the system in detail.
The first place I would look for answers would be the ggplot2 website:
http://docs.ggplot2.org/
However, since opts() is not really a geom, stat or scale there are no examples that focus specifically on it. The next place to look would be the section of the website that contains material from the book-- including source code for examples:
http://ggplot2.org/book/
The example code for the chapter "Polishing your plots for publication" contains some well commented examples of using set_theme() and opts() to alter plot appearance.
The ultimate source for information is of course the book it's self. If you find ggplot2 has simplified and streamlined your workflow for producing statistical graphics, buying a copy of the book is a great way to say "thank you" and support the further development of the package.
Update
After some further investigation, you may be able to find a relatively complete list of options by listing out the source of one of the "theme" functions such as theme_bw. The source doesn't provide a description of the possible key=value pairs, but it does at least show most of the key names that have an effect when set via opts().

Besides the obvious references to ggplot2 reference manual and to the graphs section of Cookbook for R, Hadley Wickham provides a nice opts() List on github.

Theme templates:
https://github.com/jrnold/ggthemes
like "The economist", "Stata", "tufte" and more..
I know the answer is not exactly what was asked, but it was what I was looking for when I found this question, so others might too.

All the options I've ever used have been explained in hadley's great ggplot2 book.

Best list I've found for version 0.9.2.1 is here.

?opts
although, this does not display how to finetune its arguments, therefore it's better to check the options given above. If you can get a copy of the ggplot2 reference manual, it will help you a lot.

I made this quick reference for any theme or tasks you might look for. For a more general understanding, this ggplot2 tutorial should help.

Related

Using R's exams package for assignments: Is it possible to add question hints?

The exams package is a really fantastic tool for generating exams from R.
I am interested in the possibilities of using it for (programming) assignments. The main difference from an exam is that besides solutions I'd also like hints to be included in the PDF / HTML output file.
Typically I put the hints for (sub)-questions in a separate section at the end of the PDF assignment (using a separate Latex section), but this requires manual labour. These are for students to consult if they need help getting started on any particular exercise, and it avoids having them look at the solutions directly for hints on how to start.
An assignment might look like:
Question 1
Question 2 ...
Question 10
Hints to all questions
I'd be open to changing the exact format as long it is possible to look up hints without looking up the answer, and it remains optional to read the hints.
So in fact I am looking for an intermediate "hints" section between the between the "question" and "solution" section, which is present for some questions but not for all.
My questions: Is this already possible? If not, how could this be implemented using the exams package?
R/exams does not have dedicated/native support for this kind of assignment so it isn't available out of the box. So if you want to get this kind of processing you have to ensure it yourself using LaTeX for PDF or CSS for HTML.
In LaTeX I think it should be possible to do what you want using the newfloat and endfloat packages in the LaTeX template that you pass to exams2pdf(). Any LaTeX template needs to provide {question} and {solution} environments, e.g., the plain.tex template shipped with the package has
\newenvironment{question}{\item \textbf{Problem}\newline}{}
\newenvironment{solution}{\textbf{Solution}\newline}{}
with the exercises embedded as
\begin{enumerate}
%% \exinput{exercises}
\end{enumerate}
Now instead of the \newenvironment{solution}... you could use
\usepackage{newfloat,endfloat}
\DeclareFloatingEnvironment{hint}
\DeclareDelayedFloat{hint}{Hint}
\DeclareFloatingEnvironment{solution}
\DeclareDelayedFloat{solution}{Solution}
This defines two new floating environments {hint} and {solution} which are then declared delayed floats. And then you would need to customize these environments regarding the text displayed within the questions at the beginning and the listing at the end. I'm not sure if this can get you exactly what you want, though, but hopefully it is a useful place to start from.

how to make comments appear from custom functions

using RStudio I have noticed that, when calling a function, I can hit tab and a popup will appear with the possible parameters that can be chosen, e.g. if I type round( and hit tab, x= and digits= will appear as possible choices. This also happens with the custom functions I write. The difference is that built-in functions popups also have comments and explanations regarding the individual parameters. Is it possible to recreate such a behavior with custom functions as well?
I see what you mean. If you write a customised function
foo = function(x,y) { ... }
Then you go foo( and hit tab, the code completion pop-up menu will give you the options x = and y =. However, when you type an existing R function such as round(, not only does tab give you the options, but there's an explanation beneath each variable, telling you its role in the function:
The only way I could think of doing this for your own functions is to package your functions in your own customised package, and to make sure the "help" documentations includes your functions' parameters. This is getting beyond the realm of a stackoverflow question, but I'll point you to a couple of blogs where I learned the basics of R packages.
The Not So Standard Deviation blog explains how to write a simple package with help documentation, which is precisely what you need to see your customised functions appear with explanations inside RStudio's autocomplete. In a nutshell, you'll need to install roxygen2, devtools and, with each customised function, you'll need to thoroughly comment the function like this :
(disclaimer: the goofy cat example is the blogger's, not mine)
Here's a more detailed tutorial on creating R packages, and here's another blog on getting organised with R packages. Good luck!

Why does created histogram using ggplot2 always differ from examples in the tutorials?

I tested a couple of tutorials about how to create histograms using ggplot2 like this one. I think that I understand the concept however I am not sure why my created histograms slightly differ from the ones in the tutorials. Here is one histogram which actually should look like the first histogram in the tutorial I mentioned above.
I am using RStudio with R v3.3.1 and ggplot2 v2.1.0.
This a current known issue in ggplot2. A solution exists, but with major changes coming shortly, has not been implemented yet. If necessary, you can use
install.packages("devtools")
devtools::install_github("Katiedaisey/ggplot2")
to install the fixed version directly from my github fork.

Conditionally formatting tables using gridExtra in R

Recently I was exposed to the grid.table function in the package gridExtra in R. I am simply looking to make my dataframes look "better" than simply printing them to the console.
The default settings work pretty well, but now I am getting greedy. I am asking for help because I am not sure if the following two formatting settings are possible. I read the documentation but am still not sure if this can happen...
Conditionally format/highlight rows and columns if they meet a certain criteria. My thinking was that I could highlight a certain row to make it stand out from the rest of the table.
Is it possible to left alight the first column in the table and center align everything else?
In short, I really like how easy it is to format a dataframe as a table, but now I am hoping I can do a few extra formatting techniques to get the tables to where I need them to be.
Having just emailed Baptiste the author of gridExtra, that functionality is not currently available.
However, he says that once the gtable package is officially released he is planning to make an update to grid.table which should make it easier to customise the output. But also says not to "hold your breath"...He also however suggested to feel free to use the source code and adapt it.
But I too have been looking for ways to highlight rows in tables and have used the package dprint to do so. Hope that helps.

Namespaces in R packages

How do people learn about giving an R package a namespace? I find the documention in "R Extensions" fine, but I don't really get what is happening when a variable is imported or exported - I need a dummy's guide to these directives.
How do you decide what is exported? Is it just everything that really shouldn't required the pkg:::var syntax? What about imports?
Do imports make it easier to ensure that your use of other package functions doesn't get confused when function names overlap?
Are there special considerations for S4 classes?
Packages that I'm familiar with that use namespaces such as sp and rgdal are quite complicated - are there simple examples that could make things clearer?
I have a start on an answer on the devtools wiki: https://r-pkgs.org/Metadata.html
Few years later here....
I consolidated findings from Chambers, other StackOverflow posts, and lots of tinkering in R:
https://blog.thatbuthow.com/how-r-searches-and-finds-stuff/
This is less about implementing NAMESPACE/IMPORTS/DEPENDS and more about the purpose of these structures. Answers some of your questions.
The clearest explanation I've read is in John Chambers' Software for Data Analysis: Programming with R, page 103. I don't know of any free online explanations that are better than what you've already found in the R Extensions manual.
You could also pick an easy, small package and follow it.
I semi-randomly looked at digest which is one of my smaller packages. I loads a (small) dynamic library and exports one symbol, the digest() function. Here is the content of the NAMESPACE file:
## package has dynamic library
useDynLib(digest)
## and one and only one core function
export(digest)
Have a look at the rest of the source files and maybe try to read Writing R Extensions alongside looking at the example, and do some experiments.
http://www.stat.uiowa.edu/~luke/R/namespaces/morenames.pdf

Resources