Broken y-axis histogram in R? (again) - r

I've been working on putting together a broken y-axis histogram, in order to display one very tall bar while not having the other bars invisible. In particular, I would like to be able to control not only what the tics are on the x-axis (they need to show the count bins from the histogram, not just the axes), but also how many tics there are on the x-axis.
This question got me started, though I had to figure out on my own how to use the xtics option in plotrix's gap.barplot to show the histogram bins:
Put a break in the Y-Axis of a histogram
However, I still cannot figure out a way to plot a broken y-axis histogram without having it try to plot a tic for every single bin. Does anybody know a way to do this?

Related

R stack multiple boxplot on top of each other

I am trying to make some boxplots. Here is a sample data
set.seed(1)
a<-rnorm(100)
a1<-rnorm(100);a2<-rnorm(100);a3<-rnorm(100);a4<-rnorm(100)
b1<-rnorm(100);b2<-rnorm(100);b3<-rnorm(100);b4<-rnorm(100)
c1<-rnorm(100);c2<-rnorm(100);c3<-rnorm(100);c4<-rnorm(100)
d1<-rnorm(100);d2<-rnorm(100);d3<-rnorm(100);d4<-rnorm(100)
e1<-rnorm(100);e2<-rnorm(100);e3<-rnorm(100);e4<-rnorm(100)
f1<-rnorm(100);f2<-rnorm(100);f3<-rnorm(100);f4<-rnorm(100)
dat<-data.frame(a,a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4,e1,e2,e3,e4,f1,f2,f3,f4)
par(mfrow=c(4,1))
boxplot(dat$a,dat$a1,dat$b1,dat$c1,dat$d1,dat$e1,dat$f1)
boxplot(dat$a,dat$a2,dat$b2,dat$c2,dat$d2,dat$e2,dat$f2)
boxplot(dat$a,dat$a3,dat$b3,dat$c3,dat$d3,dat$e3,dat$f3)
boxplot(dat$a,dat$a4,dat$b4,dat$c4,dat$d4,dat$e4,dat$f4)
And this is the resultant plot
As you can see, the four boxplots lie on top of each other. Is there any way I can combine these plots on top of each other so that there is no spaces between them as well as make the size of boxplot small (i.e. the boxes inside the plots)
I thought doing a par(mfrow=c(4,1)) should do the trick but it is leaving a lot of spaces between the plots. Ideally, I would want a single x-axis and single y-axis (further split into four axis to show the values of each of the plots)
Thanks
You can use par(mar=c(0,0,0,0)) to get rid of the entire figure margin. Adjusting the four mar values will change the margins (see ?par).
As for changing the size of the boxplots, you can adjust the boxwex argument in the boxplot function (see ?boxplot). Here is code that changes both mar and boxwex.
par(mfrow=c(4,1), mar=c(2,3,0,1))
boxplot(dat$a,dat$a1,dat$b1,dat$c1,dat$d1,dat$e1,dat$f1, boxwex=0.25)
boxplot(dat$a,dat$a2,dat$b2,dat$c2,dat$d2,dat$e2,dat$f2, boxwex=0.5)
boxplot(dat$a,dat$a3,dat$b3,dat$c3,dat$d3,dat$e3,dat$f3, boxwex=0.75)
boxplot(dat$a,dat$a4,dat$b4,dat$c4,dat$d4,dat$e4,dat$f4, boxwex=1,
names=1:7)
You can set the first element of mar to 0 if you want to completely get rid of the space between the plots, but that doesn't seem like it would look particularly nice, and that makes it trickier to get the x-axis in the bottom figure without changing its size relative to the first three plots.
Another alternative you could try is to put all the boxplots into one plot, but have side-by-side boxplots for each category (1-7). You can use the at argument in the boxplot function to specify the position of each boxplot along the x-axis.

Plot same plot again (or choose which plot to draw)

I have a complex layout of plots, say for example
layout(matrix(1:2,nrow=1), widths=c(1,7))
I have drawn earlier plots, and am now working on a particular plot (e.g. working on the code on the plot). I redraw the plot often to see impacts of changing e.g. margins.
How can I avoid that each time I redraw the plot it moves on to the next plot in the layout?
e.g. each time I have to start from the beginning, call dev.off, call layout, plot the earlier plots, and then start again with my plot.
Can I just select which plot in the layout I want to plot? Can I move the "counter" that selects which plot is being plotted next?

ggplot error bar not showing on graph because it falls outside y axis limits [duplicate]

If I make a line plot (a time series, for example) and set my axis limits, I want the line to continue off the plot when points are outside of the axis limits, and then come back into the plot for the next point that is within the axis limits. Right now, it seems that ggplot2 will just drop the points completely and give me a an "Error:" message.
If you limit your axes by reducing the axis scale (scale_x_continuous(limits=...)), then that is the expected behavior. By adjusting the scale, you are defining what data should be part of the plot. If you want to use all the data, but just zoom in on a particular region of the axes, you want to use coord_cartesian(xlim=..., ylim=...) instead.

Horizontal grid not matching y axis ticks

I have the following data:
x=c(2.880262,3.405859,3.613575,3.744480,3.682059,3.694075,3.758320,4.034290,4.202741,4.309383,4.996279,5.981309,5.103148,4.926363,4.696024,5.522913,5.330382,4.434304,5.154567,6.247156,8.612752,9.996526,9.606994,10.303496,5.954970,5.688171,6.340349,6.252854,6.355642,5.988570,7.317148,11.664384,14.231579,16.489029,23.100640,20.280043,21.562793,24.311327,23.735198,23.796386,23.118181,23.269722,19.886981,20.000975,19.967642,24.278910,17.447721,14.536114,20.646378,19.096832,20.258060,19.803196)
y=1:52
w=c(-2784,-2897,-2897,-2066,-2466,-2466,-2466,-2466,-2102,-2102,-2102,-2202,-2094,-2094,-2094,-2094,-1691,-1691,-1691,-1691,-1691,-1674,-1774,-1774,-2019,-2019,-2019,-2019,-2019,-1988,-1988,-1988,-1988,-1988,-1888,-1888,-1888,-1888,-1888,-1888,-1888,-1488,-2051,-2051,-2051,-2051,-2315,-2315,-2315)
v=1:49
When I try to plot these, my grid does not match the tick marks. Is there a way to fix this in base?
plot(y,x,type='l',col='blue',log='y')
grid(NA,NULL)
Resulting plot:
And the other plot:
plot(v,w,type='l',yaxt='n')
grid(NA,NULL)
axis(2,pretty(w),format(pretty(w)/1000,big.mark=','))
Result:
I put both up because I am using different techniques to label the y axis, and one is a log chart while the other is not. By the way, I have hundreds of other data sets that are placing the grid lines by the tick marks. It is just these two that are not matching grids to ticks.
For the first plot, just use equilogs=F.
For the second plot, since you are using non-default axis ticks, I think you'll have to resort to abline like it says in ?grid. Good luck!

stop ggplot2 from dropping data points outside of axis limits?

If I make a line plot (a time series, for example) and set my axis limits, I want the line to continue off the plot when points are outside of the axis limits, and then come back into the plot for the next point that is within the axis limits. Right now, it seems that ggplot2 will just drop the points completely and give me a an "Error:" message.
If you limit your axes by reducing the axis scale (scale_x_continuous(limits=...)), then that is the expected behavior. By adjusting the scale, you are defining what data should be part of the plot. If you want to use all the data, but just zoom in on a particular region of the axes, you want to use coord_cartesian(xlim=..., ylim=...) instead.

Resources