I want to use geom_ribbon in ggplot2 to draw shaded confidence ranges. But if one of the lines goes outside the set y limits, the ribbon is cut off without extending to the edge of the plot.
Minimal example
x <- 0:100
y1 <- 10+x
y2 <- 50-x
ggplot() + theme_bw() +
scale_x_continuous(name = "x", limits = c(0,100)) +
scale_y_continuous(name = "y", limits = c(-20,100)) +
geom_ribbon(aes(x=x, ymin=y2-20, ymax=y2+20), alpha=0.2, fill="#009292") +
geom_line(aes(x=x , y=y1)) +
geom_line(aes(x=x , y=y2))
What I want is to reproduce the same behaviour as I get with plotting in base R, where the shading extends to the edge
plot(x, y1, type="l", xlim=c(0,100),ylim=c(-20,100))
lines(x,y2)
polygon(c(x,rev(x)), c(y2-20,rev(y2+20)), col="#00929233", border=NA)
The problem is that limits is removing all data which are not within its range.
What you want is to first plot and then zoom in. This can be done by using coord_cartesian.
ggplot() + theme_bw() +
geom_ribbon(aes(x = x, ymin = y2 - 20, ymax = y2 + 20), alpha = 0.2, fill = "#009292") +
geom_line(aes(x = x, y = y1)) +
geom_line(aes(x = x, y = y2)) +
coord_cartesian(ylim = c(-25, 100), xlim = c(0,100))
Related
I want to plot a diagram with a log scales y axis:
data <- data.frame(x = seq_len(5), y = c(2,1,100,500,30))
ggplot(data) + aes(x,y) + geom_col() + scale_y_log10()
But because the horizontal axis always crosses the vertical axis at y = 1, the bar for y = 1 is never drawn.
I know that log(1) = 0, but can I set the plot base to 0.1 to let the bar start below 1?
Can't you just multiply the values of y by 10 but divide the labels by the same amount?
ggplot(data) + aes(x, y * 10) + geom_col() +
scale_y_log10(labels = function(x) x/10, name = "y")
You could do the same with a histogram:
set.seed(2)
ggplot(data.frame(x = rnorm(1000)), aes(x)) +
geom_histogram(aes(y = ..count.. * 10), color = "black", fill = "gold") +
scale_y_log10(labels = function(x) x/10, name = "count")
An alternative solution is to place the bottom of the bars at -Inf, so that the log10(1) still shows up. This is a bit tricky, because you have to reparameterise the bars as rectangles and get the geom to understand that the -Inf is after scale transformation.
library(ggplot2)
data <- data.frame(x = seq_len(5), y = c(2,1,100,500,30))
ggplot(data) +
geom_rect(
aes(xmin = x - 0.45, xmax = x + 0.45,
ymin = stage(1, after_scale = -Inf), ymax = y)
) +
scale_y_log10()
Created on 2020-11-03 by the reprex package (v0.3.0)
I want to know how to turn this plot:
Into this plot:
As you can see the panel and axis on the 2nd plot are limited to the data extent. I made the second graph using design software but want to know the code.
Ive already limited the x and y axis using
xlim and ylim but no difference.
Please see my code below, sorry its so messy, first time using r studio. Thanks!
ggplot() +
geom_errorbar(data = U1483_Coiling_B_M_Removed_R, mapping = aes(x = `Age (Ma) Linear Age Model`, ymin = `Lower interval*100`, ymax = `Upper interval*100`), width = 0.025, colour = 'grey') +
geom_line(data = U1483_Coiling_B_M_Removed_R, aes(x = `Age (Ma) Linear Age Model`, y = `Percent Dextral`)) +
geom_point(data = U1483_Coiling_B_M_Removed_R, aes(x = `Age (Ma) Linear Age Model`, y = `Percent Dextral`), colour = 'red') +
geom_point(data = U1483_Coiling_B_M_Removed_R, aes(x = `Age (Ma) Linear Age Model`, y = `Lab?`)) +
theme(axis.text.x=element_text(angle=90, size=10, vjust=0.5)) +
theme(axis.text.y=element_text(angle=90, size=10, vjust=0.5)) +
theme_classic() +
theme(panel.background = element_rect(colour = 'black', size = 1)) +
xlim(0, 2.85) +
ylim(0, 100)
You can use expand when specifying axis scales, like so:
# Load library
library(ggplot2)
# Set RNG
set.seed(0)
# Create dummy data
df <- data.frame(x = seq(0, 3, by = 0.1))
df$y <- 100 - abs(rnorm(nrow(df), 0, 10))
# Plot results
# Original
ggplot(df, aes(x, y)) +
geom_line() +
geom_point(colour = "#FF3300", size = 5)
# With expand
ggplot(df, aes(x, y)) +
geom_line() +
geom_point(colour = "#FF3300", size = 5) +
scale_y_continuous(expand = c(0, 0))
I need to combine a map, which represents an archaeological site, with an XY ggplot chart of different archaeological objects. The map is in a tiff file and must respect its proportion.
First, this is the map, highlighting the reference scales in red (in X axis, from -6000 to -4000 there are 20 meters of distance, for instance; in Y axis, from 900 to 2100 there are 12 meters).
My ggplot chart is obtained by running this code:
archaeo <- ggplot() +
geom_ellipsis(data=Unit_H,
aes(x0 = X, y0 = Y, a = Diameter_E.W/2+250, b = Diameter_N.S/2+250, angle = 0),
lwd=0, col="darkgray", fill="gray", alpha=0.15) +
geom_ellipsis(data=Unit_H,
aes(x0 = X, y0 = Y, a = Diameter_E.W/2+120, b = Diameter_N.S/2+120, angle = 0),
lwd=0, col="darkgray", fill="gray", alpha=0.25) +
geom_ellipsis(data=Unit_H,
aes(x0 = X, y0 = Y, a = Diameter_E.W/2, b = Diameter_N.S/2, angle = 0),
lwd=0.5, col="darkgray", fill="gray", alpha=0.75) +
geom_point(data=Unit_H, aes(X, Y), size = 0.5) +
geom_point(data=Refits_H_trans, aes(x,y,group=sample, colour=factor(sample))) +
geom_line(data=Refits_H_trans, lwd=0.2, lty=1, aes(x,y, group=sample, colour=factor(sample))) +
coord_fixed() +
theme_bw() +
theme(legend.position="none") +
ggtitle("Unit H") +
xlim(-6600,-3800) +
ylim(400,2400)
The resulting chart is:
Now, my problem, which deals with the inclusion of the map as background of the ggplot. I used background_image() from ggpubr, with this result:
map_levelH <- readPNG("Planta H-I.png")
Map.archaeo <- ggplot() +
background_image(map_levelH) +
geom_ellipsis(data=Unit_H,
aes(x0 = X, y0 = Y, a = Diameter_E.W/2+250, b = Diameter_N.S/2+250, angle = 0),
lwd=0, col="darkgray", fill="gray", alpha=0.15) +
geom_ellipsis(data=Unit_H,
aes(x0 = X, y0 = Y, a = Diameter_E.W/2+120, b = Diameter_N.S/2+120, angle = 0),
lwd=0, col="darkgray", fill="gray", alpha=0.25) +
geom_ellipsis(data=Unit_H,
aes(x0 = X, y0 = Y, a = Diameter_E.W/2, b = Diameter_N.S/2, angle = 0),
lwd=0.5, col="darkgray", fill="gray", alpha=0.75) +
geom_point(data=Unit_H, aes(X, Y), size = 0.5) +
geom_point(data=Refits_H_trans, aes(x,y,group=sample, colour=factor(sample))) +
geom_line(data=Refits_H_trans, lwd=0.2, lty=1, aes(x,y, group=sample, colour=factor(sample))) +
coord_fixed() +
theme_bw() +
theme(legend.position="none") +
ggtitle("Unit H") +
xlim(-6600,-3800) +
ylim(400,2400)
As you can see, scales of the ggplot and the map don't match. So, my questions are:
How can I georeference the map with the values of the ggplot X and Y axes?
I need to keep the proportion of the image, in order not to distort it. How can I do it? I am asking this because if I change the xlim values, the image also change and its proportion changes.
I've recreated a simple example where this problem exists:
library(tidyverse)
# create the background
bck_square <- data.frame(x=c(1,1,0,0),y=c(0,1,1,0))
p <- ggplot(bck_square, aes(x=x, y=y)) +
geom_point(size=10, color="red") +
theme(panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
#panel.background=element_blank(), keep the background to see where image ends
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank())
p
I will save the image to be used as a background for my figures:
ggsave("temp.png",p)
img <- readPNG("temp.png")
Setting the background using background_image from the ggpubr package, the old square does not line up with the new square, even though the data is the same. This is expected, as ggsave adds a thin border around the image
library(ggpubr)
ggplot(bck_square, aes(x, y)) +
background_image(img) +
geom_point()
However, by using annotation_custom instead (see this guide), you can adjust where the minimum and maximum for the image should be. Playing around with the border parameters I was able to get the image background and figure to line up.
library(png)
library(grid)
min_border <- .064
max_border <- .061
ggplot(bck_square, aes(x, y)) +
annotation_custom(g,xmin=-min_border, xmax=1+max_border, ymin=-min_border, ymax=1+max_border) +
geom_point()
This method should work with a tiff file. Another potential solution could be spatial data transformations using rspatial (see the documentation here) but this may over-complicate the issue.
I've coded this:
ggplot() +
geom_point(mapping = aes(x = X, y = y)) +
geom_abline(intercept = -0.9930872, slope = 0.4866284, colour = "red") +
geom_abline(intercept = -1, slope = 0.5, colour = "blue")
but cannot seem to get a working legend for my least square and populuation regression line. I've tried various stack overflow answers but nothing seems to give me what I need.
Add a legend to a ggplot2 scatter plot including additional lines
This looked like the best answer, but I can't get it to work!
Any suggestions?
set.seed(1234)
X <- rnorm(20,sd=2.5)
y <- -1+0.5*X+rnorm(20, sd=0.4)
library(ggplot2)
ggplot() +
geom_point(mapping = aes(x = X, y = y)) +
geom_abline(aes(intercept = -0.9930872, slope = 0.4866284, colour = "line1"), lwd=1) +
geom_abline(aes(intercept = -1, slope = 0.5, colour = "line2"), lwd=1) +
scale_colour_manual(values=c("line1"="red","line2"="blue"))
With slight modification your code works just fine:
ggplot() +
geom_point(mapping = aes(x = X, y = y)) +
geom_abline(aes(colour = "line_1", intercept = -0.9930872, slope = 0.4866284)) +
geom_abline(aes(colour = "line_2", intercept = -1, slope = 0.5)) +
scale_colour_manual(name = "lines", values = c("red", "blue")) +
theme(legend.position = "bottom")
Added legend position in case if you want to change that aswell.
Is there a method to overlay something analogous to a density curve when the vertical axis is frequency or relative frequency? (Not an actual density function, since the area need not integrate to 1.) The following question is similar:
ggplot2: histogram with normal curve, and the user self-answers with the idea to scale ..count.. inside of geom_density(). However this seems unusual.
The following code produces an overinflated "density" line.
df1 <- data.frame(v = rnorm(164, mean = 9, sd = 1.5))
b1 <- seq(4.5, 12, by = 0.1)
hist.1a <- ggplot(df1, aes(v)) +
stat_bin(aes(y = ..count..), color = "black", fill = "blue",
breaks = b1) +
geom_density(aes(y = ..count..))
hist.1a
#joran's response/comment got me thinking about what the appropriate scaling factor would be. For posterity's sake, here's the result.
When Vertical Axis is Frequency (aka Count)
Thus, the scaling factor for a vertical axis measured in bin counts is
In this case, with N = 164 and the bin width as 0.1, the aesthetic for y in the smoothed line should be:
y = ..density..*(164 * 0.1)
Thus the following code produces a "density" line scaled for a histogram measured in frequency (aka count).
df1 <- data.frame(v = rnorm(164, mean = 9, sd = 1.5))
b1 <- seq(4.5, 12, by = 0.1)
hist.1a <- ggplot(df1, aes(x = v)) +
geom_histogram(aes(y = ..count..), breaks = b1,
fill = "blue", color = "black") +
geom_density(aes(y = ..density..*(164*0.1)))
hist.1a
When Vertical Axis is Relative Frequency
Using the above, we could write
hist.1b <- ggplot(df1, aes(x = v)) +
geom_histogram(aes(y = ..count../164), breaks = b1,
fill = "blue", color = "black") +
geom_density(aes(y = ..density..*(0.1)))
hist.1b
When Vertical Axis is Density
hist.1c <- ggplot(df1, aes(x = v)) +
geom_histogram(aes(y = ..density..), breaks = b1,
fill = "blue", color = "black") +
geom_density(aes(y = ..density..))
hist.1c
Try this instead:
ggplot(df1,aes(x = v)) +
geom_histogram(aes(y = ..ncount..)) +
geom_density(aes(y = ..scaled..))
library(ggplot2)
smoothedHistogram <- function(dat, y, bins=30, xlabel = y, ...){
gg <- ggplot(dat, aes_string(y)) +
geom_histogram(bins=bins, center = 0.5, stat="bin",
fill = I("midnightblue"), color = "#E07102", alpha=0.8)
gg_build <- ggplot_build(gg)
area <- sum(with(gg_build[["data"]][[1]], y*(xmax - xmin)))
gg <- gg +
stat_density(aes(y=..density..*area),
color="#BCBD22", size=2, geom="line", ...)
gg$layers <- gg$layers[2:1]
gg + xlab(xlabel) +
theme_bw() + theme(axis.title = element_text(size = 16),
axis.text = element_text(size = 12))
}
dat <- data.frame(x = rnorm(10000))
smoothedHistogram(dat, "x")