Cassandra
Cassandra

Reputation: 137

scale_y_continuous not plotting the top break/label

I am trying to add y-axis labels, but the labels for 8 and 16 label is not plotting even though I have specified breaks/labels for that value. All the other breaks/labels that I have specified show up.

ggplot(data = total.incidence, aes(x = Year, y = Value, group = City , color = City )) +
  geom_line(size = 1.5, alpha = 0.75) +
  geom_errorbar(aes( ymin=Value_low, ymax=Value_hi), width=0.5, size = 1, alpha = 0.75) +
  scale_x_continuous(breaks = c(2020, 2025, 2030, 2035, 2040), 
                     labels = c(2020, 2025, 2030, 2035, 2040), 
                     limit = c(2020, 2040.3),
                     expand = c(0, 0)) + 
  scale_y_continuous(breaks = c(0.5, 1, 2, 4, 8, 16 ), 
                     labels = c(0.5, 1, 2, 4, 8, 16 ),
                     trans = "log2" ) + 
  guides(colour  = guide_legend(nrow = 1, override.aes=list(fill=NA))) +
  theme(axis.line = element_line(colour = "black"),
        axis.title.x = element_text(size = 11 ),
        axis.text = element_text(size = 11))

I have tried to extend the y-range by specifying limits = c(0, 16) and limits = c(0, 16.1), it produces an empty plot (no data or y-axis) and I get the following warning:

Warning message:
The position guide is perpendicular to the intended axis. Did you mean to specify a different guide `position`? 

Thanks!

EDIT: Reprex df:

total.incidence <- structure(list(City = structure(c(6L, 4L, 3L, 4L, 2L, 2L, 5L, 2L, 6L, 4L, 6L, 1L, 3L, 3L, 2L), 
.Label = c("A", "B", "C", "D", "E", "F"), 
class = "factor"), 
Year = c(2024L, 2025L, 2026L, 2026L, 2028L, 2021L, 2037L, 2038L, 2026L, 2023L, 2027L, 2035L, 2040L, 2027L, 2036L), 
Value = c(0.95, 3.6, 2.2, 3.2, 0.6, 2.7, 0.8, 0.4, 0.8, 5.2, 0.8, 0.8, 1.7, 2.1, 0.4 ), 
Value_low = c(0.7, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
Value_hi = c(1.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), 
row.names = c(160L, 99L, 69L, 100L, 40L, 33L, 142L, 50L, 162L, 97L, 163L, 16L, 83L, 70L, 48L), class = "data.frame")

Upvotes: 2

Views: 4301

Answers (1)

Peter
Peter

Reputation: 12699

Does this do what you expected:


library(ggplot2)

ggplot(data = total.incidence, aes(x = Year, y = Value, group = City , color = City )) +
  geom_line(size = 1.5, alpha = 0.75) +
  geom_errorbar(aes( ymin=Value_low, ymax=Value_hi), width=0.5, size = 1, alpha = 0.75) +
  scale_x_continuous(breaks = c(2020, 2025, 2030, 2035, 2040), 
                     labels = c(2020, 2025, 2030, 2035, 2040), 
                     limit = c(2020, 2040.3),
                     expand = c(0, 0)) + 
  scale_y_continuous(breaks = c(0.5, 1, 2, 4, 8, 16 ), 
                     labels = c(0.5, 1, 2, 4, 8, 16 ),
                     trans = "log2",
                     limit = c(-1, 16)) + 
  guides(colour  = guide_legend(nrow = 1, override.aes=list(fill=NA))) +
  theme(axis.line = element_line(colour = "black"),
        axis.title.x = element_text(size = 11 ),
        axis.text = element_text(size = 11),
        legend.position = "bottom")

The scale_y limit argument needed a -1 for the lower limit to make the plot show. I think this may be on account of your choice to use a log2 transformation as the value of log 0 is undefined.

Created on 2020-05-24 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions