I have a set of paired data, x and y, that I want to plot but they are of varying lengths due to some NA values in y. How can I plot x and y only where there is data present in both variables?
x y
10 1
2 3
4 NA # not plotted
10 40
try - plot(na.pass(df)) might be useful in this case.
Related
I have data that contains information about sub-plots with different numbers and their corresponding species types (more than 3 species within each subplot). Every species have X & Y coordinates.
> df
subplot species X Y
1 1 Apiaceae 268675 4487472
2 1 Ceyperaceae 268672 4487470
3 1 Vitaceae 268669 4487469
4 2 Ceyperaceae 268665 4487466
5 2 Apiaceae 268662 4487453
6 2 Magnoliaceae 268664 4487453
7 3 Magnoliaceae 268664 4487453
8 3 Apiaceae 268664 4487456
9 3 Vitaceae 268664 4487458
with these data, I have created ppp for the points of each subplot within a window of general plot (big).
grp <- factor(data$subplot)
win <- ripras(data$X, data$Y)
p.p <- ppp(data$X, data$Y, window = window, marks = grp)
Now I want to divide a plot into equal 3 x 3 sub-plots because there are 9 subplots. The genetal plot is not rectangular looks similar to rombo shape when I plot.
I could use quadrats() funcion as below but it has divided my plot into unequal subplots. Some are quadrat, others are traingle etc which I don't want. I want all the subplots to be equal sized quadrats (divide it by lines that paralel to each sides). Can you anyone guide me for this?
divide <-quadrats(p.patt,3,3)
plot(divide)
Thank you!
Could you break up the plot canvas into 3x3, then run each plot?
> par(mfrow=c(3,3))
> # run code for plot 1
> # run code for plot 2
...
> # run code for plot 9
To return back to one plot on the canvas type
> par(mfrow=c(1,1))
This is a question about the spatstat package.
You can use the function quantess to divide the window into tiles of equal area. If you want the tile boundaries to be vertical lines, and you want 7 tiles, use
B <- quantess(Window(p.patt), "x", 7)
where p.patt is your point pattern.
Here is a plot of several different time series that I made in R:
I made these using a simple loop:
for(i in 1:ngroups){
x[paste0("Group_",i)] = apply(x[,group == i],1,mean)
}
plot(x$Group_1,type="l",ylim=c(0,300))
for(i in 2:ngroups){
lines(x[paste0("Group_",i)],col=i)
}
I also could have made this plot using matplot. Now, as you can see, each group is the mean of several other columns. What I would like to do is plot the series as in the plot above, but additionally show the range of the underlying data contributing to that mean. For example, the purple line would be bounded by a region shaded light purple. At any given time index, the purple region will extend from the lowest value in the purple group to the highest value (or, say, the 5 to 95 percentiles). Is there an elegant/clever way to do this?
Here is an answer using the graphics package (graphics that come with R). I also try to explain how it is that the polygon (which is used to generate the CI) is created. This can be repurposed to solve your problem, for which I do not have the exact data.
# Values for noise and CI size
s.e. <- 0.25 # standard error of noise
interval <- s.e.*qnorm(0.975) # standard error * 97.5% quantile
# Values for Fake Data
x <- 1:10 # x values
y <- (x-1)*0.5 + rnorm(length(x), mean=0, sd=s.e.) # generate y values
# Main Plot
ylim <- c(min(y)-interval, max(y)+interval) # account for CI when determining ylim
plot(x, y, type="l", lwd=2, ylim=ylim) # plot x and y
# Determine the x values that will go into CI
CI.x.top <- x # x values going forward
CI.x.bot <- rev(x) # x values backwards
CI.x <- c(CI.x.top, CI.x.bot) # polygons are drawn clockwise
# Determine the Y values for CI
CI.y.top <- y+interval # top of CI
CI.y.bot <- rev(y)-interval # bottom of CI, but rev Y!
CI.y <- c(CI.y.top,CI.y.bot) # forward, then backward
# Add a polygon for the CI
CI.col <- adjustcolor("blue",alpha.f=0.25) # Pick a pretty CI color
polygon(CI.x, CI.y, col=CI.col, border=NA) # draw the polygon
# Point out path of polygon
arrows(CI.x.top[1], CI.y.top[1]+0.1, CI.x.top[3], CI.y.top[3]+0.1)
arrows(CI.x.top[5], CI.y.top[5]+0.1, CI.x.top[7], CI.y.top[7]+0.1)
arrows(CI.x.bot[1], CI.y.bot[1]-0.1, CI.x.bot[3], CI.y.bot[3]-0.1)
arrows(CI.x.bot[6], CI.y.bot[6]-0.1, CI.x.bot[8], CI.y.bot[8]-0.1)
# Add legend to explain what the arrows are
legend("topleft", legend="Arrows indicate path\nfor drawing polygon", xjust=0.5, bty="n")
And here is the final result:
I have made a df using some random data.
Here's the df
df
x y
1 1 3.1667912
2 1 3.5301539
3 1 3.8497014
4 1 4.4494311
5 1 3.8306889
6 1 4.7681518
7 1 2.8516945
8 1 1.8350802
9 1 5.8163498
10 1 4.8589443
11 2 0.3419090
12 2 2.7940851
13 2 1.9688636
14 2 1.3475315
15 2 0.9316124
16 2 1.3208475
17 2 3.0367743
18 2 3.2340156
19 2 1.8188969
20 2 2.5050162
When you plot using stat_summary with mean_cl_normal and geom smooth
ggplot(df,aes(x=x,y=y))+geom_point() +
stat_summary(fun.data=mean_cl_normal, geom="smooth", colour="red")
As someone commented, maybe mean_cl_boot was better so I used it.
ggplot(df,aes(x=x,y=y))+geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="smooth", colour="red")
They are indeed a little different. Also you could play with confint parameter depending on your need.
I have a data frame(mappedUn) of the structure:
C1 C2 C3 C4 C5 C6
1 1 1 3 1 1
3 3 3 16 3 3
10 NA 10 NA 6 6
11 NA 11 NA 10 11
NA NA NA NA 11 NA
NA NA NA NA 12 NA
note :I have stripped the entries in the above example to fit it here ,also I have replaced the column names to make it simpler
I was wondering if there is a way to color code scatter plots in R, I am using the pairs method to plot different scatter plots, The method I run is :
pairs(mappedUn[1:6])
Here is what I get:
Notice some graphs have two points some have 3 and so on...Is there a way to add different background color to each of the plot in the above graph based on how many point it has ,
for instance 4 points- red, 3-yellow,2 green etc
My ultimate goal is to visually distinguish the plots with high number of common points
The key here is to customize the parameter panel inside pairs(). Try the following to see whether it meets your requirement.
n.notNA <- function(x){
# define the function that returns the number of non-NA values
return(length(x) - sum(is.na(x)))
}
myscatterplot <- function(x, y){
# ll is used for storing the parameters for plotting region
ll <- par("usr")
# bg is used for storing the color (an integer) of the background of current panel, which depends on the number of points. When x and y have different numbers of non-NA values, use the smaller one as the value of bg.
bg <- min(n.notNA(x), n.notNA(y))
# plot a rectangle framework whose dimension and background color are given by ll and bg
rect(ll[1], ll[3], ll[2], ll[4], col = bg)
# fill the rectangle with points
points(x, y)
}
# "panel = myscatterplot" means in each panel, the plot is given by "myscatterplot()" using appropriate combination of variables
pairs(data, panel = myscatterplot)
A related question : R: How to colorize the diagonal panels in a pairs() plot?
I have my data in the variable data:
data = read.csv("datafile.csv")
datafile.csv is of the form:
x1,y1,z1
x2,y2,z2
.....
xn,yn,zn
How do I create a 3D scatter plot? (the scale etc. should be automatically taken care of).
Let's simulate a data example.
#create data observations for x, y and z
x = c(10,09,03,04,05)
y = c(08,04,07,08,09)
z = c(15,10,11,09,09)
#join vectors x, y and z directly into a data.frame as suggested by #thelatemail.
data=data.frame(x,y,z)
The object data is supposed to simulate the data you have. See it below
data
x y z
1 10 8 15
2 9 4 10
3 3 7 11
4 4 8 9
5 5 9 9
The answer:
library(scatterplot3d)
scatterplot3d(data$x,data$y,data$z)
See ?scatterplot3d to explore other arguments inside this function.
I would like to plot two y values for two ranges of x values that are non-congruent. I have tried the plot(rect()) command unsuccessfully.
My inputs for x ranges and y values respectively are:
[1, 2.7] 0.05325728
(2.7, 155] 0.05179712
Any help is very much appreciated!
Putting all your data into a data.frame might be helpful here:
dat <- data.frame(x1=c(1,27),x2=c(27,155),y=c(0.05325728,0.05179712))
# x1 x2 y
#1 1 27 0.05325728
#2 27 155 0.05179712
# specify a height for the rectangles
hght <- 0.01
with(dat,plot(NA,xlim=c(min(x1),max(x2)),ylim=c(min(y),max(y)+hght),type="n"))
with(dat,rect(x1,y,x2,y+hght))