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

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)))

Related

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 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")
}

Tick division and labels causing confusion in ggplot2 [duplicate]

This question already has answers here:
How to display only integer values on an axis using ggplot2
(13 answers)
Closed 6 years ago.
When I plot the example below the x-axis labels are confusing since the ticks labeled 2008 and 2012 are 2008.5 and 20012.5. I fiddled and realized that the number format gets truncated because it's so long. What's a good way to floor the ticks and labels?
library(ggplot2)
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1
EDIT: THis is indeed a duplicate question:
How to display only integer values on an axis using ggplot2
My apologies.
This two options could be an idea....
option 1:
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1 <- p1 + scale_x_continuous(breaks = foo$x)
p1
option 2:
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1 <- p1 + scale_x_continuous(breaks = foo$x[seq(1, length(foo$x), 2)])
p1

Adding equations to ggplots in R [duplicate]

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)

Resources