I would like to ask a help in modify axes in control chart in RStudio, by qcc package.
Generated a controlchart type EWMA, in the axes x, appear group, sequencially (that represents each observations), but I need insert an information about year (divide group into sequence of year). I tried using the command (axes), but doesn't work.
Which command or function it is possible to modify to achieve this?
Thank you very for help!
Yours faithfully
Guilherme
The ewma function inqcc accepts a labels parameter: a character vector of labels for each group.
You could, for instance, do something like:
library(qcc)
data(pistonrings)
attach(pistonrings)
diameter <- qcc.groups(diameter, sample)
label_x <- as.character(seq.Date(from = as.Date("2020/01/01"), length.out = 25, by = "day"))
q2 <-
ewma(
diameter[1:25, ],
center = 0,
lambda = 0.4,
std.dev = 0.57,
nsigmas = 3,
add.stats = FALSE,
labels = label_x
)
And end up with a properly labelled x-axis in your EWMA plot:
Related
I am working on a script where I need to calculate the coordinates for a beeswarm plot without immediately plotting. When I use beeswarm, I get x-coordinates that aren't swarmed, and more or less the same value:
But if I generate the same plot again it swarms correctly:
And if I use dev.off() I again get no swarming:
The code I used:
n <- 250
df = data.frame(x = floor(runif(n, 0, 5)),
y = rnorm(n = n, mean = 500, sd = 100))
#Plot 1:
A = with(df, beeswarm(y ~ x, do.plot = F))
plot(x = A$x, y=A$y)
#Plot 2:
A = with(df, beeswarm(y ~ x, do.plot = F))
plot(x = A$x, y=A$y)
dev.off()
#Plot 3:
A = with(df, beeswarm(y ~ x, do.plot = F))
plot(x = A$x, y=A$y)
It seems to me like beeswarm uses something like the current plot parameters (or however it is called) to do the swarming and therefore chokes when a plot isn't showing. I have tried to play around with beeswarm parameters such as spacing, breaks, corral, corralWidth, priority, and xlim, but it does not make a difference. FYI: If do.plot is set to TRUE the x-coordinates are calculated correctly, but this is not helpful as I don't want to plot immediately.
Any tips or comments are greatly appreciated!
You're right; beeswarm uses the current plot parameters to calculate the amount of space to leave between points. It seems that setting "do.plot=FALSE" does not do what one would expect, and I'm not sure why I included this parameter.
If you want to control the parameters manually, you could use the functions swarmx or swarmy instead. These functions must be applied to each group separately, e.g.
dfsplitswarmed <- by(df, df$x, function(aa) swarmx(aa$x, aa$y, xsize = 0.075, ysize = 7.5, cex = 1, log = ""))
dfswarmed <- do.call(rbind, dfsplitswarmed)
plot(dfswarmed)
In this case, I set the xsize and ysize values based on what the function would default to for this particular data set. If you can find a set of xsize/ysize values that work for your data, this approach might work for you.
Otherwise, perhaps a simpler approach would be to leave do.plot=TRUE, and then discard the plots.
I am needing to produce normally distributed density plots with different total areas (summing to 1). Using the following function, I can specify the lambda - which gives the relative area:
sdnorm <- function(x, mean=0, sd=1, lambda=1){lambda*dnorm(x, mean=mean, sd=sd)}
I then want to plot up the function using different parameters. Using ggplot2, this code works:
require(ggplot2)
qplot(x, geom="blank") + stat_function(fun=sdnorm,args=list(mean=8,sd=2,lambda=0.7)) +
stat_function(fun=sdnorm,args=list(mean=18,sd=4,lambda=0.30))
but I really want to do this in base R graphics, for which I think I need to use the "curve" function. However, I am struggling to get this to work.
If you take a look at the help file for ? curve, you'll see that the first argument can be a number of different things:
The name of a function, or a call or an expression written as a function of x which will evaluate to an object of the same length as x.
This means you can specify the first argument as either a function name or an expression, so you could just do:
curve(sdnorm)
to get a plot of the function with its default arguments. Otherwise, to recreate your ggplot2 representation you would want to do:
curve(sdnorm(x, mean=8,sd=2,lambda=0.7), from = 0, to = 30)
curve(sdnorm(x, mean=18,sd=4,lambda=0.30), add = TRUE)
The result:
You can do the following in base R
x <- seq(0, 50, 1)
plot(x, sdnorm(x, mean = 8, sd = 2, lambda = 0.7), type = 'l', ylab = 'y')
lines(x, sdnorm(x, mean = 18, sd = 4, lambda = 0.30))
EDIT I added ylab = 'y' and updated the picture to have the y-axis re-labeled.
This should get you started.
Could you explain me if there is a way to extract outliers from box plot. I have plotted a box plot and I want to extract only the outliers.
Here is the code for the box plot.
# melting down
require(reshape)
melt_nx <- melt(nx, id.vars = c("x", "y"))
boxplot(data = melt_nx, main = "NX", value ~ variable, las = 2,
par(mar = c(15, 5, 4, 2) + 0.1),
names = c("We1", "We2", "we3"))
Is it possible from the box plot to extract the outliers only?
The boxplot function returns a list with one of it node-names as "out". These are the values that are beyond the "whiskers". I don't know about executing par within the argument list but if you want these particular values, then use this:
vals <- boxplot(data = melt_nx, main = "NX", value ~ variable, las = 2,
names = c("We1", "We2", "we3"))
vals$out
And do read all these help pages:
?boxplot
?boxplot.stats
?bxp
?fivenum
I know this has been answered, but for me there is an alternative method using the Boxplot method from the car package. Note the capital B in the Boxplot function call.
This is the code that does it for me, it returns the row numbers of the outliers which you can then use in your dataframe to filter out or extract, etc...
outliers<-Boxplot(x~y, data=df, id.method="y")
Note that the extracted values are of type Character. Then to exclude them you could do something like:
df2 <- df[-as.numeric(outliers),]
Hope this helps a little
I have 20 subjects and each subject has 2 durations with different (staggered) start time point and end time point. I would like to make a line chart in R which would look like the lift table in suruvival analysis with staggered entry. To be specific, say the Y-axis is the ID and X-axis is the duration, the plot would be ideally to show the 2 durations stacked for the same ID.
Any help would be greatly appreciated.
You can make a blank plot and draw the lines yourself. Here's my attempt at an example based on your description.
set.seed(500)
df <- data.frame(
id = 1:20,
time_start1 = sample(1:25, 20),
time_end1 = sample(51:75, 20),
time_start2 = sample(26:50, 20),
time_end2 = sample(76:100, 20)
)
plot(NULL, NULL, xlim=c(0,100), ylim=c(1,20),
xlab="Time", ylab="Identifier")
segments(
x0=df$time_start1,
y0=df$id,
x1=df$time_end1,
y1=df$id
)
segments(
x0=df$time_start2,
y0=df$id - 0.25,
x1=df$time_end2,
y1=df$id - 0.25,
lty=2
)
I have a large data set which I would like to make a 3D surface from. I would like the x-axis to be the date, the y-axis to be the time (24h) and the z-axis (height) to be a value I have ($). I am a beginner with R, so the simpler the better!
http://www.quantmod.com/examples/chartSeries3d/ has a nice example, but the code is way to complicated for my skill level!
Any help would be much appreciated - anything I have researched so far needs to have the data sorted, which is not suitable I think.
Several options present themselves, persp() and wireframe(), the latter in package lattice.
First some dummy data:
set.seed(3)
dat <- data.frame(Dates = rep(seq(Sys.Date(), Sys.Date() + 9, by = 1),
each = 24),
Times = rep(0:23, times = 10),
Value = rep(c(0:12,11:1), times = 10) + rnorm(240))
persp() needs the data as the x and y grid locations and a matrix z of observations.
new.dates <- with(dat, sort(unique(Dates)))
new.times <- with(dat, sort(unique(Times)))
new.values <- with(dat, matrix(Value, nrow = 10, ncol = 24, byrow = TRUE))
and can be plotted using:
persp(new.dates, new.times, new.values, ticktype = "detailed", r = 10,
theta = 35, scale = FALSE)
The facets can be coloured using the col argument. You could do a lot worse than study the code for chartSeries3d0() at the page you linked to. Most of the code is just drawing proper axes as neither persp() nor wireframe() handle Date objects easily.
As for wireframe(), we
require(lattice)
wireframe(Value ~ as.numeric(Dates) + Times, data = dat, drape = TRUE)
You'll need to do a bit or work to sort out the axis labelling as wireframe() doesn't work with objects of class "Date" at the moment (hence the cast as numeric).