How to edit an existing histogram (or any chart)?

I am working on tutorial R. I am currently making histograms. As the tutorial progresses, it asks for changes in the histograms already built - add xlab, add the main one, etc. Etc.

This is great, except every time I have to create a histogram from scratch.

How to edit an existing, and not write out all the code again? Will there be a question of creating a histogram object and editing it every time? I have tried this. Let's say I start with this:

hearthist <- hist(outcome[,11]) 

Then I want to add xlabel. I tried this without joy:

 hearthist (xlab="30-Day Death Rate") 

What is the best practice here? Do I need to update a new histogram every time I want to add a parameter?

+4
source share
2 answers

No; basic graphics use a pen on a paper idiom; as soon as you sign, until you receive a new sheet of paper.

So use to learn the appropriate tools. In this case, the R-aware editor, in which you can write your R-code and pass it to an executable instance of R. I use ESS with Emacs, but kool children use RStudio. Let the last one go.

Even the standard, regular old R has a history mechanism that allows you to scroll back your commands and re-run them from the command line.

Or learn how to use the appropriate R. tools. title() allows you to add xlab , ylab , main and sub to an existing plot.

+5
source

You are trying to create a scene. I mean that you add additional information to the main plot. I think that the graphics package is not very flexible in terms of visual decoding of plot information.

I would use other pacakge R-graphics, such as Lattice / ggplot2, based on the mesh package, which are more suitable for such manipulations.

here is an example using ggplot2:

 set.seed(1234) df <- data.frame(cond = factor( rep(c("A","B"), each=200) ), rating = c(rnorm(200),rnorm(200, mean=.8))) ggplot(df, aes(x=rating)) + geom_histogram(binwidth=.5) 

Now I edit the xlab of the source scene:

 last_plot()+xlab("30-Day Death Rate") 
+2
source

All Articles