-->

To plot them is my real test

image

I almost couldn’t bring myself to post this, but it’s April Fools’ Day, so I’ll never have a better opportunity.

This Gist shows how to scrape “stats” and .PNG images from, erm, Bulbapedia, run a simple dimensionality reduction on those “stats,” and plot all 151 original Pokemon.

I don’t know much about Pokemon, so I can’t vouch for the utility of this approach, but perhaps this post will one day be seen as the foundation of the field of Pokemon Studies.

By d-sparks

Tags: rstats graphics aprilFools

US State Maps using map_data()

image

Today’s short post will show how to make a simple map using map_data().

Let’s assume you have data in a CSV file that may look like this:

image

Notice the lower case state names; they will make merging the data much easier. The variable of interest we’re going to plot is the relative incarceration rates by race (whites and blacks) across each of the fifty states (we’ll remove DC once we load the data). Using the map_data(“state”) command, we can load a data.frame called “all_states”, shown below:

image

Merging that data with the data frame we have as a CSV produces:

image

We can then plot each state and shade it by our variable of interest:

image

Full code is below:

By use-r-friendly

Tags: graphics rstats ggplot2 AdventCalendaR

Happy Thanksgiving from is.R()!

Today’s post is mostly to wish everyone a Happy Thanksgiving. We will be taking the rest of the week off, and will return on Monday. Below is some code to reproduce the adorable Turkey above; enjoy!

By use-r-friendly

Tags: ggplot rstats graphics

Representing density in two dimensions

I’ll be subbing today for Chris, as we continue to explore some ggplot2 basics. Today, imagine that you have data distributed in two dimensions, and that you would like to convey differences in point density over space.

As with many things, this is fairly easy to do with ggplot2, but it’s not quite as straightforward as it relies on the use of a “stat,” as opposed to a “geom.” In this case, we use stat_density2d (naturally), and encode our statistic of interest (density) into an aesthetic variable by use of ..level.. .

By d-sparks

Tags: ggplot2 rstats graphics basics

Hjust and Vjust

So, when you’re setting the position of text in ggplot, you may have to use the hjust and vjust commands. Depending on your demands, and if you don’t understand what they’re doing, they might seem hard to use. I found one script that produces the graph below, as well as an excellent discussion here: http://goo.gl/TaUsG

I have modified the code to produce the following handy “guide” to when to use which level for each of the various values in combination with three different angle settings. Again, this is someone else’s work, but a re-post might be helpful:

By use-r-friendly

Tags: ggplot graphics rstats thirdPartyCode

Textual Healing Part II

Yesterday’s post showed a number of quick coding options for changing text in a basic ggplot. Here’s another two tricks for fine-tuning faceted plots. Again, let’s load our made up data about tooth growth (a real dataset in R, ToothGrowth, has the real data if you’re interested).

As usual, we begin with creating our dataset:

First plot:

Now, say we want to change something about the facet strip text. Specifically we want to highlight them with a yellow background and change the text size.

Plot.1 + theme(strip.text.x = element_text(size=16, face=”bold”) ,

               strip.background = element_rect(fill=”yellow”)   

               ) + theme(axis.text.x = element_text(colour=”black”, 

              size = 11, 

              face = “bold.italic”,

              angle=45,

              vjust=1,

              hjust=1))

Now, what if we wanted I, II and III to be meaningful; perhaps I corresponds to one half dose, II to Five CCs and III to ‘A lot’… I know this isn’t a great choice of specific labels and we could just have gone back and recreated the data from scratch. This is intentionally done to show how to overcome another problem. Below we create a factor variable that does this and then plot the result.

text.plots$DOSE2 <- c()

text.plots$DOSE2[text.plots$DOSE==”I”] <- “One Half Dose”

text.plots$DOSE2[text.plots$DOSE==”II”] <- “Five CCs”

text.plots$DOSE2[text.plots$DOSE==”III”] <- “A Lot”

Plot.2 <- ggplot( text.plots, aes( x = SUPP, y = LENGTH , fill = SUPP ) ) +

  geom_boxplot( alpha = 0.6, outlier.colour = c(“grey40”) , outlier.size=3.5

  ) + scale_fill_manual(values=c(“cadetblue”, “orange”, “orangered3”)) + 

  facet_wrap(~DOSE2) + theme_bw() +labs(title=”Tooth Growth in Guinea Pigs \n”, 

                                       x=”\n Treatment”, y=”Change in Length (mm) \n”) +  guides(fill = guide_legend(“\n Supplement”)) +geom_hline(y=0, lty=2)

Plot.3 <- Plot.2 + scale_y_continuous(breaks=seq(-4, 24, by=4))

Plot.3


As many of you have probably experienced, this is in the “wrong” order due to how factors are treated (alphabetically). One ‘hack’ is to simply add spaces to the variable names, another way is to do something like this:

text.plots$DOSE2 <- factor(text.plots$DOSE2)

text.plots$DOSE2 <- factor(text.plots$DOSE2,

                               levels= levels(text.plots$DOSE2)[c(3,2,1)])

Now our plot looks like this:

Now they’re in their “right” order, not just alphabetical, and we can adjust the plot as we have in previous posts… voila:

Full Code Below:


By use-r-friendly

Tags: rstats ggplot2 graphics

Textual Healing

While I know there are several awesome guides for how to make fine-tuning adjustments to plot labels/axes/ticks/text in ggplot, this is still the most common question I get from people new to ggplot: how do I change the size/font/color/position of (some text element)? Here are some examples. 

First, let’s load the made-up data from yesterday.

Our default plot looks like this:

#Adjust X Axis Label Size/Face/Color:

Plot.1 + theme(axis.title.x = element_text(face=”bold”, 

                                           colour=”dodgerblue”, 

                                           size=14) )

To Adjust Y Axis Label Size/Face/Color:

Plot.1 + theme(axis.title.y = element_text(face=”italic”, 

                                           colour=”darkred”, 

                                           size=24) )

When adjusting multiple parameters, you can use specify both within the “theme” command:

Plot.1 + theme(axis.title.x = element_text(face=”bold”, 

                                           colour=”dodgerblue”, 

                                           size=14),

               axis.title.y = element_text(face=”italic”, 

                                           colour=”darkred”, 

                                           size=24) )

Changing Main Title Text/Face/Color  

Plot.1 + theme(plot.title = element_text(family=”sans”,

                                           face=”bold”, 

                                           colour=”darkblue”, 

                                           size=44) )

When modifying the text in the legend, you can adjust either the legend title text…

Plot.1  + theme(legend.title = element_text(colour=”dodgerblue”, 

                                            size=26, 

                                            face=”bold”))

…or the text for the legend labels:

Plot.1 + theme(legend.text = element_text(colour=”goldenrod4”, 

                                          size = 18, 

                                          face = “italic”))

Or by doing both simultaneously:

Plot.1 + theme(legend.title = element_text(colour=”dodgerblue”, 

                                           size=26, 

                                           face=”bold”),

               legend.text = element_text(colour=”goldenrod4”, 

                                          size = 18, 

                                          face = “italic”))

Another option in the element_text() is angle, which is useful in certain occasions, especially when you have tight axis tick labels:

These can be adjusted using the angle command for the axis.text.x element:

Plot.1  + theme(axis.text.x = element_text(colour=”black”, 

                                           size = 11, 

                                           face = “bold.italic”,

                                           angle=45,

                                           vjust=1,

                                           hjust=1))

Tomorrow’s Post will show how to adjust the facet strips…stay tuned! Full code/script appears below:

By use-r-friendly

Tags: rstats graphics gglplot

Can’t a plot catch a break(s)?

This post continues with the theme of how to modify plots from within ggplot; today we will specifically looking at custom axis breaks. Plots later in the week will examine the commands to change text in the plot area. The various other shortcomings of this plot will be discussed in future posts.

To create custom breaks on the Y-axis:

By use-r-friendly

Tags: rstats ggplot graphics

Plotting letters as shapes in ggplot2

This post is a little more esoteric than most, but I found myself needing to solve this problem, so I’m just passing the solution on to you. The plot above shows the distribution of DW-NOMINATE scores for the 18th Congress, with party indicated by both color and shape. You will notice that there are more parties in 1824 than there are today — so many, in fact, that ggplot2 will resist plotting the seven shapes needed to account for each party. Note that I am confident that there is a good, peer-reviewed reason for this, so caveat emptor.

One work-around is to plot the initial letter of each party as a text geom, but in this case, the legend indicates the use of geom_text with an “a,” rather than an indicator for each shape. This is non-optimal, particularly if it’s not perfectly clear how the plotted letter symbols align with the party names:

The solution (or possibly, hack) is to use geom_shape, but use a custom scale that passes the numeric referents to each of the letters you want to use. To do so, we just need to choose the initial to use for each party name, and pass them to scale_shape_manual with utf8ToInt(). Incidentally, to manually look up the shape-to-numeric indicator correspondence, just run example(points).

By d-sparks

Tags: foreign ggplot2 devtools graphics rstats

GGtutorial: Day 5 - Gradient Colors and Brewer Palettes

So, continuing with the short tutorials on how to do relatively simple (but sometimes very frustrating things) in ggplot, today’s post looks at how to use gradient colors and Brewer colors to color either continuous or discrete dependent variables. 

If you have a continuous dependent variable, you’ll want to use a gradient option that gently transitions between two specified colors. Some examples are below:

This is the output:

If you want to choose colors for factors or discrete variables, you might want to use a Brewer color scale (http://colorbrewer2.org/):

This is the output:

By use-r-friendly

Tags: ggplot2 rstats graphics