Adding equations to ggplots in R [duplicate] - r

This question already has answers here:
Add regression line equation and R^2 on graph
(10 answers)
Closed 6 years ago.
I'd like to manually add a regression equation to a plot that includes the proper mathematical notation (e.g., italics, superscripts). Annotate() seems perfect for this, but it doesn't seem to accept the expression() function in the labels argument. Here is a simple example. The annotation with expression() in it won't display. What am I doing wrong?
library(ggplot2)
x <- 1:10
y <- x^2
df <- data.frame(x, y)
sp <- ggplot(df, aes(x, y)) + geom_point()
sp + annotate('text', label = expression('y = x^2'), x = 2.5, y = 50) +
annotate('text', label = 'y = x^2', x = 2.5, y = 75)

Use geom_text instead of annotate:
sp + geom_text(aes(2.5,75, label=(paste(expression("y = x "^-2*"")))),parse = TRUE)

Related

how to only display decimals in ggplot axis tick labels? [duplicate]

This question already has answers here:
R - ggplot axis number format - remove leading zero
(2 answers)
Number formatting in labels in ggplot2?
(1 answer)
Closed 9 months ago.
How can I get ggplot to display only decimals in the axis tick labels?
Consider a toy dataframe like the following, for which all the variables are < 1 in absolute value:
set.seed(42)
x <- rnorm(n=500, sd = 0.1)
y <- 0.05 + 2*x + rnorm(n=500, sd = 0.05)
df <- data.frame(x,y)
summary(df)
library(ggplot2)
ggplot(data = df, aes(x = x, y = y)) +
geom_point()
Is it possible to have .xx instead of 0.xx on the axis?
You can pass a function to the labels argument of the scale_x_continuous(), in this case a lambda style function:
ggplot(data = df, aes(x = x, y = y)) +
geom_point() +
scale_x_continuous(labels=~sub("^(-?)0.", "\\1.", sprintf("%.1f", .x)))

ggplot a vector in R [duplicate]

This question already has answers here:
How to plot one variable in ggplot?
(5 answers)
Closed 2 years ago.
I can simply plot a vector in R language using plot, like this:
vec <- sqrt(1:100)
plot(vec, type = "l")
But I want to plot this vector using ggplot2 because its plots are better and more beautiful. But I'm struggling with it. any help would be appreciated.
Try this:
ggplot(as.data.frame(vec)) +
geom_point(aes(vec, sqrt(vec)))
It works, but I would like to advise you to create a dataframe before making the plot.
Let's say you want a plot with lines and/or points. One way to have control over what you do is:
Create a dataframe with an x ​​column and a y column
df <- data.frame(x = 1:100, y = 1:100)
pass the dataframe to ggplot()
ggplot(df)
add the geom you want by defining x and y in aes
ggplot(df) +
geom_point(aes(x = x, y = y)) +
geom_line(aes(x = x, y = y))
customize your plot
Note 1: in step 3 you can define aes also in ggplot to not repeat the code:
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_point()
Note 2: in aes, x and y on the left of = are the names of the parameters, while x and y on the right of = are the names of the columns of the dataframe. The names of the x and y parameters can be omitted and you can leave only the name of the columns of the dataframe
Thanks to the good answer of Leonardo, the short answer to my question (to graph a vector vec via ggplot) would be this:
d <- data.frame(x = 1:length(vec), y = vec)
ggplot(d, aes(x, y)) + geom_line()

How to draw a "U" shapped graph in R [duplicate]

This question already has answers here:
What type of graph is this? And can it be created using ggplot2?
(2 answers)
Closed 2 years ago.
I need to plot a "U-Shape" graph with ggplot, but i didn't get a nicer result with this code:
df <- data.frame(y = seq(0,8,1),
x = c(10,8,7,3,1,3,7,8,10))
ggplot(df, aes(y = y)) +
geom_line(aes(x = x))
can someone give me a help?
Something like this?
x = seq(-10,10,1)
df <- data.frame(x = x, y = x^2)
ggplot(df, aes(y = y, x = x)) +
geom_line()
It looks like geom_path is what you're looking for here, rather than geom_line. geom_path connects the values in the order they're in the data frame, while geom_line in the order along the x-axis.
df <- data.frame(y = seq(0,8,1),
x = c(10,8,7,3,1,3,7,8,10))
ggplot(df, aes(y = y)) +
geom_path(aes(x = x))
This is one possible solution.
library(ggplot2)
f <- function(x) 1 + x^2
x <- seq(-10, 10)
y <- f(x)
ggplot(data.frame(x, y), aes(x=x, y=y)) + geom_line()

How to add greek letters to a label in geom_text() label in ggplot2 [duplicate]

This question already has answers here:
How to use Greek symbols in ggplot2?
(4 answers)
Closed 2 years ago.
I am trying to label the tiles in geom_tile with the Greek symbol, kappa, as follows
kappa = value
I've tried using expression() and bquote() but cannot find a way to make it work
df = data.frame(x = letters[1:10],y = letters[11:20], value = 1:10)
p = ggplot(df, aes(x = x, y = y)) +
geom_tile()+geom_text(aes(label= paste("k = ",value,"")), color = "white")
p
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/5bDLv.png
use parse = TRUE
also learn more about mathematical expressions used in plot by following this: ?plotmath
ggplot(df, aes(x = x, y = y)) +
geom_tile() +
geom_text(mapping = aes(label = paste('kappa', "==", value)), parse = TRUE, color = "white")

How to mix a character and a numerical variable in axis label of ggplot2 [duplicate]

This question already has an answer here:
How to use simultaneously superscript and variable in a axis label with ggplot2
(1 answer)
Closed 5 years ago.
Hi I have written a simple function to make a plot of a-th polynomial of x in ggplot2. Following is my code:
PlotPower <- function(x,a){
y <- x^a
dat <- data.frame(x,y)
f <- function(x) x^a
ggplot(dat,aes(x,y)) + geom_point() + stat_function(fun = f, colour = "red") +
scale_x_continuous(breaks = pretty(dat$x, n = 10)) +
scale_y_continuous(breaks = pretty(dat$y, n = 10)) +
labs(x = "x", y = expression(x^a), title = "Polynomial plot of x")
}
Now say I am calling PlotPower(1:10,3), the plot looks like
But I want y axis label to automatically change into x^3. I have not been able to do that. I have tried to write
labs(y = "x"^a)
but I am getting error message: Error in "x"^a : non-numeric argument to binary operator, which is expected as I am mixing a character variable with a numerical variable.
Please help me writing the ggplot2 y axis label properly inside my PlotPower function so that with whatever "a" I call the PlotPower function, I get y axis label as x^a, where x is the character x but a is the value of argument a.
Replace the expression with bquote. The variable inside .(a) is the one that gets substituted. For more variations -like the ones mentioned in the comments- look at this answer.
PlotPower <- function(x,a){
y <- x^a
dat <- data.frame(x,y)
ylab = paste("x", a, sep='^')
f <- function(x) x^a
ggplot(dat,aes(x,y)) + geom_point() + stat_function(fun = f, colour = "red") +
scale_x_continuous(breaks = pretty(dat$x, n = 10)) +
scale_y_continuous(breaks = pretty(dat$y, n = 10)) +
labs(x = "x", y = bquote(x^.(a)), title = "Polynomial plot of x")
}

Resources