Search code examples
rggplot2scaleaxis

R: custom tick markers for y-axis using log10 scale in ggplot


I'm having great difficulty trying to create a nice y-axis when I use a log10 scale.. I'v tried things I have seen on SO but they give me a messed up plot. This is the desired y-axis I want: enter image description here

What type of arguments do I need to provide in the scale_y_log10() function to get the desired outcome? Any help or advice is greatly appreciated! Thanks in advance! Code I'm using:

library(ggplot2)

box_whisk_graph<-ggplot(data = box_fresh_chloride, aes(x = factor(year), y = val)) +
  geom_boxplot(coef=5) +
  geom_hline(aes(yintercept = 230,colour="red"),size=1.3)+
  geom_hline(aes(yintercept = 860,colour="#FF3333"),size=1.3,linetype="dotted")+
     scale_y_continuous(trans="log10",limits=c(10,2000),breaks=c(10,2000,100))

Plot I'm getting: enter image description here

Sample dataset:

box_fresh_chloride = structure(list(orgid = c("USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ"), locid = c("USGS-01482500", 
"USGS-0146453250", "USGS-01392150", "USGS-01411035", "USGS-01411466", 
"USGS-01411444", "USGS-01407821", "USGS-01464527", "USGS-01405340", 
"USGS-01409387", "USGS-01403385", "USGS-01467005", "USGS-01409815", 
"USGS-0146708240", "USGS-01411110", "USGS-01400000", "USGS-01467150", 
"USGS-01391500", "USGS-01467010", "USGS-0140940950"), stdate = structure(c(16394, 
16610, 16328, 16583, 16602, 16602, 16602, 16588, 16588, 16588, 
16588, 16589, 16590, 16590, 16594, 16589, 16589, 16595, 16602, 
16583), class = "Date"), sttime = structure(c(35100, 39600, 38400, 
35100, 43200, 37800, 36900, 33600, 45000, 46200, 40500, 47400, 
42300, 36000, 39600, 40500, 34200, 39600, 34200, 45000), class = c("hms", 
"difftime"), units = "secs"), charnam = c("Chloride", "Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride", "Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride", "Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride", "Chloride"
), val = c(23.6, 221, 144, 10.8, 10.5, 5.76, 37.1, 22.5, 78.3, 
8.67, 51.5, 17.9, 4.48, 55.8, 16.1, 62.9, 67.6, 187, 47.5, 27.7
), valunit = c("mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", 
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", 
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l"), swqs = c("FW2-NT", 
"FW2-NT", "FW2-NT", "PL", "FW2-NT", "PL", "FW2-NT", "FW2-NT", 
"FW2-NT", "PL", "FW2-NT", "FW2-NT", "PL", "FW2-NT", "PL", "FW2-NT", 
"FW2-NT", "FW2-NT", "FW2-NT", "PL"), WMA = c(17L, 20L, 4L, 15L, 
17L, 16L, 12L, 20L, 9L, 14L, 9L, 19L, 14L, 18L, 15L, 8L, 18L, 
4L, 19L, 14L), year = c(2014, 2015, 2014, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2015)), .Names = c("orgid", "locid", "stdate", "sttime", 
"charnam", "val", "valunit", "swqs", "WMA", "year"), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

Solution

  • One way to solve this issue is to manually build yourself a vector of 'breaks' values, and then provide those values to the scale_y_continuous() function call.

    breaks_vec = rep(seq(from=1, to=10), 3) * 10^rep(seq(from=1, to=3), each=10)
    
    box_whisk_graph <- ggplot(data=box_fresh_chloride, aes(x=factor(year), y=val)) +
                       theme_bw() +
                       geom_boxplot(coef=5) +
                       geom_hline(yintercept=230, colour="red", size=1.3)+
                       geom_hline(yintercept=860, colour="#FF3333", size=1.3, 
                                  linetype="dotted")+
                       scale_y_continuous(trans="log10", limits=c(10,2000), 
                                          breaks=breaks_vec, minor_breaks=NULL)
    

    enter image description here