Code to clear all plots in RStudio

dev.off() closes the current graphical device. This clears all of the plots for me in RStudio as long as I don’t have a different graphical device open at the moment. If you do have other graphical devices open then you can use dev.list() to figure out which graphical device is RStudio’s. The following should do that but I haven’t tested … Read more

Construct a manual legend for a complicated plot

You need to map attributes to aesthetics (colours within the aes statement) to produce a legend. I understand where Roland is coming from, but since this is only 3 attributes, and complications arise from superimposing bars and error bars this may be reasonable to leave the data in wide format like it is. It could … Read more

Categories r Tags

Use of ~ (tilde) in R programming Language

The thing on the right of <- is a formula object. It is often used to denote a statistical model, where the thing on the left of the ~ is the response and the things on the right of the ~ are the explanatory variables. So in English you’d say something like “Species depends on Sepal Length, Sepal Width, Petal Length and Petal Width”. … Read more

Round down a numeric

I think you are looking for floor(a * 100) / 100. Quick Test I just noticed that you changed your question 7 hours ago. Then my answer is not doing exactly what you want (as I am assuming by “rounding down” you always want to round toward -Inf). But I have discussed this in first version of … Read more

How To Create Vector of Vector In R

Use a list: Think of it as a map/dictionary/associative array that is being indexed by an integer. And if you want to take a string like the one above and turn it into a list of vectors: I’m using strsplit to split by newlines, then applying strsplit again to each line. The as.numeric is there … Read more

Categories r Tags

increase legend font size ggplot2

You can use theme_get() to display the possible options for theme. You can control the legend font size using: replacing X with the desired size.

How to access the last value in a vector?

Suppose I have a vector that is nested in a dataframe one or two levels. Is there a quick and dirty way to access the last value, without using the length() function? Something ala PERL’s $# special var? So I would like something like: instead of

Categories r Tags

Get the list of installed packages by user in R

How we can get the list of installed packages by user in R along with its version? I know about the command installed.packages() which will give information about all packages (base or non-base). But how we can get those installed by user to have something like this: For all user installed packages (i.e. those package … Read more

Categories r Tags