I'm using a data frame in a long format to create a panelled density plot in lattice. Now I'd like to add a vertical line at the median x-value within each panel. I found a suggestion for doing it in an dotplot ( http://r.789695.n4.nabble.com/how-to-add-a-vertical-line-for-each-panel-in-a-lattice-dotplot-with-log-scale-td4632513.html ), but this doesn't work for me. Here is my code:
data(Chem97, package="mlmRev")
densityplot(~gcsescore | factor(score), data=Chem97,
panel=function(...){
panel.densityplot(...)
median.values <- median(x)
panel.abline(v=median.values, col.line="red")
})
The error is: Object x not found. So I tried the following:
panel=function(x,...){
panel.densityplot(...)
}
The moment I add x as an argument to the panel function, I get the error Error using packet 1 (2, 3 etc.). x is missing.
What's going wrong?
I've finally found the solution:
densityplot(~gcsescore | factor(score), data=Chem97,
panel=function(x,...){
panel.densityplot(x,...)
panel.abline(v=quantile(x,.5), col.line="red")
})
Related
I have data like this :
df <- data.frame(X=rnorm(10,0,1), Y=rnorm(10,0,1), Z=rnorm(10,0,1))
I need to plot each variables against each other, so I used
plot(df)
It plotted each variable within the df against the each other exactly what is required.
But I want to add 45 degree line(where x=y), in each and every sub plot. I want to know how it can be done ? I also tried through loop but due to "space constraint" it could not happen[in reality i have 5 variables within the df]. Please help.
Thanks
plot(df) calls pairs to plot data.frames. So, using this answer, we can try:
my_line <- function(x,y,...){
points(x,y,...)
segments(min(x), min(y), max(x), max(y),...)
}
pairs(df, lower.panel = my_line, upper.panel = my_line)
I want to plot a histogram with density line. The code below does not work. Any idea why?
control <- c(4.17,3.05,5.18,4.01,6.11,4.1,5.17,3.57,5.33,5.59,
4.66,5.58,3.66,4.50,3.90,4.61,5.62,4.53,6.05,5.14)
hist(control,col="gray",xlab="Utility",prob=TRUE,
main="Histogram of individual utilities")
curve(dnorm(control,mean=mean(control),
sd=sd(control)), add=TRUE,col="red")
What was your error message? You should have told us. I get:
Error in curve(dnorm(control, mean = mean(control), sd = sd(control)), :
'expr' must be a function, or a call or an expression containing 'x'
So make it an expression containing x:
curve(dnorm(x,mean=mean(control),sd=sd(control)), add=TRUE,col="red")
According to the previous questions answered I thought the following script should work.
require(lattice)
histogram(cyl~mpg|gear*am,
data=mtcars,
nint=5,
panel=function(y,...){
panel.histogram(...)
m<-mean(y)
panel.txt(x=30,y=60,labels=m)
}
)
The histograms plot but I'm getting the "packet 1 argument "y" is missing, with no default"
Help appreciated as I've wasted an hour of my life on this puzzle..
panel.histogram needs an x argument (not a y argument) which is a clue that this is what histogram passes to the panel.
Also, you need to pass the x argument through to panel.histogram by including it in the argument list.
Finally, it's panel.text rather than panel.txt.
histogram(cyl~mpg|gear*am,
data=mtcars,
nint=5,
panel=function(x, ...){
panel.histogram(x=x,...)
m<-mean(x)
panel.text(x=30,y=60,labels=m)
}
)
I have these two df.
x;
experiment expression
1 HC 50
2 LC 4
3 HR 10
4 LR 2
y;
HC_conf_lo HC_conf_hi LC_conf_lo LC_conf_hi HR_conf_lo HR_conf_hi LR_conf_lo LR_conf_hi
1 63.3293 109.925 2.33971 5.26642 8.8504 16.7707 0.124013 0.434046
I want to use df:y to plot low and high conf. points. Output should be a barplot with errorbars. Can someone show me using lines in the basic package how to do this?
So don't know if your data is valid. Assuming the confidence intervals are valid.
Here's what you can do to get error bars in your data
#First reading in your data
x<-read.table("x.txt", header=T)
y<=read.table("y.txt", header =T)
#reshaping y to merge it with x
y.wide <-data.frame(matrix(t(y),ncol=2,byrow=T)) #Transpose Y,
#matrix with 2 cols, byrow,
#so we get the lo and hi values in one row
names(y.wide)<-c("lo","hi") #name the columns in y.wide
#Make a data.frame of x and y.wide
xy.df <-data.frame(x,y.wide) # this will be used for plotting the error bars
#make a matrix for using with barplot (barplot takes only matrix or table)
xy<-as.matrix(cbind(expression=x$expression,y.wide))
rownames(xy)<-x$experiment #rownames, so barplot can label the bars
#Get ylimts for barplot
ylimits <-range(range(xy$expression), range(xy$lo), range(xy$hi))
barx <-barplot(xy[,1],ylim=c(0,ylimits[2])) #get the x co-ords of the bars
barplot(xy[,1],ylim=c(0,ylimits[2]),main = "barplot of Expression with ? bars")
# ? as don't know if it's C.I, or what
with(xy.df, arrows(barx,expression,barx,lo,angle=90, code=1,length=0.1))
with(xy.df, arrows(barx,expression,barx,hi,angle=90, code=1,length=0.1))
Resultant Plot
But it doesn't look right, This is because your expression values don't fall between the lo and hi values.
With the hack below,
barplot(xy[,1],ylim=c(0,ylimits[2]),main = "barplot of Expression with ? bars")
with(xy.df, arrows(barx,lo,barx,hi,angle=90, code=2,length=0.1))
with(xy.df, arrows(barx,hi,barx,lo,angle=90, code=2,length=0.1))
The resultant plot
So look at the both arrows call carefully, and you will see how I achieved it.
I would recommend double checking your calculations though.
And this is far easier with ggplot2. Look at this page for examples and code
http://docs.ggplot2.org/0.9.3.1/geom_errorbar.html
In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).
For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.
After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?
I appreciate any help!
histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,
type='density',
nint=100,
breaks=NULL,
layout=c(10,3),
scales= list(y=list(relation="free"),
x=list(relation="free")),
panel=function(x,...) {
mean.values <-mean(x)
sample.n <- length(x)
panel.text(lab=paste("Sample size = ",sample.n))
panel.text(lab=paste("Mean = ",mean.values))
panel.histogram(x,col="pink", ...)
panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:
https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html