Write ggplot2 layers on different lines - r

This might by a silly question but I'm adding quite a few layers on a ggplot2 and this makes finally a huge line on screen, quite difficult to read.
Say I want to write:
p <- ggplot(mydata, aes(x,y))
+ geom_point()
+ geom_contour(data = another_df, aes(z=z))
+ etc.
Instead of having:
p <- ggplot(mydata, aes(x,y)) + geom_point() + geom_contour(data = another_df, aes(z=z)) + etc.
for an easy reading of the code. This returns an error in R because it's not all in the same line. How could I do that ? I have tried to add a c( ... ) but it creates a list an not a plot.
thanks

Just leave the + at the end of the previous line, not the start of the next:
p <- ggplot(mydata, aes(x,y)) +
geom_point() +
geom_contour(data = another_df, aes(z=z)) +
....
R won't let you do:
1
+ 2
and get the answer you were looking for. It's exactly the same with your example except an error is thrown because the method for + is expecting 2 arguments and is only getting one.

Related

Trouble using facet_wrap

I'm a newbie, so this might be super basic.
When I run this code
Dplot <- qplot(x = diamonds$carat, y = diamonds$price, color = diamonds$color) +
ggtitle("An A+ plot") +
xlab("carat") +
ylab("price") +
geom_point()
Dplot <- Dplot + facet_wrap(vars(diamonds$clarity))
Dplot
I get an error message that says:
Error in gList(list(x = 0.5, y = 0.5, width = 1, height = 1, just =
"centre", :
only 'grobs' allowed in "gList"
I've tried googling, but haven't been able to figure out what the issue is.
I would advise against using qplot except in the most basic cases. It teaches bad habits (like using $) that should be avoided in ggplot.
We can make the switch by passing the data frame diamonds to ggplot(), and putting the mappings inside aes() with just column names, not diamonds$. Then the facet_wrap works fine as long as we also omit the diamonds$:
Dplot = ggplot(diamonds, aes(x = carat, y = price, color = color)) +
ggtitle("An A+ plot") +
xlab("carat") +
ylab("price") +
geom_point()
Dplot + facet_wrap(vars(clarity))
Dplot + facet_wrap(~ clarity) # another option
Notice the code is actually shorter because we don't need to type diamonds$ all the time!
The vars(clarity) option works fine, more traditionally you would see formula interface used ~ clarity. The vars() option is new-ish, and will play a little nicer if you are writing a function where the name of a column to facet by is stored in a variable.

No line in plot chart despite + geom_line()

I've read documentation and I think that my code should be right, but still there is no line between the points in the output. What is wrong?
The x'axis is discrete and y'axis is continuous.
My code
point.sqrmPrice <- ggplot(overview.df, aes(x = areaSize, y = sqrmPrice)) +
geom_line() +
geom_point() +
scale_y_continuous(breaks = c(seq(min(overview.df$sqrmPrice), max(overview.df$sqrmPrice), by = 10000) )) +
theme_bw()
The underlying issue here is a duplicate of this stack post.
Here's a reproducible example showing what #SN248 meant about adding group to the code
ggplot(iris, aes(x = factor(Sepal.Length), y = Sepal.Width)) +
geom_line(aes(group=1)) + geom_point() + theme_bw()
You are not getting a line because areaSize is a factor. Convert to numeric with
overview.df$areaSize <- as.numeric(as.character(overview.df$areaSize))
and then make the plot.
What you have to think about it is, do you expect a single line to connect all the dots?
Else, how many lines do you expect, that will tell you how many groups will you need to have.
You are missing the group aesthetic required for geom_line(), because you haven't specified how many groups (lines) you want in your plot.

`geom_abline` and `facet_wrap` seem incompatible

I get errors when I use geom_abline in the same plot as facet_wrap or facet_grid, and I don't understand why. For example
# Example data
ex <- data.frame(x=1:10, y=1:10, f=gl(2, 5))
ggplot() +
geom_point(data=ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
causes Error in if (empty(data)) { : missing value where TRUE/FALSE needed.
Above I set the data in the geom_point layer because later on I will add data from a different data frame. This has something to do with the problem, because when I set the data in the base layer I get a different error:
ggplot(ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Error in as.environment(where) : 'where' is missing
Workaround
There's an easy workaround: If I make a data frame to define a 1:1 line and plot it using geom_line I get essentially the same plot I would have gotten from geom_abline...
# Define a 1:1 line with data
one_to_one <- data.frame(xO=range(ex$totalcells), yO=range(ex$totalcells))
# Plot the 1:1 line with geom_line
ggplot() +
geom_point(data=ex, aes(x=x, y=y)) +
geom_line(data=one_to_one, aes(x=xO, y=yO), colour="black") +
facet_wrap(~f)
...so this question is more about why those errors arise (and whether they represent a bug or expected behavior) rather than how to work around the problem.
The following works:
ggplot(ex, aes(x=x, y=y)) + geom_point() +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Note the additional geom_point() I added, based on your second example.

ggplot removes all my data when I set x and y limits

I'm using R to generate a plot from a table.
I used the command a <- read.table("table.txt") and that worked fine. When I type "a" it prints out my full table correctly.
I also entered library(ggplot2) so I could use ggplot.
Here was my first command:
ggplot(a, aes(x=V2, y=V5, group=V7)) +
geom_point(size=4, aes(col=V7)) + xlab("Rank") +
ylab("Inter-helix angle (Degree)") +
opts(legend.position="none")
this command generated all the points, but when I added xlim(0,110) + ylim(0,110) to the end, it gave me a warning and said "Removed 101 Rows containing missing values" (my table is 101 rows long). The plot that was generated was completely empty. What happened?
I'm guessing perhaps your data falls out of the ranges you have specified in xlim and ylim e.g.:
library(ggplot2)
df <- data.frame(x=1:10,y=1:10)
ggplot(a, aes(x=x, y=y)) +
geom_point(size=4) + xlab("Rank") +
ylab("Inter-helix angle (Degree)")
works fine, but:
ggplot(df, aes(x=x, y=y)) +
geom_point(size=4) + xlab("Rank") +
ylab("Inter-helix angle (Degree)") + xlim(0,0.1) + ylim(0,0.1)
gives the same error as you get because no values fall between 0 and 0.1
Warning message:
Removed 10 rows containing missing values (geom_point).
Although without your data this is just the most likely answer. By the way in general folk tend to use coord_cartesian:
ggplot(df, aes(x=x, y=y)) +
geom_point(size=4) + xlab("Rank") +
ylab("Inter-helix angle (Degree)") + coord_cartesian(xlim=c(0,0.1)) +
coord_cartesian(ylim=c(0,0.1))

How to format number values for ggplot2 legend?

I am working on finishing up a graph generated using ggplot2 like so...
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue))
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)
I have attached the result and you can see the numeric values in the legend for Revenue do not have a comma. How can I add a comma to those values? I was able to use scale_y_continuous for the axis, can that be used for the legend also?
Just to keep current, in ggplot2_0.9.3 the working syntax is:
require(scales)
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(labels=comma) +
scale_colour_continuous(labels=comma)
Also see this exchange
Note 2014-07-16: the syntax in this answer has been obsolete for some time. Use metasequoia's answer!
Yep - just a matter of getting the right scale_colour_ layer figured out. Try:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors, colour = Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
I personally would also move my the colour mapping to the geom_point layer, so that it doesn't give you that odd line behind the dot in the legend:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors)) +
geom_point(aes(colour = Revenue)) +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
...as I stumbled over this older thread, maybe it makes sense to add you need to load library("scales"), otherwise you get the following error message
Error in check_breaks_labels(breaks, labels) : object 'comma' not found

Resources