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.
Related
ggplot(data=df, aes(x='Matcing_Probability', y=Locus_Name, group=1)) +
+ geom_line(color="#aa0022", size=1.75) +
+ geom_point(color="#aa0022", size=3.5)
This is the graph I am getting from the code.
You need to send ggplot2 symbols (unquoted column names) in aes() if you are assigning an aesthetic to a column in your dataset. Otherwise, it will assume you are sending the string of a new symbol. So:
# your original
ggplot(data=df, aes(x='Matching_Probability', y=Locus_Name, group=1))
# change to this:
ggplot(data=df, aes(x=Matching_Probability, y=Locus_Name, group=1))
Consider the difference in the following example to highlight why even more:
# this works fine
df <- data.frame(x=1:10, y=1:10)
ggplot(df, aes(x=x,y=y)) + geom_point()
# this doesn't:
ggplot(df, aes(x="x",y=y)) + geom_point()
I have the following data frame:
df <- data.frame(x=c(1,2,3,4,5),
y=c(2,3,5,9,9),
label=c('blah1','blah2','blah3','blah4','blah5'),
vjust=c('top','bottom','top','bottom','top'),
posVjust=c(0.9,1.1,0.9,1.1,0.9),
stringsAsFactors=FALSE)
and can plot it directly like so:
p <- ggplot(df, aes(x=x,y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(vjust=vjust))
p
However, I'd like to use the posVjust column as part of geom_text's aes but I can't like so:
geom_text(aes(vjust=vjust,position=position_stack(vjust=posVjust)))
I get the following error:
Warning: Ignoring unknown aesthetics: position
> p
Don't know how to automatically pick scale for object of type
PositionStack/Position/ggproto. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (5): vjust,
position, x, y, label
Is there a way to use my posVjust column as part of the position_stack call?
position isn't an aesthetic and goes outside of aes. As far as I know, position_stack takes a single value, rather than a vector. However, you could change posVjust to be posVjust=c(-0.1,0.1,-0.1,0.1,-0.1) and then do the following:
ggplot(df, aes(x=x, y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(y=y + posVjust))
You could also dispense with posVjust and just do:
ggplot(df, aes(x=x, y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(y=y + c(-0.1,0.1)))
You can add vjust=vjust as well, which will add a small additional increment of vertical offset.
Another option is to remove the points and just use labels instead of point markers. Offsetting the geom_text labels then becomes unnecessary. For example:
ggplot(df, aes(x=x, y=y, label=label)) +
geom_line(linetype="12", colour="grey50") +
geom_text() +
theme_bw()
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.
I am trying to plot the outliers and mean point for the box plots in below using the data available here. The dataset has 3 different factors and 1 value column for 3600 rows.
While I run the below the code it shows the mean point but doesn't draw the outliers properly
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
Again, while I am modify the code like in below the mean points disappear !!
ggplot(df, aes(x=Representations, y=Values, colour=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
In both of the cases I am getting the message: "ymax not defined: adjusting position using y instead" 3 times.
Any kind suggestions how to fix it? I would like to draw the mean points within individual box plots and show outliers in the same colour as the plots.
EDIT:
The original data set does not have any outliers and that was reason for my confusion. Thanks to MrFlick's answer with randomly generated data which clarifies it properly.
Rather than downloading the data, I just made a random sample.
set.seed(18)
gg <- expand.grid (
Methods=c("BC","FD","FDFND","NC"),
Metrics=c("DM","DTI","LB"),
Representations=c("CHG","QR","HQR")
)
df <- data.frame(
gg,
Values=rnorm(nrow(gg)*50)
)
Then you should be able to create the plot you want with
library(ggplot2)
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
stat_summary(fun.y="mean", geom="point",
position=position_dodge(width=0.75), color="white") +
facet_wrap(~Metrics)
which gave me
I was using ggplot2 version 0.9.3.1
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))