Horizontal grid not matching y axis ticks - r

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!

Related

Only show every nth axis tick marks in ggplot

I am able to group the dots by assigning my categorical data specific x- and y-values with empty spaces in between. Instead of showing every single tick mark on the axes,
or showing no ticks at all,
I would like to show only every nth tick mark on the axes corresponding to the labels. Can this be done in ggplot? Or maybe there is a different approach to generate the plot I am looking for.
Hard to answer without a reproducible example. You could try adding a scale_x_continuous() and scale_y_continuous() statement.
ggplot(data,aes(x,y))+
scale_x_continuous(breaks=c(0,2,4,6,8,10),labels=c("0","2","4","6","8","10"))

Multiple Axis with Plots.jl

Is there a way to have the second dataset plot on a separate axis, overlaid on the first plot?
using Plots; gadfly(size=(800,400))
plot(Vector[randn(100)], line = ([:green], :step))
plot!(Vector[randn(100)], line = ([:red], :step))
It is now done by adding a twinx() argument:
plot(rand(10))
plot!(twinx(),100rand(10))
There is, however, some unintentional axis and label behavior:
Labels are plotted on top of each other by default
xticks are plotted on top of the already existing plot
Default colors are not correlated to the total number of series in the subplot
Therefore, I suggest adding some additional arguments:
plot(rand(10),label="left",legend=:topleft)
plot!(twinx(),100rand(10),color=:red,xticks=:none,label="right")
There still seems to be an issue correlating all series associated with the subplot at the moment.
It's easy, but doesn't work with Gadfly. It should work fine with PyPlot and GR. Here's an example:
I can confirm (for GR) using Plots; gr()

Adding individual X and Y axis labels when using facet_wrap()

I am attempting to plot lots of graphs on the fly and I chanced upon the facet_wrap functionality. It produced the desired results until I realised that it was not assigning individual axes headings. There was just a single X and Y axis heading for a whole set of graphs. What I'm looking for is a way to assign individual axes headings for each graph.
Is this possible using the facet_wrap functionality at all?
Looking forward to any suggestions and advice.
EDIT:
(removed previous, incorrect, answer)
It is my understanding that if the axes of your plots are not the same (i.e. require different labels), the way to go would be with multiple separate plots (on the same page), and not with facet_wrap.

In Stata, how do I modify axes of dot chart?

I'm trying to create a dot chart in Stata, splitting it into two categories
Running a chunk of code:
sysuse nlsw88, clear
drop if race == 3
graph dot (mean) wage, over(occ) by(race)
Creates such output:
So far so good but I'd like to remove labels of Y axis from the right graph to give the data some more space.
The only way I've been able to do that was to manually edit graph and hide the axis label object:
Is there a way to do it programmatically? I do know I could use one more over() but in some graphs of mine that is already taken.
I believe the solution is buried in help bystyle and help by_option. However, I can't get it to work with your example (I'm on Stata 12). But the description is clear. For example:
A bystyle determines the overall look of the combined graphs,
including
whether the individual graphs have their own axes and labels or if instead the axes and labels are shared across graphs arrayed in the
same row and/or in the same column;
...
There are options that let you control each of the above attributes --
see [G-3] by_option --
And also
iyaxes and ixaxes (and noiyaxes and noixaxes) specify whether the y axes and x axes are
to be displayed with each graph. The default
with most styles and
schemes is to place y axes on the leftmost graph of each row and to place x axes on
the bottommost graph of each column. The y and
x axes include the
default ticks and labels but exclude the axes titles.
If for some reason that doesn't work out, something like
sysuse nlsw88, clear
drop if race == 3
graph dot (mean) wage, over(occ) by(race)
gr_edit .plotregion1.grpaxis[2].draw_view.setstyle, style(no)
does (but I don't really like the approach). You can mess with at least the axis number [#] to do a bit of customization. I guess recording changes in the graphical editor and then recycling the corresponding code, may be one way out of difficult situations.

R/Zoo: show a tick every year on x-axis

I've a zoo object, with a yearqtr index, covering about 50 years. When plotted the x-axis shows labels every 10 years, which feels a bit barren:
b=zoo(1:200,as.yearqtr(1900+seq(1,200)/4))
plot(b)
Some study got me this:
plot(b,xaxt="n");axis(1,time(b))
Which feels like swinging from one extreme to the other, as the x-axis is a blur of ticks, with ugly fractional labels. Is there an easy way to have it just show years? (What I was looking for initially was a way to say: "lower the x-axis label spacing a bit", but there seems nothing like that? cex.axis just alters the font-size.)
Did you read help(axis)?
Here is one way, just creating a simple index every four quarters:
R> ind <- seq(1, length(b), by=4)
and using it to index the axis placement and labels:
R> plot(b,xaxt="n")
R> axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.5)
I used las=2 and the lower cex value to make this fit. Once every year may still be too plenty.
Computing "good" axis labels is really hard.
This is probably one of those (rare) situations when you want to use grid rather then ticks to better show your data. As #dirk-eddelbuettel pointed out - tweaking good axis labels is hard, especially with such density. You also might want your labels inside plot, so the grid will slightly hide their density. The easiest grid to get is with abline, unless you want to play with ggplot2, but it's uglier then standard plots in R (personal opinion). Also - make the plot wider. In fact, it's better to get rid of box around plot too ;) Below is mod of Dirk's approach:
png("strangeplot.png",width=800)
#extend y-axis to fit inside labels and remove box
plot(b,type="n",xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(min(b)-30,max(b)),bty="n"))
#use 'mpg' to get labels inside
axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.6,tick=F,mgp=c(0,-2.5,0))
axis(2,tick=F,las=1)
#you locate lines slightly to the left of label...
abline(h=seq(0,200,by=50),v=time(b)[ind]-0.5,col=gray(0.9))
#...so you need to add extra single line in the end
abline(v=max(time(b)[ind])+0.5,col=gray(0.9))
#plot at the end to get it above grid
points(b,type="l")
dev.off()

Resources