R: why is boxplot(x,log="y") different from boxplot(log(x))? - r

delme <- exp(rnorm(1000,1.5,0.3))
boxplot(delme,log="y")
boxplot(log10(delme))
Why are the whiskers different in this 2 plots?
Thanks
Agus

I would say that in your first plot you just changed the y axis to log, so the values you plot still range between 1 and 10. In this plot the y axis is a log scale. The whiskers on this axis look different because the space between each "tick" (ie axis break) is not constant (there is more space between 2 and 4 than between 10 and 8)
In the second plot, you take the log of the values then plot them, so they range from .2 to 1, and are plotted with a linear y axis.
Look at the summary for both of the normal and log transformed dataframes

Related

R: plotting predicted probabilities with plot(effect()) - how to customise plot appearance?

I would like to plot the predicted probabilities of Y (binary outcome) over the range of observed x values (x=age). I use the following code to produce the plot:
(1) I calculate the predicted probabilities of Y over a specified range of x-values (xlevels = x.list) for my independent variable (age), and save it in an object.
prob <- effect(c("age"),M, xlevels = x.list)
(2) Then I plot that object, customising certain plot appearances (such as axis labels, color of confidence intervals, etcetera).
plot(prob,
xlab="x",
ylab="Pred. prob.",
confint=list(col="red", alpha=0.3),
lines=list(col="red")
rug=FALSE, main="")
This produces a plot that almost looks like the one I would like to have. However, when trying to customise the main title, the y and x axis limits, as well as the ticks on the axis, the plot gets produced, but unfortunately also messed up (the y axis does not range from 0 to 1, and the actual line with confidence intervals gets pushed out of the plots' margins).
plot(prob,
xlab="x",
ylab="Pred. prob.",
confint=list(col="red", alpha=0.3),
lines=list(col="red")
rug=TRUE,
axes=list(y=lim={c(0, 1, 0.1)})))
In particular, I would like to change the y-axis so that it ...
(a) ranges from 0-1
(b) with where ticks in increments of 0.1.
(c) I further would like to rid of the box around the plot, i.e. only have the x and y axis drawn.
I have been trying to read up on ?plot.eff, but unfortunately cannot get the legacy arguments to work. Any input on how the code should be modified to get it to work would be much appreciated.
axes=list(y=lim={c(0, 1, 0.1)})) looks very strange, have you tried just ylim=c(0,1)
also do main=""

dygraphs configuring dual x axes

I have a dygraph that is plotting two sets of data. The one x-axis is 1 to 600 and the second y-axis is 1-300. How can both be plotted showing the x-axis and the 300 point plot scaled correctly. See the attached example of what I am getting. Would love to have numbers on the second y axis too.
thanksCurrent Code Example

How does R decide the x and y axis of mosaicplot

I am curious how R decides x and y axis of mosaic plot?
data(HairEyeColor)
mosaicplot(HairEyeColor)
It puts the "Hair" at x-axis and "Eye" at the y-axis and break the Sex.
Is it predetermined?
What will happen if we have more than 5 variables and do the mosaic plot?
See: Mosaic Plot vignette
xlab, ylab: x- and y-axis labels used for the plot; by default, the
first and second element of names(dimnames(X)) (i.e., the name of the
first and second variable in X).

Simple histogram plot wrong?

test <- rep(5,20)
hist(test,freq=FALSE,breaks=5)
The vector contains 20 times the value 5. When I plot this with freq=FALSE and breaks=5 I expect to see 1 bar at x=5 with height = 1.0, because the value 5 makes up 100% of the data.
Why do I instead see 1 bar that ranges from x=0 to x=5 and has height = 0.2 ??
hist plots an estimate of the probability density when freq=FALSE or prob=TRUE, so the total area of the bars in the histogram sums to 1. Since the horizontal range of the single bar that is plotted is (0,5), it follows that the height must be 0.2 (5*0.2=1)
If you really want the histogram you were expecting (i.e. heights correspond to fraction of counts, areas don't necessarily sum to 1), you can do this:
h <- hist(test,plot=FALSE)
h$counts <- h$counts/length(test)
plot(h)
Another possibility is to force the bar widths to be equal to 1.0, e.g.
hist(test,freq=FALSE,breaks=0:10)
Or maybe you want
plot(table(test)/length(test))
or
plot(table(test)/length(test),lwd=10,lend="butt")
?
See also: How do you use hist to plot relative frequencies in R?

Uneven axis in R plot

Is it possible to draw an uneven axis in R? I know that I can specify labels at specific spots, but I mean, I want a particular section of my graph to be spread out. For instance, imagine an X axis such as:
-10 -5 0 1 2 3 4 5 10
where there is equal spacing between each of the above values.
You would need to make a factor of your levels, say "fac", and then plot. Later using axis() with labels=as.character(fac).
dat <- data.frame(x=factor(c(-10 ,-5, 0, 1, 2, 3, 4, 5, 10)), y=1:9)
with(dat, plot(x, y)) # a step-like plot
with(dat, plot(as.numeric(x), y, type="p", xaxt="n")) # points instead of steps
axis(1, at=1:9, labels=as.character(dat$x)) # the "irregular" axis
Thinking further about #Ben Bolker's question, it should also be possible to define a an auxiliary x-value to determine the plotting "x=" and the axis "at=" coordinate on the horizontal and then using unique(as.character(.)) applied to the "real x" as the "labels=" argument to axis as shown above but not needing a factor construction. An even more complex scheme is possible with this approach where continuous values across specific ranges of the auxiliary variable could be used for plotting but truncated values constructed for the labels at the boundaries of those ranges. I think further design justification and specification would be needed before building an implemented example.

Resources