0
votes

I read this answer : How to control number of minor grid lines in ggplot2?

Although I couldn't figure out a way to reconcile it with my requirements.

I want there to be a way to input the number of minor gridlines between two major ticks. (or the ratio of the minor to major grid size) Say I want to divide it into 5 parts (4 minor gridlines). How do I do that?

Since there will be many graphs, of which I wouldn't know the axis limits, I can't explicitly define the size of one minor gridline step. I want to use whatever algo ggplot2 uses to pick the number of major gridlines, and just have 4 times as many minor ones.

I'd like the r graph on the right to look like the excel graph on the left

enter image description here

CODE (in case that helps solve the issue)

ggtheme <- theme(axis.line = element_line(size = 0.5, colour = "black"), 
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour="grey90",size = rel(0.5)),         
panel.grid.minor = element_line(colour="grey95",size = rel(0.25)));

ggp_sctr2 = ggplot( sub2_ac_data, aes(x=(sub2_ac_data[,i]), 
y=sub2_ac_data[, rescol], colour = factor(sub2_ac_data[,topfac[1]]), 
shape = factor(sub2_ac_data[,topfac[1]])  )) + geom_point(size = 2.5) +
scale_shape_manual(values=symlist[Nmsn_sub1+1:20])  + 
scale_colour_manual(values = unname(cols[Nmsn_sub1+1:16])) + 
geom_smooth(method="lm", formula = y ~ splines::bs(x, 3),  
linetype = "solid", size = 0.25,fill = NA ) 

print(ggp_sctr2 + ggtitle( paste(scxnam[1],nomvar,
"vs",colnames(sub2_ac_data[i]),i, sep = " ")) + 
theme(axis.text.x=element_text(angle = 90, hjust = 1,vjust = 0.5,size=8)) + 
labs(x = colnames(sub2_ac_data[i]), y=colnames(sub2_ac_data[rescol]), 
colour=colnames(sub2_ac_data[topfac[1]]), 
shape=colnames(sub2_ac_data[topfac[1]])) + ggtheme + 
theme(plot.title = element_text(face = "bold", size = 16,hjust = 0.5))) ;
1
Welcome to StackOverflow! For code debugging please always ask with a reproducible example per the MCVE and r tag description, with the desired output. You can use dput(), reprex::reprex() or built-in data sets for reproducible data.Hack-R
So I added the part of the code that has the definition of the theme, as well as the lines where it's used. Although I imagine the first part would be the more useful section... Please feel free to suggest other improvements/refinements to it as wellShaurya Verma
I don't see where you attempted to include the suggestion in the answer you linked to. Did you try using minor.breaks? If you did, what happened?aosmith
Ah yes, I tried to add them at the ends of each of the three lines. The following happened, given by line number: [[1]] Error: Don't know how to add RHS to a theme object. __ [[2]] Got no minor vertical gridlines. ___ [[3]] No visible impact.Shaurya Verma
Also, I found an answer closer to what I want but it doesn't seem to be working for me: stackoverflow.com/questions/39691591/… I copy pasted their exact code and that also gave the same NULL value for majors, and then an error subsequently. Could you (or someone) please test it at your (their) end?Shaurya Verma

1 Answers

0
votes

I found one possible solution, although it's not very elegant.*

Basically you extract the major gridlines and then create a sequence of minor gridlines based on a multiplier.

I've replied to a related question (as a modification to an answer by Eric Watt). It's just a change of syntax, that wasn't explained too well in any documentation.

This is the link: ggplot2 integer multiple of minor breaks per major break

Here is the code:

library(ggplot2)

df <- data.frame(x = 0:10,
                 y = 10:20)
p <- ggplot(df, aes(x,y)) + geom_point()

majors <- ggplot_build(p)$layout$panel_params[[1]]$x.major_source;majors
multiplier <- 5
minors <- seq(from = min(majors),
              to = max(majors),
              length.out = ((length(majors) - 1) * multiplier) + 1);minors
p + scale_x_continuous(minor_breaks = minors)

*Not very elegant because:

  1. It fails in case the graph doesn't get any data (which can happen in a loop, so exceptions need to be made for it).

  2. It only starts minor gridlines from the min to max of the existing major gridlines, not throughout the extremities of the coordinate space where points can be placed