Taking a simple plot from ggplot2 manual
p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
p + geom_hline(yintercept=20)
I get a horizontal line at value 20, as advertised.
Is there a way to limit the range of this line on x axis, to let's say2 - 4 range?
You can use geom_segment() instead of geom_hline() and provide x= and xend= values you need.
p+geom_segment(aes(x=2,xend=4,y=20,yend=20))
Related
Consider the following example:
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_cartesian(c(15, 20))
It sets the the limits of the x-axis, but the y limits remain as with the original plot, leaving a huge empty area.
Is it possible to automatically adjust y limits in this case? Similar to what
ggplot(mtcars[mtcars$mpg>15&mtcars$mpg<20,], aes(mpg, wt)) +
geom_point()
would produce.
Such automatism would make it unnecessary to manually calculate the y limits (which is not even trivial unless expand=0, as one has to take into account how y limits are expanded compared to what is provided).
Why don't you just set the y limits too?
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_cartesian(xlim = c(15, 20), ylim = c(2.5,4.5))
Of course you can calculate the limits beforehand with some function, but I'm not sure if that makes any sense, because to calculate the limits in a region, you will have to tell that function which are the limits of the region, which represents the same amount manual effort as putting those limits into the ggplot function directly.
Such a function could look like this:
find_ylimits <- function(data,xlim,overhead = 1){
filter <- xlim[1] <= data[[1]] & data[[1]] <= xlim[2]
c(min(data[[2]][filter])*overhead,
max(data[[2]][filter])*overhead)
}
And then you could make the plot as follows:
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_cartesian(xlim = c(15, 20), ylim = find_ylimits(mtcars[,c("mpg","wt")],c(15,20)))
I am trying to make a plot where I could just specify the min or the max value for the y axis. But I get Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
From the documentation:
You can leave one value as NA to compute from the range of the data.
Thus, I did:
#Getting some data in
plot <- ggplot(mydata,
aes_string(y="yvar", x="xvar", colour="group", group="group", fill="group")
)
#Adding some error bars
plot <- plot + geom_errorbar(aes(ymax=agg+var, ymin=agg-var), size=0.5, colour="black", data=mydata)
plot <- plot + geom_point(size=4)
plot <- plot + geom_line(size=1)
#Here is when I just want to set y max
plot <- plot + coord_cartesian(ylim= c(NA, 100))
If I remove the ylim or change the NA to a numeric value, it works well. What am I missing here?
You can use expand limits to extend the axis in only one direction. For example:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
expand_limits(y=c(NA, 50))
For your example, it would be:
plot + expand_limits(y=c(NA, 100))
You can even provide a single value. If that value is greater than the maximum of the data, it will expand the maximum. If lower than the minimum of the data, it will expand the minimum. In your example:
plot + expand_limits(y=100)
And here are two reproducible examples:
p = ggplot(mtcars, aes(wt, mpg)) +
geom_point()
p + expand_limits(y=-20)
p + expand_limits(y=200)
I am trying to create a plot with facets. Each facet should have its own scale, but for ease of visualization I would like each facet to show a fixed y point. Is this possible with ggplot?
This is an example using the mtcars dataset. I plot the weight (wg) as a function of the number of miles per gallon (mpg). The facets represent the number of cylinders of each car. As you can see, I would like the y scales to vary across facets, but still have a reference point (3, in the example) at the same height across facets. Any suggestions?
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
geom_hline (yintercept=3, colour="red", lty=6, lwd=1) +
facet_wrap( ~ cyl, scales = "free_y")
[EDIT: in my actual data, the fixed reference point should be at y = 0. I used y = 3 in the example above because 0 didn't make sense for the range of the data points in the example]
It's unclear where the line should be, let's assume in the middle; you could compute limits outside ggplot, and add a dummy layer to set the scales,
library(ggplot2)
library(plyr)
# data frame where 3 is the middle
# 3 = (min + max) /2
dummy <- ddply(mtcars, "cyl", summarise,
min = 6 - max(wt),
max = 6 - min(wt))
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
geom_blank(data=dummy, aes(y=min, x=Inf)) +
geom_blank(data=dummy, aes(y=max, x=Inf)) +
geom_hline (yintercept=3, colour="red", lty=6, lwd=1) +
facet_wrap( ~ cyl, scales = "free_y")
Is it possible to only set the lower bound of a limit for continuous scale? I want to make all my plots 0 based without needing to specify the upper limit bound.
e.g.
+ scale_y_continuous(minlim=0)
You can use expand_limits
ggplot(mtcars, aes(wt, mpg)) + geom_point() + expand_limits(y=0)
Here is a comparison of the two:
without expand_limits
with expand_limits
As of version 1.0.0 of ggplot2, you can specify only one limit and have the other be as it would be normally determined by setting that second limit to NA. This approach will allow for both expansion and truncation of the axis range.
ggplot(mtcars, aes(wt, mpg)) + geom_point() +
scale_y_continuous(limits = c(0, NA))
specifying it via ylim(c(0, NA)) gives an identical figure.
How about using aes(ymin=0), as in:
ggplot(mtcars, aes(wt, mpg)) + geom_point() + aes(ymin=0)
You can also try the following code which will give you the min y-axis at zero and also without the extra gap between x-axis and min y value.
scale_y_continuous(limits = c(0, NA), expand = c(0,0))
I don't think you can do this directly. But as a work-around, you can mimic the way that ggplot2 determines the upper limit:
scale_y_continuous(limits=c(0, max(mydata$y) * 1.1))
I'm having some trouble adding a vertical line to a plot when the x-axis is a datetime (POSIXct) object. It seems to always want to put the line at the Epoch. Here's an example:
df <- data.frame(x=ymd('2011-01-01')+hours(0:24), y=runif(25))
ggplot(df, aes(x=x,y=y)) + geom_point()
Now I try to add a line at the third observation time:
ggplot(df, aes(x=x,y=y)) + geom_point() + geom_vline(aes(x=df$x[3]))
Something I'm doing wrong?
Try doing this instead:
geom_vline(xintercept = df$x[3])
ggplot(df, aes(x=x,y=y)) + geom_point() + geom_vline(aes(xintercept=df$x[3]))
you want xintercept rather than x in your geom_vline aes.