This question already has answers here:
3D equivalent of the curve function in R?
(4 answers)
Closed 8 years ago.
If x and y can vary from 0 to 10, how can I plot a maths function z = x*(y^2) as an image plot or heatmap ? The x and y should come to their respective axes and z value should be shown as colour on the plot. I could find method to plot only discrete values, not a continuous function. Thanks for your help.
For example
library(emdbook)
curve3d(x*y^2,xlim=c(0,10),ylim=c(0,10),sys3d="image")
curve3d is just a wrapper (the sys3d argument gives you a range of plotting possibilities) -- more generally, you can create a function and use outer() (if it's vectorized) to create a matrix, e.g.
xvec <- yvec <- seq(0,10,length.out=41)
z <- outer(xvec,yvec,function(x,y) x*y^2)
image(xvec,yvec,z)
Related
This question already has answers here:
Why does the hist() function not have area one
(4 answers)
Closed 1 year ago.
I am trying to plot an histogram using R.
I decided to use the function hist() but I cannot understand why by changing the "breaks" option the sum of the density also changes.
In fact if I write
h <- hist(data, freq =F, breaks = "FD")
and then run
sum(h$density)
the result is 2 (same thing for breaks = "Scott"). While if I use
h <- hist(data, freq =F)
the result is 1 (as expected).
Summing the density values only makes sense if your bins are one unit wide. You want to sum the areas of the bars, which is the density value times the bin width. Presumably your FD bins are half the width of the default bins.
This question already has an answer here:
Ordering of points in R lines plot
(1 answer)
Closed 3 years ago.
I used plot(x, y, type="p") to draw a scatter plot, and it seems right (Figure 1). However, when using plot(x, y, type="l") to draw a line, there are some mussy lines (Figure 2). Why didn't it a "single" line?
Looks like your x vector needs to be sorted, when using line plots, the order in which your points are submitted is very important as the lines are drawn connecting one point to the next one.
y <- y[order(x)]
x <- x[order(x)]
# now you can make your plot
plot(x, y, type="l")
This question already has an answer here:
Messy plot when plotting predictions of a polynomial regression using lm() in R
(1 answer)
Closed 5 years ago.
Here's how I plotted a quadratic curve:
factor <- 1:7
data <- c(0.1375000,0.2500000,0.3416667,0.4583333,0.7250000,0.9166667,1.0000000)
plot(factor, fitted(lm(data~factor+I(factor^2))), type="l")
I try to do the same with my another data.
factor1<-c(2833,2500,2437,2124,1382,3736,2100,1844,2740,957,1614,1100,1550,3858,2430,2139,1812,1757,1847,945)
data1<-c(0.95,0.88,0.88,0.93,0.81,0.67,0.55,0.53,0.52,0.90,0.87,0.20,0.28,-0.16,0.23,0.11,0.26,0.08,0.73,0.76)
plot(factor1,fitted(lm(data1~factor1+I(factor1^2))), type="l")
I think this is because the second dataset is not sorted but I thought R automatically sorts them before plotting them.
Could anyone tell me how to plot a quadratic line in the second plot.
You can order it first
i<-order(factor1)
plot(factor1[i],fitted(lm(data1[i]~factor1[i]+I(factor1[i]^2))), type="l")
This question already has answers here:
Exponential regression in R
(2 answers)
Closed 6 years ago.
I am looking to add a curve to my plot to show an exponential decrease over time. Ive plotted two small sets of data with the basic plot() function and just for clarity I wanted to add a smoothed line.
The data points for the two datasets are
1.00000 0.37360 0.27688 0.22992 0.17512 0.13768 0.08048
1.00000000 0.44283122 0.30871143 0.23647913 0.22586207 0.09800363 0.06206897
with the x values showing the decay over time (0,1,2,3,4,5,6)
I like to use ggplot2 as it makes adding lines from fitted models so simple.
Without to much to go on the following may help you out....
#prepare the data
y <- c(1.00000, 0.37360, 0.27688, 0.22992, 0.17512, 0.13768, 0.08048,
1.00000000, 0.44283122, 0.30871143, 0.23647913, 0.22586207, 0.09800363, 0.06206897)
x <- c(0,1,2,3,4,5,6,0,1,2,3,4,5,6)
z <- c(1,1,1,1,1,1,1,0,0,0,0,0,0,0)
dat <- as.data.frame(cbind(x,y,z))
#load the library
library(ggplot2)
#plot the data
ggplot(data=dat,aes(x=x,y=y))+
#add Points with different shapes depending on factor z
geom_point(aes(shape=factor(z)))+
#Add line using non-linear regreassion
stat_smooth(method="nls",formula = y~a*exp(-x*b),method.args=list(start=c(a=2,b=2)),se=F,color="red")+
#add line using linear regression
stat_smooth(method="lm",formula = y~exp(-x),se=F,color="blue")
This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.