1

I am new to R and ggplot and have been trying to remove part of y-axis range for hours and still haven't managed to make it happen. I wrote the following code snippet to make things reproducible in case someone knows the answer. So here is a code snipped:

library(ggplot2)
p<- ggplot(data.frame(), aes(color=c("I","Y","I","Y","I","Y"), y=c(75.33,72.95,86.46,79.63,1136.09,993.27), x=c(1,1,2, 2,3, 3))) + 
  geom_line() + 
  geom_point()+
  scale_x_continuous(breaks=c(1,2,3),labels = c("A","B","C"))+
  scale_y_log10() +
  annotation_logticks(sides = "l")+
  theme(legend.position = "none")+ labs(x = "", y = "")
print(p)

And the plot looks like

enter image description here

I want to remove the y-axis range from 100 to 900 to help with the visibility of the values below 100. I have already tried this and this but neither of them work (I get weird NA erros, etc). Would appreciate a help.

user3639557
  • 4,791
  • 6
  • 30
  • 55
  • 2
    The whole point of a plot is that the distance between points are interpretable. If you remove a chunk of your axis, you are violating that basic rule. The connecting line would be meaningless. Are you sure that's what you want? – MrFlick May 15 '21 at 05:02
  • Yes, unless there is a way to show the lost information for those values that are squashed between 0-100. To be more clear, the purpose of this plot is not to compare A, B, C. It is to compare the blue and red line on setting A, setting B, setting C. – user3639557 May 15 '21 at 05:07

2 Answers2

2

How about this as an alternative, if you're looking to emphasize the blue and red within each setting?

ggplot(df1, aes(x, y, color = color)) +
  geom_errorbar(aes(ymin = y, ymax = y)) +
  geom_point() +
  facet_wrap(~x, scales = "free") +
  expand_limits(y = 0) +
  theme(legend.position = "none")+ 
  labs(x = NULL, y = NULL)
  

enter image description here

input data

df1 <- data.frame(
  color=c("I","Y","I","Y","I","Y"),
  y = c(75.33,72.95,86.46,79.63,1136.09,993.27),
  x= c(rep(LETTERS[1:3], each = 2)))
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

Hi I am not a big fan of the "squishing" with user-defined transformations. Logarithmic scales cause already enough pain for some analysts / readers.
I support @Jon's approach using facets and scale_free. However, one of the posts that you pointed to provides an interesting example and defines a generic squish_trans function` which was fun to test. Just adapt things properly!

Kudos to @Stibu for developing and sharing the squish_trans() function. Check out R/ggplot2: Collapse or remove segment of y-axis from scatter-plot for more details.

Key points to note: work with scale_y_continuous() and set visually appealing breaks.

library(ggplot2)

p<- ggplot(data.frame(), aes(color=c("I","Y","I","Y","I","Y"),     y=c(75.33,72.95,86.46,79.63,1136.09,993.27), x=c(1,1,2, 2,3, 3))) + 
    geom_line() + 
    geom_point()+
    scale_x_continuous(breaks=c(1,2,3),labels = c("A","B","C")) +
#--------------- apply squish_trans to scale_y_continuous --------------------
# settings chosen: lower bound = 200, upper bound 900
# defining breaks to show on graph and 
# setting limits to "force" a top level tick (this is not strictly necessary, but  looks better ... I think)
    scale_y_continuous( trans  = squish_trans(200, 900, 100)
                       ,breaks = c(0, 50, 100, 900, 1000, 1100, 1200)
                       ,limits = c(0, 1200)
    ) +
#-------------- remove the additional log-based ticks
# commented out to show
    #  annotation_logticks(sides = "l")  +

    theme(legend.position = "none")+ labs(x = "", y = "")

# print plot
p

This yields:

enter image description here

Ray
  • 2,008
  • 14
  • 21