I have two variables that I want to compare in a histogram like the one below. For each bin of the histogram the frequency of both variables is shown what makes it easy to compare them.
You can use the add parameter to hist (see ?hist, ?plot.histogram):
hist(rnorm(1000, mean=0.2, sd=0.1), col='blue', xlim=c(0, 1))
hist(rnorm(1000, mean=0.8, sd=0.1), col='red', add=T)
To find out about the add parameter I noticed that in ?hist the ... argument says that these are arguments passed to plot.histogram, and add is documented in ?plot.histogram. Alternatively, one of the examples at the bottom of ?hist uses the add parameter.
you can use prop.table and barplot like this
somkes <- sample(c('Y','N'),10,replace=T)
amount <- sample (c(1,2,3),10,replace=T)
barplot(prop.table(table(somkes,amount)),beside=T)
Related
In R I often add legends to my plots like this
legend("topright",c("a=1","b=1"),lwd=c(1,2))
However, what I want to do is produce a plot which contains nothing but that legend. How do I do it? (Preferably without using package such as ggplot)
You can generate a new, empty plot frame using frame() or plot.new()
plot.new()
legend("topright",c("a=1","b=1"),lwd=c(1,2))
Use the type='n' parameter as in:
plot(x,y,type='n')
See ?plot.default for details. If you will want to add some text/points/lines to the plot afterward you may want to provide the x and y parameters, and/or the ylim and xlim parameters in order to set up the plotting region.
You can also drop the axes with the argument axes=F, and you can set the xlab,ylab, and main to NA, if you really want a blank plot.
I want to break the x-axis of a plot of a cumulative distribution function for which I use the function plot.stepfun, but don't seem to be able to figure out how.
Here's some example data:
set.seed(1)
x <- sample(seq(1,20,0.01),300,replace=TRUE)
Then I use the function ecdf to get the empirical cumulative distribution function of x:
x.cdf <- ecdf(x)
And I change the class of x.cdf to stepfun, because I prefer to call plot.stepfun directly over using plot.ecdf (which also uses plot.stepfun, but has fewer possibilities to customize the plot).
class(x.cdf) <- "stepfun"
Then I am able to create a plot as follows:
plot(x.cdf, do.point=FALSE)
But now I want to break up the x-axis between 12 and 20, e.g. using axis.break [plotrix-library] such as here, but since I have no ordinary x and y-argument for plotting, I don't know how to do this.
Any help would be very much appreciated!
"Breaking the axis between 12 and 20" doesn't make a lot of sense to me since 20 is the end of the x range, so I will exemplify breaking it between 12 and 15. The plotrix.axis.break function doesn't actually do very much (as can be seen if you step through that example.) All it does is put a couple of slashes at a particular location, the "breakpos". All the rest of the work needs to be done with regular plotting functions and plot.stepfun isn't really set up to do it, so I'm using regular plot.default with the type="s" argument. You need to do the offsetting of the x values, the arguments to the ecdf function and the labels in the axis arguments.
png()
plot( c(seq(1,12,0.1), seq(15,20,0.1)-3), # Supply the range, shifted
x.cdf(c(seq(1,12,0.1), seq(15,20,0.1))), # calc domain values, not shifted
type="s", xaxt="n", xlab="X", ylab="Quantile")
axis(1, at=c( 1:12, (16:20)-3), labels=c(1:12, (16:20)) ) #shift x's, labels unshifted
axis.break(breakpos=12)
dev.off()
I am trying to plot a growth spline for the variable "ipen" against "year" in this data. The code I am using is:
grofit <- read.csv('http://dl.dropbox.com/u/1791181/grofit.csv')
# install.packages(c("grofit"), dependencies = TRUE)
require(grofit)
growth = gcFitSpline(grofit$year, grofit$ipen)
plot(growth)
This works fine, and produces the plot below. But the problem is that (a) I can't change the default labels with xlab or ylab options, and (b) I can't change the scale of x-axis to be (0,100) with ylim=c(0,100) in the plot() statement. I could not find any pointers in the grofit()documentation. .
Don't directly use plot on the gcFitSpline result, but rather use str(growth) to explore the structure of the curve fit object and determine what you want to plot, or look at plot.gcFitSpline code (just type the name of the function without parentheses in the console and press Enter!)
For instance:
growth = gcFitSpline(grofit$year, grofit$ipen)
plot (grofit$year, grofit$ipen, pch=20, cex=0.8, col="gray", ylim=c(0, 100),
ylab="Growth", xlab="Year", las=1)
points(growth$fit.time, growth$fit.data, t="l", lwd=2)
This gives:
Actually, I think the grofit documentation does have a pointer about this:
... Other graphical parameters may also passed as arguments [sic]. This has currently no effect and is only meant to fulfill the requirements of a generic function.
You may have to rewrite the plot.gcFitSpline function specific to the graphical parameters you want, but there might be a reason that the function is the way it is.
BTW, I'm not sure it's a great idea to give your data the same name as the package you're using.
I am trying to make a simple barplot but i have a problem that I have duplicated names on x-axis. So when ever I am trying to write names on x-axis it does not show complete string. I have following data
x <- c(1.8405917,0.3265986,1.5723623,464.7370299,0.0000000,3.2235716,
3.1223534, 7.0999787, 1.7122258,3.2005524,3.7531266,469.4436828)
and I am using barplot
barplot(x,xlab=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC","CC/AC",
"CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"))
But it does not work. I also used
axis()
But it does not work as well.
Thanks in advance.
No, xlab is for providing a label for the entire x-axis of the plot, not for labelling the individual bars.
barplot() takes the labels for the bars from the names of the vector plotted (or something that can be derived into a set of names).
> names(x) <- c("AA/AA", "AA/CC", "AA/AC", "AA/NC", "CC/AA", "CC/CC", "CC/AC",
+ "CC/NC", "AC/AA", "AC/CC", "AC/AC", "AC/NC")
> barplot(x)
> ## or with labels rotated, see ?par
> barplot(x, las = 2)
Edit: As #Aaron mentions, barplot() also has a names.arg to supply the labels for the bars. This is what ?barplot has to say:
names.arg: a vector of names to be plotted below each bar or group of
bars. If this argument is omitted, then the names are taken
from the names attribute of height if this is a vector,
or the column names if it is a matrix.
Which explains the default behaviour if names.arg is not supplied - which is to take the names from the object plotted. Which usage is most useful for you will mainly be a matter of taste. Not having the row/column/names might speed code up slightly, but many of R's functions will take the names attribute (or similar, e.g. row names) directly from objects so you don't have to keep providing labels for plotting/labelling of results etc.
xlab should be names.arg. See ?barplot for details.
The way to use axis() is to capture the midpoints, which is what the barplot function returns. See ?barplot:
mids <- barplot(x, xlab="")
axis(1, at=mids, labels=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC",
"CC/AC","CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"),
las=3)
Try this:
barplot(x, cex.names=0.7,
names.arg=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC","CC/AC",
"CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"))
I have a plot() that I'm trying to make, but I do not want the x-values to be used as the axis labels...I want a different character vector that I want to use as labels, in the standard way: Use as many as will fit, drop the others, etc. What should I pass to plot() to make this happen?
For example, consider
d <- data.frame(x=1:5,y=10:15,x.names=c('a','b','c','d','e'))
In barplot, I would pass barplot(height=d$y,names.arg=d$x.names), but in this case the actual x-values are important. So I would like an analog such as plot(x=d$x,y=d$y,type='l',names.arg=d$x.names), but that does not work.
I think you want to first suppress the labels on the x axis with the xaxt="n" option:
plot(flow~factor(month),xlab="Month",ylab="Total Flow per Month",ylim=c(0,55000), xaxt="n")
then use the axis command to add in your own labels. This example assumes the labels are in an object called month.name
axis(1, at=1:12, labels=month.name)
I had to look up how to do this and I stole the example from here.