Juan Carlos
Juan Carlos

Reputation: 305

Customizing the y-axis in ggplots2 without using free scale

I´m trying to plot a facet_wrap setting the y axis limit individually. I´ve looked for the solution in the forum but I didn´t find it.

My data looks like these,

structure(list(fecha = c(2000, 2000, 2000, 2000, 2000, 2000, 
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2003), cientifico = c("Gaidropsarus macrophthalmus", 
"Gadiculus argenteus", "Microchirus variegatus", "Arnoglossus laterna", 
"Blennius ocellaris", "Eutrigla gurnardus", "Argentina sphyraena", 
"Micromesistius poutassou", "Lesueurigobius friesii", "Callionymus maculatus", 
"Lepidorhombus boscii", "Capros aper", "Chelidonichthys cuculus", 
"Helicolenus dactylopterus", "Callionymus lyra", "Arnoglossus imperialis", 
"Chelidonichthys obscurus", "Trisopterus minutus", "Buglossidium luteum", 
"Trachinus draco", "Serranus cabrilla", "Scorpaena loppei", "Gadiculus argenteus", 
"Microchirus variegatus", "Helicolenus dactylopterus", "Gaidropsarus macrophthalmus", 
"Eutrigla gurnardus", "Lesueurigobius friesii", "Argentina sphyraena", 
"Arnoglossus laterna", "Micromesistius poutassou", "Blennius ocellaris", 
"Callionymus maculatus", "Chelidonichthys cuculus", "Capros aper", 
"Lepidorhombus boscii", "Callionymus lyra", "Arnoglossus imperialis", 
"Chelidonichthys obscurus", "Trachinus draco", "Serranus cabrilla", 
"Buglossidium luteum", "Trisopterus minutus", "Scorpaena loppei", 
"Blennius ocellaris", "Arnoglossus laterna", "Argentina sphyraena", 
"Micromesistius poutassou", "Helicolenus dactylopterus", "Callionymus lyra", 
"Callionymus maculatus", "Capros aper", "Eutrigla gurnardus", 
"Microchirus variegatus", "Gadiculus argenteus", "Lepidorhombus boscii", 
"Lesueurigobius friesii", "Gaidropsarus macrophthalmus", "Arnoglossus imperialis", 
"Chelidonichthys cuculus", "Chelidonichthys obscurus", "Buglossidium luteum", 
"Trachinus draco", "Trisopterus minutus", "Serranus cabrilla", 
"Scorpaena loppei", "Arnoglossus laterna"), q95 = c(16, 11, 15, 
12, 15, 24, 20, 20, 7, 10, 26, 14, 25, 33, 27, 15, 29, 20, 13, 
27, 22, 13, 10, 15, 17, 15, 21, 7, 20, 12, 21, 15, 12, 27, 13, 
26, 26, 15, 29, 26, 23, 13, 20, 12, 15, 12, 20, 21, 14, 27, 12, 
14, 23, 15, 11, 26, 7, 15, 15, 27, 28, 12, 25, 21, 22, 14, 13
)), row.names = c(1L, 3L, 4L, 5L, 6L, 8L, 11L, 12L, 15L, 16L, 
17L, 18L, 20L, 22L, 24L, 28L, 32L, 36L, 38L, 39L, 42L, 43L, 44L, 
46L, 47L, 48L, 49L, 51L, 52L, 53L, 55L, 57L, 58L, 60L, 63L, 64L, 
70L, 72L, 79L, 80L, 81L, 82L, 83L, 86L, 87L, 88L, 89L, 90L, 91L, 
92L, 93L, 94L, 98L, 101L, 103L, 104L, 106L, 107L, 116L, 117L, 
120L, 121L, 123L, 126L, 127L, 128L, 131L), class = "data.frame")

And using the following code,

  p <- ggplot(peques, aes(x = fecha, y = q95)) + geom_point(color="black") +
  geom_line(color="blue") +
  facet_wrap(~cientifico, ncol=3, scales="free") + 
  labs(x="Año", y="Q95 (cm)", face="bold") +
  theme_bw()+ 
  annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf, size=1)+ 
  annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf, size=1)+
  theme(axis.ticks.length=unit(.15,"cm"),
  strip.text = element_text(face = "italic"),
  axis.title.x = element_text(face="bold", vjust=2.95, size=14), 
  axis.title.y = element_text(face="bold", vjust= 1.95, size=12),
  axis.text.x = element_text(size=8),  
  axis.text.y = element_text(size=8)) +
  scale_x_continuous(breaks = seq(1990,2020, by=1),labels = c(1990, rep("",4), 1995, rep("",4), 2000, rep("",4), 2005, rep("",4),
           2010, rep("",4), 2015, rep("",4), 2020 )+
scale_y_continuous(limits=c(min[i], max[i)]), breaks=intervalo[i]), labels=labels[i])) 

I get this graph enter image description here

But I want to define the intervals in the y axis, set all the numbers as integer not some as decimal.... I´m trying with the following code just before the ggplot to define the min, the max and the interval for each species but no luck.

min <- list()
max <- list()
intervalo <- list()
sp <- list()

for (i in 1:length(especies_peques)){
  sp[[i]] <- subset(peques, cientifico ==especies[i])
  
min[[i]] <- min(sp[i]$q95)
max[[i]] <- max(sp[i]$q95)
intervalo[[i]] <- seq(min[i], max[i], by=2)
labels[[i]] <- as.character(intervalo[i])
labels[!(intervalo %% 2 == 0)] <- ""

I hope I´ve explained myself well. Any hint for solving my problem will be more than welcomed. Thanks

Upvotes: 0

Views: 40

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26640

Your code didn't work for me, so this might not help, but a potential solution to reduce 'clutter' on the y-axis is to use the scales::pretty_breaks() function, e.g.

library(tidyverse)
library(palmerpenguins)

penguins %>% 
  na.omit() %>% 
  ggplot(aes(x = species, y = flipper_length_mm)) +
  geom_boxplot() +
  facet_wrap(~year + island)

example_1.png

And, with scales::pretty_breaks():

penguins %>% 
  na.omit() %>% 
  ggplot(aes(x = species, y = flipper_length_mm)) +
  geom_boxplot() +
  facet_wrap(~year + island) +
  scale_y_continuous(breaks = scales::pretty_breaks(4))

example_2.png

Upvotes: 1

Related Questions