Squared transform in coord ggplot2 - r

I have data where I think that y^2 ~ x.
So, I want to plot y as a function of x with some transformed scaled for y.
N <- 100
ggplot(data_frame(x = runif(N), y = 20 * sqrt(x) + rnorm(N)), aes(x, y)) +
geom_point()
+ scale_y_square??

You need to make a new transformation with scales::trans_new and to use it with coord_trans:
N <- 100
ggplot(data_frame(x = runif(N), y = 20 * sqrt(x) + rnorm(N)), aes(x, y)) +
geom_point() +
coord_trans(y = scales::trans_new("square", function(x) x^2, "sqrt"))

Related

log_2(x + 1) transformation in ggplot2

I'm trying to implement the log_2(x + 1) transformation in ggplot2 but am running into issues.
Here is an MWE
library(ggplot2)
x <- rexp(100)
y <- rexp(100)
df <- data.frame(x = x, y = y)
p <- ggplot(df, aes(x = x, y = y)) + geom_point(colour = "blue") +
scale_x_continuous(trans = "log2") +
scale_y_continuous(trans = "log2")
print(p)
However, I'm unsure how to best go about transforming the axes, as well as labelling the axes as log_2{x + 1) and log_2(y + 1).
You could use log2_trans from scales with a function to add 1 like this:
library(ggplot2)
library(scales)
x <- rexp(100)
y <- rexp(100)
df <- data.frame(x = x, y = y)
p <- ggplot(df, aes(x = x, y = y)) + geom_point(colour = "blue") +
scale_x_continuous(trans = log2_trans(),
breaks = trans_breaks("log2", function(x) x + 1),
labels = trans_format("log2", math_format(.x + 1))) +
scale_y_continuous(trans = log2_trans(),
breaks = trans_breaks("log2", function(x) x + 1),
labels = trans_format("log2", math_format(.x + 1)))
print(p)
Created on 2022-11-04 with reprex v2.0.2

Plot some points in contour curve from ggplot2

I'd like to plot a specific number of points of z in the contour curve, for example, 8 or 10 points. Below I show an example, but with all points.
library(ggplot2)
library(tidyverse)
rosenbrock <- function(x){
d <- length(x)
out <- 0
for(i in 1 : (d - 1)){
out <- out + 100 * ( x[i]^2 - x[i + 1] )^2 + (x[i] - 1)^2
}
out
}
set.seed(2020)
coord <- matrix(runif(2000, -100, 100), ncol = 2)
graph <- apply(coord, 1, rosenbrock)
results <- data.frame(x = coord[, 1], y = coord[, 2], z = graph)
results <- results %>% arrange(desc(z))
results %>%
ggplot(aes(x = x, y = y, z = z)) +
geom_point(aes(colour = z)) +
stat_density2d() +
theme_light()
You can set the alpha to equal zero when you originally plot the points, and then filter the data to include the points that you want (here, I just took a random sample):
results %>%
ggplot(aes(x = x, y = y, z = z)) +
geom_point(aes(colour = z), alpha=0) +
stat_density2d() +
geom_point(data = sample_n(results, 10), aes(colour = z)) +
theme_light()

How to add a legend in ggplot to my graph

Hi have some code to simulate a Gaussian process. Please can someone help me add a legend to my plots on the top right corner. I want to state the different parameter values for each of the line styles/colours, e.g. l=1, l=5, l=10. Thanks.
# simulate a gaussian process
simGP = function(K){
n = nrow(K)
U = chol(K) # cholesky decomposition
z = rnorm(n)
c(t(U) %*% z)
}
# choose points to simulate the covariance.
x = seq(-1, 1, length.out = 500)
# Exponential kernel ------------------------------------------------------
kernel_exp = function(x, l = 1) {
d = as.matrix(dist(x))/l
K = exp(-d)
diag(K) = diag(K) + 1e-8
K
}
{y1 = simGP(kernel_exp(x,l=10))
y2 = simGP(kernel_exp(x,l=1))
y3 = simGP(kernel_exp(x,l=0.1))
data1 <- as.data.frame(x,y1)
data2 <- as.data.frame(x,y2)
data3 <- as.data.frame(x,y3)
df=data.frame(data1,data2,data3)
ggplot() +
geom_line(data=data1, aes(x=x, y=y1), color="green4", linetype = "twodash", size=0.5) +
geom_line(data=data2, aes(x=x, y=y2), color='red', linetype="longdash", size=0.5) +
geom_line(data=data3, aes(x=x, y=y3), color='blue') +
scale_color_manual(values = colors) +
theme_classic() +
labs(x='input, x',
y='output, f(x)')+
theme(axis.text=element_text(size=16),
axis.title=element_text(size=14))}
You can do it using a dataframe variable to group the linetype and colour.
If you want to specify color and linetype, use scale_color_discrete and scale_linetype_discrete
y1 = simGP(kernel_exp(x,l=10))
y2 = simGP(kernel_exp(x,l=1))
y3 = simGP(kernel_exp(x,l=0.1))
data1 <- data.frame(x, y = y1, value = "10")
data2 <- data.frame(x, y = y2, value = "1")
data3 <- data.frame(x, y = y3, value = "0.1")
df=rbind(data1,data2,data3)
ggplot(data = df, aes(x=x, y=y, color = value, linetype = value, group = value)) +
geom_line(size=0.5) +
theme_classic() +
labs(x='input, x',
y='output, f(x)')+
theme(axis.text=element_text(size=16),
axis.title=element_text(size=14))

geom_line for a parametric function

Here is an example for visualizing Bezier curve:
library(ggplot2)
t = seq(0, 1, 0.001)
x0 = 4
y0 = 1
x1 = 28
y1 = 48
x2 = 50
y2 = 42
x3 = 40
y3 = 5
x = x0 * (1 - t)^3 + 3 * x1 * t * (1 - t)^2 + 3 * x2 * t^2 * (1 - t) + x3 * t^3
y = y0 * (1 - t)^3 + 3 * y1 * t * (1 - t)^2 + 3 * y2 * t^2 * (1 - t) + y3 * t^3
data = data.frame(x, y)
data1 = data.frame(x = c(x0, x2), xend = c(x1, x3), y = c(y0, y2), yend = c(y1, y3))
data2 = data.frame(x = c(x0, x1, x2, x3), y = c(y0, y1, y2, y3))
mplot = ggplot(data, aes(x, y)) + geom_line() + geom_segment(data = data1, aes(x = x, y = y, xend = xend, yend = yend)) + geom_point(data = data2, aes(x, y))
mplot
Result:
The right part of the curve has been rendered as an area. I guess this is because the y-values are not unique for certain x-values. How to solve this?
Instead of using geom_line() you may use geom_path():
ggplot(data, aes(x, y)) +
geom_path() +
geom_segment(data = data1, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_point(data = data2, aes(x, y))
... or even geom_point() would do the trick, but I guess you would have to play with point size to get what you want.

How do I place an identical smooth on each facet of a ggplot2 object?

Here's an example:
eg <- data.frame(x = c(1:50, 50:1),
y = c(1:50, 1:50) + rnorm(100),
g = rep(c("a","b"), each=50))
qplot(x, y, data = eg) +
facet_wrap(~ g) +
geom_smooth()
I'd like to be able to plot the overall smooth on both facets as well as having the facet-specific smooths.
Edit: here's one way.
my.smooth <- gam(y ~ s(x), data = eg)
my.data <- data.frame(x = 1:50)
my.data$y <- predict(my.smooth, newdata = my.data)
qplot(x, y, data = eg) +
facet_wrap(~ g) +
geom_smooth() +
geom_smooth(data = my.data)
Thanks for any help!
Andrew
Clever trick: setting the faceting variable to NULL
library(ggplot2)
eg <- data.frame(x = c(1:50, 50:1),
y = c(1:50, 1:50) + rnorm(100),
g = rep(c("a","b"), each=50))
p <- qplot(x, y, data = eg) +
facet_wrap(~ g) +
geom_smooth()
p + geom_smooth(data=within(eg, g <- NULL), fill="red")
Or if you prefer, use facet_grid(..., margins=TRUE):
p + facet_grid(.~g, margins=TRUE)

Resources