ggplot a vector in R [duplicate] - r

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

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

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

Plotting a third variable against x and y axis

I am very new to R and I am trying to plot a third variable to a plot using ggplot2. I have searched for an answer and I could not find anything similar (or I didn't know the right words to search).
I have three columns of data which will be my x, y and z variable.
I want a graph that can show the values for x and y axis (as in the first and second column variables). However, I want the "points" (as a scatter plot) in the graph to be the values shown in variable z. Is there a way of doing that?
Everything that I have tried plot x against y.
Thanks for any help!
I believe this is what you are asking: Map two variables: (x,y) in their axis and display the "text" of a third variable.
Let's use this data frame - We'll try to "write" X1 and X3
df <- data.frame(X1 = 1:5, X2 = 2*1:5, X3 = rnorm(1:5))
With base graphics you can just plot one character
plot(df$X1, df$X2, pch = paste(df$X1)) plot(df$X1, df$X2, pch = paste(df$X3))
doesn't seem to work well.
Using ggplot2:
ggplot(df, aes(x = X1, y = X2)) + geom_text(label = df$X1)
ggplot(df, aes(x = X1, y = X2)) + geom_text(label = df$X3)
a fancier alternative is adding colour in the aes()
ggplot(df, aes(x = X1, y = X2, color=X3)) + geom_text(label = df$X3)
I want the "points" (as a scatter plot) in the graph to be the values shown in variable z. Is there a way of doing that?
Definitely. The bit that you need to think about is how to present the data in your z variable. By that I mean do you want the information in z to be shown by the points' colour, size or area? There are some great examples of how to do this at the R cookbook.
If you have a data frame called my.data, which has columns x, y, and z, you need to set up your plot like this:
my.plot <- ggplot(data = my.data,
aes(x = x,
y = y))
The example above says "plot the data in my.data using my.data$x to set the x location and y.data$y to set the y location". If your x variable was grid.x and y was grid.y you would have
my.plot <- ggplot(data = my.data,
aes(x = grid.x,
y = grid.y))
then you need to add your points. This time we'll assume that the information in z is going to used to set the colour of the points, which in this case is the colour aesthetic:
my.plot <- my.plot + geom_point(aes(colour = z))
print(my.plot)
And that should be that. You don't need to tell geom_point() what x and y are, because you already did that when you set up the plot.

ggplot2-line plotting with TIME series and multi-spline

This question's theme is simple but drives me crazy:
1. how to use melt()
2. how to deal with multi-lines in single one image?
Here is my raw data:
a 4.17125 41.33875 29.674375 8.551875 5.5
b 4.101875 29.49875 50.191875 13.780625 4.90375
c 3.1575 29.621875 78.411875 25.174375 7.8012
Q1:
I've learn from this post Plotting two variables as lines using ggplot2 on the same graph to know how to draw the multi-lines for multi-variables, just like this:
The following codes can get the above plot. However, the x-axis is indeed time-series.
df <- read.delim("~/Desktop/df.b", header=F)
colnames(df)<-c("sample",0,15,30,60,120)
df2<-melt(df,id="sample")
ggplot(data = df2, aes(x=variable, y= value, group = sample, colour=sample)) + geom_line() + geom_point()
I wish it could treat 0 15 30 60 120 as real number to show the time series, rather than name_characteristics. Even having tried this, I failed.
row.names(df)<-df$sample
df<-df[,-1]
df<-as.matrix(df)
df2 <- data.frame(sample = factor(rep(row.names(df),each=5)), Time = factor(rep(c(0,15,30,60,120),3)),Values = c(df[1,],df[2,],df[3,]))
ggplot(data = df2, aes(x=Time, y= Values, group = sample, colour=sample))
+ geom_line()
+ geom_point()
Loooooooooking forward to your help.
Q2:
I've learnt that the following script can add the spline() function for single one line, what about I wish to apply spline() for all the three lines in single one image?
n <-10
d <- data.frame(x =1:n, y = rnorm(n))
ggplot(d,aes(x,y))+ geom_point()+geom_line(data=data.frame(spline(d, n=n*10)))
Your variable column is a factor (you can verify by calling str(df2)). Just convert it back to numeric:
df2$variable <- as.numeric(as.character(df2$variable))
For your other question, you might want to stick with using geom_smooth or stat_smooth, something like this:
p <- ggplot(data = df2, aes(x=variable, y= value, group = sample, colour=sample)) +
geom_line() +
geom_point()
library(splines)
p + geom_smooth(aes(group = sample),method = "lm",formula = y~bs(x),se = FALSE)
which gives me something like this:

How to make multiple plots in r?

I have a large matrix mdat (1000 rows and 16 columns) contains first column as x variable and other columns as y variables. What I want to do is to make scatter plot in R having 15 figures on the same window. For example:
mdat <- matrix(c(1:50), nrow = 10, ncol=5)
In the above matrix, I have 10 rows and 5 columns. Is it possible that to use the first column as variable on x axes and other columns as variable on y axes, so that I have four different scatterplots on the same window? Keep in mind that I will not prefer par(mfrow=, because in that case I have to run each graph and then produce them on same window. What I need is a package so that I will give it just data and x, y varaibeles, and have graphs on same windows.
Is there some package available that can do this? I cannot find one.
Perhaps the simplest base R way is mfrow (or mfcol)
par(mfrow = c(2, 2)) ## the window will have 2 rows and 2 columns of plots
for (i in 2:ncol(mdat)) plot(mdat[, 1], mdat[, i])
See ?par for everything you might want to know about further adjustments.
Another good option in base R is layout (the help has some nice examples). To be fancy and pretty, you could use the ggplot2 package, but you'll need to reshape your data into a long format.
require(ggplot2)
require(reshape2)
molten <- melt(as.data.frame(mdat), id = "V1")
ggplot(molten, aes(x = V1, y = value)) +
facet_wrap(~ variable, nrow = 2) +
geom_point()
Alternatively with colors instead of facets:
ggplot(molten, aes(x = V1, y = value, color = variable)) +
geom_point()
#user4299 You can re-write shujaa's ggplot command in this form, using qplot which means 'quick plot' which is easier when starting out. Then instead of faceting, use variable to drive the color. So first command produces the same output as shujaa's answer, then the second command gives you all the lines on one plot with different colors and a legend.
qplot(data = molten, x = V1, y = value, facets = . ~ variable, geom = "point")
qplot(data = molten, x = V1, y = value, color = variable, geom = "point")
Maybe
library(lattice)
x = mdat[,1]; y = mdat[,-1]
df = data.frame(X = x, Y = as.vector(y),
Grp = factor(rep(seq_len(ncol(y)), each=length(x))))
xyplot(Y ~ X | Grp, df)

Resources