Only show every nth axis tick marks in ggplot - r

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"))

Related

R: How to center the tick labels (putting labels between tick marks) using ggplot?

I was using R ggplot for data visualization, and I met the problem centering the tick labels between tick marks on x/y axes. Here's a similar question but the implementation is matplotlib.
This is what I had (the wrong version):
And this is what I expected:
THANKS A LOT!!!
It is hard to reproduce your plot without the data you used to create the plot or the code you used to generate the plot
You can move the x-axis labels to the right by adding the following code to your plot
+ theme(axis.text.x=element_text(hjust=-0.5))
Additionally, one option might be just to rotate the x-axis labels instead of moving them. You can do this by adding the following code
+ theme(axis.text.x=element_text(angle = 90))

axis.break does not move with axis R

I want to plot the vector 4:10 but from 0 to 10. I don't like the large gap in produces, and I want to create my own style of plot, so I write the code:
plot(4:10,axes=FALSE,ylim=c(2,10),xlim=c(0,8))
axis(1,pos=2)
axis(2,pos=0,at=seq(2,10,2),labels=c('0','4','6','8','10'))
In order to not mislead the audience, I want to put in an axis break using axis.break() from plotrix. Unfortunately, when I add
axis.break(2,2.5)
on my plot, I don't get the break where my axis is but where it would be if I used the default axis. How do I get axisbreak() to move with my axis? Or else, how do I put an axis break where I want it?
Add pos=0 to your axis.break:
plot(4:10,axes=FALSE,ylim=c(2,10),xlim=c(0,8))
axis(1,pos=2)
axis(2,pos=0,at=seq(2,10,2),labels=c('0','4','6','8','10'))
axis.break(2,2.5,pos=0)

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.

Inverting a single axis plot in R

I plotted a single column in R with the code below
plot(datanew$cap)
But what I actually need is that the points on the x-axis should be on the y-axis while the point on the y-axis should be the x-axis. If anyone has an idea on how I can do this, I will greatly apreciate.
Plotting via
plot(datanew$cap)
is basically the same as
plot(seq(length(datanew$cap)), datanew$cap)
You can use this to your advantage by swapping the arguments (or specifiying x- and y-coordinates explicitly):
plot(x=datanew$cap, y=seq(length(datanew$cap)))

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!

Resources