0
votes

I am attempting to make a figure similar to this:

enter image description here

with log ticks on the y axis and not the x. I've been able to produce this with the following code:

setwd('/Users/marleyjarvis/Desktop/')
CompMero=read.csv("CompMero.csv")
library(ggplot2)
library(scales) #for the trans and format functions
attach(CompMero)

ggplot(CompMero, aes(x=station, y=Mean)) + 
geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
geom_point(size=3) +
xlab("Tow station with respect to foam line and front") +
ylab(expression(paste(Mean~no~per~m^3))) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) + 
scale_x_continuous(breaks=c(1:3), labels=c("Inshore", "Foam line", "Offshore")) +
annotation_logticks(sides = "l") + #log ticks only on y!!!!
scale_y_log10(breaks = trans_breaks("log10", n=6, function(x) 10^x),
          labels = trans_format("log10", math_format(10^.x)),
          minor_breaks = log10(5) + -1:3) +
coord_fixed()

which results in the following figure output:

enter image description here

MY PROBLEM: There are two things about the fig that I haven't been able to figure out how to change (despite hours of searching the docs, web, etc)

  1. I would like the Y-axis numbering to NOT be in exponents: i.e. I would like the y-axis labels to read "100", "1000" etc rather than "10^2", "10^3". I've tried reading ?trans_breaks and ?trans_format and searching for math_format() syntax as well as experimenting with changing 10^.x but haven't had any luck. I will be producing these graphs for multiple sets of data with different y-axis ranges, so I would like to write this into the code to reflect the y-data range rather than simply changing the labels to "100" "1000" etc. I hope that makes sense.

  2. The "half-way" tick labels and outer ticks do not match up with the inside halfway ticks. In other words, the "10^2.5" label, which I'm refering to as the half-way tick between "10^2" and "10^3", has an outer tick mark that does not match up with the inner tick mark that it should. Instead, this outer tick mark is located physically half the distance between "10^2" and "10^3", which is positionally incorrect with the inner tick marks (which I want the outer to match). From what I understand from reading recipe 8.15 "Adding ticks for a logarithmic axis" in the R Graphics Cookbook, the minor_breaks = log10(5) + -1:3) line in my code should solve this problem, but I must be doing something wrong. I haven't been able to figure it out. Alternatively (and perhaps preferably for my final end product to publish) I would like to simply remove these halfway ticks and labels all together. I'd like to learn how to do the above (line up the halfway ticks) as well though.

My sample data: sample data: CompMero

edit: sample data is in columns with names = stationID, station, Mean, StErr
station ID: Foam line, Inshore, Offshore station: 2, 1, 3 Mean: 3419.401, 35.56681, 70.47807 StErr: 1888.509, 11.55935, 40.04964

Thank you VERY much for any help or pointers. I've spent quite a bit of time reading docs and help files and searching past questions and haven't been able to solve these two issues. I'd really appreciate it if someone could point me in the right direction.

1

1 Answers

2
votes

In case anyone else stumbles across this question and might benefit, I've worked out the axis labeling and got rid of those pesky "half-way" ticks that were misaligned using the following code:

ggplot(df, aes(x=station, y=Mean)) + 
geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
geom_point(size=3) +
xlab(NULL) +
ylab(NULL) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) + 
scale_x_continuous(expand=c(.2,0), breaks=c(1:3), labels=c("IN", "FL", "OFF")) +
annotation_logticks(sides = "l") + #log ticks only on y!!!!
scale_y_log10() +
theme(axis.text.x=element_text(size=16))+
theme(axis.text.y=element_text(size=16))