I've been having a problem today, I want to remove the x-axis from the following R-plot, but it just won't disappear. I want the axis to be on top.
Is anybody able to help me?
library(psych)
temp <- describe(attitude)
error.bars(stats=temp,xaxt="n")
axis(3)
You can use fixInNamespace() to edit the error.bars() function in the psych NAMESPACE. Try:
fixInNamespace(error.bars)
That will open the function in a rudimentary text editing window. Find the axis() calls and comment out the ones you don't want. Exit the editor and R will update the function in the NAMESPACE.
Then try using the function again.
Alternatively, you can print the code for error.bars() to the prompt, copy it into a text editor, change the name of the function, say to my.error.bars, and comment out the axis() calls as before. Save the function in a file and source() it into your session or copy and paste the function in. Then use to your heart's desire.
A third alternative, is to work out how error.bars() does it's base plotting - look at the code. Recreate that plot yourself, without axes, then call error.bars() with add = TRUE.
As in the comment, you can edit the source code. Easiest way is probably to use 'fix':
eb = fix(error.bars)
should pop up an editor. Change the axis(1,.etc.) calls to axis(3,.etc.). Then you have a new function called eb() that works like error.bars.
You might want to tweak some other things too, like the title which stomps on the axes when placed at the top.
Just to show what Gavin means with add=T :
group <- factor(rep(1:10,10))
y <- (1:10)[group] + rnorm(100)
grmean <- tapply(y,group,mean)
plot(1:10,grmean,xaxt="n",type="n")
unstacked <- unstack(data.frame(y,group),y~group)
error.bars(unstacked,add=T)
axis(3)
gives :
Related
I would like to change some graphical features of a panelView plot. In the help menu, I did not find a corresponding option.
I'm not so much into package programming. But I guess they build up on other packages. Perhaps panelView uses ggplot2 or something similar which could be accessed with a workaround? Can I somehow adjust aesthetics?
Here is the MWE from the package. In this case, lines are too thin.
library(panelView)
data(panelView)
panelView(turnout ~ 1 + policy_mail_in + policy_motor, type="outcome",
data = turnout, index = c("abb","year"))
If you look at the source code for panelView, you can see that it hard codes the line width at 0.5. The panelView function does nothing too fancy (like require internals), so you could modify the function so that there is a line width argument. A hacky solution but there is it.
I have done so, but the function is >1300 lines of code, so I'm not doing to repost it here. Rather, you can copy from here.
Now, using the panelView_B function, with the new argument, line.width, we can modify the lines:
library(panelView)
data(panelView)
panelView_B(turnout ~ 1 + policy_mail_in + policy_motor, type="outcome",
data = turnout, index = c("abb","year"), line.width = 1)
I want to change a condition within the function psych::polychoric in R.
Specifically, I want to increase the limit of different realizations of a a variable from 8 to 10 on line 77 of the code.
I can manually increase the limit by calling
trace(polychoric, edit=TRUE)
Since the script is meant for reproduction purposes for a paper of mine, I want to make handling as smooth as possible by avoiding manual editing.
Is there a way to edit the function by a piece code,
e.g. by replacing if (nvalues > 8) by if (nvalues > 10) in the code by another function?
Any suggestions would be much appreciated.
find the location in the function that you want to change
as.list(body(psych::polychoric))
Change the function
trace(psych::polychoric, quote(nvalues > 10), at=11)
Check to see that you changed what you want to change
trace(psych::polychoric, edit=TRUE)
Set the function back to original
untrace(psych::polychoric)
-----
Seems like fix may be easier for you to implement for this task
fix(polychoric)
opens a pane that you can change the code in - change and hit save.
This will make the function local to your global environment you can check this by looking at the original function trace(polychoric, edit = T) will show nvalues > 10, and trace(psych::polychoric, edit = T) will show nvalues > 8. The next time you reload psych you will be using the original function. Bit of a manual hack - but hopefully works for this one off situation.
In the following example, I want to write the residuals plot of each model in a file. I do not need to see them in my display.
for (i in 1:500){
temp.model<-lme(as.formula(paste("Var",i) ~ X1*X2, sep=""), data = example, random=~1| Exp/Person)
jpeg(paste("C:/Myfolder", i, ".jpg", sep = ""), quality=50, bg="white")
plot(temp.model)
dev.off ()
graphics.off()
}
When I run this code without loop, I obtain what I want. However, it creates blank files within the loop.
Any ideas?
Thank you.
The answer is in the FAQ, FAQ 7.22 in fact. However this is not obvious until you realize that the plot.lme function from the nlme package uses lattice/trellis graphics to do the actual plotting (there are references on the help page for plot.lme, but not obvious).
The short form of the solution (but I still recommend reading the FAQ and the other documentation to fully understand the issue) is to wrap the plot in a print command.
This is my code which is part of a larger script.
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
png(paste(survPath,"/surv_",d1,".png",sep=""))
plot(survfit1,xlab="Years",ylab="Survival probability",xmax=xmax1)
}
I don't have a good idea of what this code does yet, so I'm trying to look at each individual plot to see what it is. The problem is, whenever I run this in the R command line in the terminal in linux, nothing appears. I have to use dev.off() multiple times and then rerun this code:
plot(survfit1)
for something to appear. How can I see all the plots?
Sounds like this is really what you want:
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
x11() ## open up new graphical window for each plot (to avoid overwriting)
plot(survfit1,xlab="Years",ylab="Survival probability",
xmax=xmax1, main = d1) ## use different titles to distinguish those plots
}
This will produce plots on normal graphical windows.
If you want to use the original code, you'd better do this way:
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
png(paste(survPath,"/surv_",d1,".png",sep=""))
plot(survfit1,xlab="Years",ylab="Survival probability",xmax=xmax1)
dev.off()
}
Then, have a look at the directory given by getwd(). All the plots are saved in png files.
Calling Sys.sleep(.1) might help during the for loop. Maybe try:
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
Sys.sleep(.1)
png(paste(survPath,"/surv_",d1,".png",sep="", collapse="))
plot(survfit1,xlab="Years",ylab="Survival probability",xmax=xmax1)
dev.off()
}
I'm trying to save a ggplot within a function using graphics devices. But I found the code produces empty graphs. Below is a very very simple example.
library(ggplot2)
ff <- function(){
jpeg("a.jpg")
qplot(1:20, 1:20)
dev.off()
}
ff()
If I only run the content of the function, everything is fine. I know that using ggsave() will do the thing that I want, but I am just wondering why jpeg() plus dev.off() doesn't work. I tried this with different versions of R, and the problem persists.
You should use ggsave instead of the jpeg(); print(p); dev.off() sequence. ggsave is a wrapper that does exactly what you intend to do with your function, except that it offers more options and versatility. You can specify the type of output explicitly, e.g. jpg or pdf, or it will guess from your filename extension.
So your code might become something like:
p <- qplot(1:20, 1:20)
ggsave(filename="a.jpg", plot=p)
See ?ggsave for more details
The reason why the original behaviour in your code doesn't worked is indeed a frequently asked question (on stackoverlflow as well as the R FAQs on CRAN). You need to insert a print statement to print the plot. In the interactive console, the print is silently execututed in the background.
These plots have to be printed:
ff <- function(){
jpeg("a.jpg")
p <- qplot(1:20, 1:20)
print(p)
dev.off()
}
ff()
This is a very common mistake.