Data
library(ggplot2)
library(ggrepel)
library(scales)
dat<-read.csv("body_brain_masses.csv")
dat<-data.frame(dat, row.names = TRUE)
head(dat)
## body brain
## Mountain beaver 1.35 8.1
## Cow 465.00 423.0
## Grey wolf 36.33 119.5
## Goat 27.66 115.0
## Guinea pig 1.04 5.5
## Dipliodocus 11700.00 50.0
Color-Opacity Separation
ggplot(dat, aes(x=body, y=brain, label=rownames(dat))) +
geom_point(color="orange", alpha=0.6) +
scale_x_log10(sec.axis=sec_axis(~.)) +
scale_y_log10(sec.axis=sec_axis(~.)) +
geom_text(size=2.5, vjust=0, hjust=0.5) +
ggtitle("Brain to Body Mass Ratio")

Force-Positioned Labels
ggplot(dat, aes(x=body, y=brain, label=rownames(dat))) +
geom_point() +
geom_text_repel(size=3) +
scale_x_log10(breaks=10^(-1:5),
labels=trans_format("log10", math_format(10^.x)),
sec.axis = dup_axis(labels = scales::number, name=element_blank())) +
scale_y_log10(breaks=10^(0:3),
labels=trans_format("log10", math_format(10^.x)),
sec.axis = dup_axis(labels = scales::number, name=element_blank())) +
ggtitle("Brain to Body Mass Ratio")

Custom Function
# Formatter
exp_to_number <- function(x) {
if (x >=5) {
return (scales::number(x))
} else {
return (x)
}
}
ggplot(dat, aes(x=body, y=brain, label=rownames(dat))) +
geom_point() +
geom_text_repel(size=3) +
scale_x_log10(breaks=10^(-1:4),
labels=trans_format("log10", math_format(10^.x)),
sec.axis = dup_axis(labels = exp_to_number, name=element_blank())) +
scale_y_log10(breaks=10^(0:3),
labels=trans_format("log10", math_format(10^.x)),
sec.axis = dup_axis(labels = exp_to_number, name=element_blank())) +
ggtitle("Brain to Body Mass Ratio")
## Warning in if (x >= 5) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (x >= 5) {: the condition has length > 1 and only the first
## element will be used
