Labeling points using qplot in R - r

I'm having trouble labeling points in R. I've created a qplot that uses four numeric variables I'm plotting as the x and y axes, the color of the points and the size of the points. When I try to add the labels by just including label = player (where player is the column name with the labels I want) R says: "Error: object 'Player' not found." Maybe because this is the only text column? This is probably really simple, but my first plot, so...

qplot(cars$dist, cars$speed) + geom_text(label = cars$dist)
You can append normal ggplot syntax to qplot() exactly the same way you would when calling ggplot().
You need to specify the source of the data you are feeding: you can do so by passing the name of the dataframe to the data argument of a geom() and then referencing a specific column ('Player'), in quotes, in the aes() call within the same geom():
geom_point(data = data, aes(x = 'col1', y = 'col2'))
or you can attach() the data, and then just specify the column (without quotes or the data= parameter):
geom_point(aes(x = col1, y = col2))
Thank you to Marius for pointing out the notion that referencing data through the data parameter may be preferential over $ (data$col) in certain situations like facetting.

Related

geom_mosaic and aes magic

I use the following library for mosaic plots. In the documentation it provides the following example:
ggplot(data = fly) +
geom_mosaic(aes(x = product(DoYouRecline, RudeToRecline), fill=DoYouRecline)
So the DoYouRecline and RudeToRecline are x and y variables in a plot, but for some reason they are combined with product function which returns a list of names. How does geom_mosaic internally use product(DoYouRecline, RudeToRecline) in order to figure out that the two parameters need to be used for x and y axis?
The main problem that I have is that I need to have one of those attribute names as a variable, for instance something like this:
attr_name = 'foo'
ggplot(data = fly) +
geom_mosaic(aes(x = product(DoYouRecline, attr_name), fill=DoYouRecline)
Thanks!

How can I use superscript in axis labels which are created dynamically from a character vector in R?

I'm trying to make a script that will make one graph from each column of my dataset, using ggplot. I would like to have the y labels dynamically sourced from a separate dataframe, so I cannot construct them manually for each graph by hand. So far, so easy.
At the same time (and this is where the problem comes in), the y axis labels need to include superscripts - for example, "x10^9/l", with the "9" being the only superscript character. I tried using parse(), but it fails, saying that the "/" is unexpected.
In essence, the core script looks like this, with the source data being in the "data" dataframe and the y axis labels being in the "labels" dataframe, the whole thing being enclosed in a for() loop iterating through the columns of "data" by increasing "i":
ggplot(data, aes_string(x = colnames(data)[k], y = colnames(data)[i], color = "Category", group = "Patient")) +
ggtitle(labels[which(labels$name==colnames(data)[i]),]$nice_name) +
ylab(label = parse(text=labels[which(labels$name==colnames(data)[i]),]$label))
Thanks a lot for your help!

Modify labels in facet_grid on existing ggplot2 object

Suppose we have the following dataset:
d = data.frame(
y = rnorm(100),
x = rnorm(100),
f1 = sample(c("A", "B"), size=100, replace=T)
)
And I want to plot the data using facets:
require(ggplot2)
plot = ggplot(d, aes(x,y)) +
facet_grid(~f1, labeller = labeller(.cols=label_both))
Now let's suppose I want to capitalize all columns. It's trivial to do so with the x/y variables:
plot + labs(x="X", y="Y")
But how do I go about capitalizing the facet labels?
The obvious solutions are:
Just change the name of the variable (e.g., d$F1 = d$f1) then rerun the code.
Create a custom labeller that capitalizes the variable names
However, I cannot do either of these in my current application. I cannot change the original ggplot object; I can only layer (e.g., as I do with the x/y axis labels) or I can modify the ggplot object directly.
So, is there a way to change the facet labels by either modifying the ggplot object directly or layering it?
Fortunately, I was able to solve my own problem by creating my MWE. And, rather than keep that knowledge to myself, I figured I'd share it with others (or future me if I forget how to do this).
ggplot objects can be easily dissected using str
In this case, the ggplot object (plot) can be dissected:
str(plot)
Which lists many objects, including one called facet, which can be further dissected:
str(plot$facet)
After some trial and error, I found an object called plot$facet$params$cols. Now, using the following code:
names(plot$facet$params$cols) = "F1"
I get the desired result.

ggplot function like points

Is there any way to add points to a ggplot graph like with the points() function in base graphics? I don't often use ggplot and always prefer base graphics, but this time I must to deal with it. With + geom_point(x = c(1,2,3), y = c(1,2,3)) there is an error:
Error: Aesthetics must be either length 1 or the same as the data (33049): x, y
I'm not quite sure what you're looking for, but you can use the data= argument to geom_point() to override the default behaviour (which is to inherit data from the original ggplot call); as #dc37 points out, x and y need to be specified within a data frame, but you can do this on the fly. You might also need to specify the mapping, if the original x and y variables aren't called x and y ...
+ geom_point(data= data.frame(x = c(1,2,3), y = c(1,2,3)),
mapping = aes(x=x, y=y))
Alternatively (and maybe better):
+ annotate( geom="point", x = 1:3, y = 1:3)
From ?annotate:
This function adds geoms to a plot, but unlike [a typical] geom
function, the properties of the geoms are not mapped from
variables of a data frame, but are instead passed in as vectors.
This is useful for adding small annotations (such as text labels)
or if you have your data in vectors, and for some reason don't
want to put them in a data frame.

What does the ".." refer to in ggplot's "fill=..density.."?

I am working my way through The R Graphics Cookbook and ran into this set of code:
library(gcookbook)
library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point() +
stat_density2d(aes(alpha=..density.., fill=..density..), geom="tile", contour=FALSE)
It runs fine, but I don't understand what the .. before and after density is referring to. I can't seem to find it mentioned in the book either.
Variable names beginning with .. are possible in R, and are treated in the same way as any other variable. Trying creating one of your own.
..x.. <- 1:5
ggplot2 often creates appends extra columns to your data frame in order to draw the plot. (In ggplot2 terminology, this is "fortifying the data".) ggplot2 uses the naming convention ..something.. for these fortified columns.
This is partly because using ..something.. is unlikely to clash with existing variables in your dataset. Take that as a hint that you shouldn't name the columns in your dataset using that pattern.
The stat_density* functions use ..density.. to represent the density of the x variable. Other fortified variable names include ..count...

Resources