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

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

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

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

How to make ggplot lines run to the edge?

I have data (depth over time) that I want to display with a line plot. For clarity, I want to zoom in on a section but still show the user that the data continues outside the bounds of the plot. So I want the lines to stop at the plot's edge, rather than at the last point. This is straightforward enough in base graphics but I can't make it work in ggplot. Here's an example with base:
d <- data.frame(x = 1:10, y = 1:10)
plot(d$x, d$y, xlim = c(2,9))
lines(d$x, d$y)
A similar approach with ggplot doesn't work; the lines stop at the last point. Example:
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + xlim(2,9)
Is there a way to get lines to run to the plot's edge in ggplot? Thanks.
try this
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + coord_cartesian(xlim = c(0,9))
if you want a straight line, abline would be easiest
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_abline() + xlim(2,9)

Plot two functions in ggplot2 with different x range limits

I have plotted linear functions with ggplot as follow:
ggplot(data.frame(x=c(0,320)), aes(x)) +
stat_function(fun=function(x)60.762126*x-549.98, geom="line", colour="black") +
stat_function(fun=function(x)-0.431181333*x+2.378735e+02, geom="line", colour="black")+
ylim(-600,600)
However, I want the 1st function to be plotted for x ranging from 0 to 12 and the 2nd function to be plotted for x ranging from 12 to max(x).
Does anyone know how to do it?
It's easiest to just calculate the data you need outside of the ggplot call first.
fun1 <- function(x) 60.762126 * x - 549.98
dat1 <- data.frame(x = c(0, 12), y = NA)
dat1$y <- fun1(dat1$x)
fun2 <- function(x) -0.431181333 * x + 2.378735e+02
dat2 <- data.frame(x = c(12, 320), y = NA)
dat2$y <- fun2(dat2$x)
ggplot(mapping = aes(x, y)) +
geom_line(data = dat1) +
geom_line(data = dat2)
Or you can join the data for the lines first (as suggested by #Heroka), resulting in an identical plot:
dat.com <- rbind(dat1, dat2)
dat.com$gr <- rep(1:2, c(nrow(dat1), nrow(dat2)))
ggplot(dat.com, aes(x, y, group = gr)) +
geom_line()

How to underline text in a plot title or label? (ggplot2)

Please pardon my ignorance if this is a simple question, but I can't seem to figure out how to underline any part of a plot title. I'm using ggplot2.
The best I could find was
annotate("segment") done by hand, and I have created a toy plot to illustrate its method.
df <- data.frame(x = 1:10, y = 1:10)
rngx <- 0.5 * range(df$x)[2] # store mid-point of plot based on x-axis value
rngy <- 0.5 * range(df$y)[2] # stores mid-point of y-axis for use in ggplot
ggplot(df, aes(x = x, y = y)) +
geom_point() +
ggtitle("Oh how I wish for ..." ) +
ggplot2::annotate("text", x = rngx, y = max(df$y) + 1, label = "underlining!", color = "red") +
# create underline:
ggplot2::annotate("segment", x = rngx-0.8, xend = rngx + 0.8, y= 10.1, yend=10.1)
uses bquote(underline() with base R
pertains to lines over and under nodes on a graph
uses plotmath and offers a workaround, but it didn't help
Try this:
ggplot(df, aes(x = x, y = y)) + geom_point() +
ggtitle(expression(paste("Oh how I wish for ", underline(underlining))))
Alternatively, as BondedDust points out in the comments, you can avoid the paste() call entirely, but watch out for the for:
ggplot(df, aes(x = x, y = y)) + geom_point() +
ggtitle(expression(Oh~how~I~wish~'for'~underline(underlining)))
Or another, even shorter approach suggested by baptiste that doesn't use expression, paste(), or the many tildes:
ggplot(df, aes(x = x, y = y)) + geom_point() +
ggtitle(~"Oh how I wish for "*underline(underlining))

Resources