How to split a sequence into two ranges in ggplot

Hi All,
I would like to split a sequence into two ranges, something like this in: scale_x_continuous(breaks = seq(0.0, 1, 0.5) & seq(1,20, 1)).
Is it possible in R to have such a different breaks on X axis ?

breaks = c(seq(0.0, 1, 0.5), seq(1,20, 1))

Thank you, but it doesn't work.

If you actually want help, then you will have to supply some detail. I can only guess that you want to change the labels argument, too. All the details are in the documentation:

Of course, I will prepare a reprex.

Hi @martin.R, if you be so kind and have a look at reprex, please:

library(tidyverse)
library(magrittr)

boxLabels2 <-  c("r1", "r2", "r3", "r4")


df2 <- data.frame(yAxis = boxLabels2, boxOdds = c(1.136, 3.000, 1.724, 5.909),
 boxCILow = c(0.628, 1.216, 0.997, 2.485), boxCIHigh = c(2.056,  7.404,  2.981, 14.054), 
 p_values = c(0.673, 0.014, 0.051, 0.000))


df2$yAxis <- factor(df2$yAxis, levels = boxLabels2)


df2 %<>%  mutate(p.value_Factor = if_else(
    p_values < 0.05, "p < 0.05", "p > 0.05"
  ))

df2
#>   yAxis boxOdds boxCILow boxCIHigh p_values p.value_Factor
#> 1    r1   1.136    0.628     2.056    0.673       p > 0.05
#> 2    r2   3.000    1.216     7.404    0.014       p < 0.05
#> 3    r3   1.724    0.997     2.981    0.051       p > 0.05
#> 4    r4   5.909    2.485    14.054    0.000       p < 0.05

p2 <- ggplot(df2, aes(y=yAxis)) +
  geom_point(aes(x=boxOdds)) + 
  geom_segment(aes(x=boxCILow,xend=boxCIHigh,yend=yAxis)) 


p2 <- p2 + geom_vline(xintercept=1)


p2 + scale_x_continuous(breaks = seq(1,20, 1)) +
  ylab("") +
  xlab("Odds ratios") +
  ggtitle("Odds ratios (OR) with 95% Confidence Interval")+
  ylab("Symptoms")+
  geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed", color = "red")+
 coord_trans(x = "log10") +
 geom_text(data = df2, hjust = 0, vjust = -0.4, nudge_x = -0.2, size = 5, 
aes(x = boxOdds, label = format(p_values, nsmall = 3), color = p_values > 0.05)) +
 scale_colour_manual(values = c("red", "blue"))

and plot where your code was applied:

p2 + scale_x_continuous(breaks = seq(0.5,1, 0.5), seq(1,20, 1)) +
  ylab("") +
  xlab("Odds ratios") +
  ggtitle("Odds ratios (OR) with 95% Confidence Interval")+
  ylab("Symptoms")+
  geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed", color = "red")+
 coord_trans(x = "log10") +
 geom_text(data = df2, hjust = 0, vjust = -0.4, nudge_x = -0.2, size = 5, 
aes(x = boxOdds, label = format(p_values, nsmall = 3), color = p_values > 0.05)) +
 scale_colour_manual(values = c("red", "blue"))

Created on 2022-02-02 by the reprex package (v2.0.1)

I would like to have "something" on the left hand side of "1" on x-axis. In order to do this I use: seq(0.5,1, 0.5), this is enough for the left hand side and after "1" I would like seq(1,20, 1) as I do not need so small spaces between brakes. My additional question is why on the upper plot the more to the right on the x-axis the breaks are becoming more squeezed .
Kind regards,
Andrzej

You've got the breaks argument wrong again. breaks should be one vector, but you have supplied two. Use breaks = c(0.5, seq(1,20, 1)) or similar.

The 0.5 will not be shown by default because it's outside the range of your data. You can specify xlim in the coordinate specification to zoom out (or adjust the default expansion of the scales).

You transformed the axis into a log scale and specified that you wanted each integer to be plotted, so that's why the numbers are squeezed.

I have used it the way it was by your example, this is why:

How can I change it in order not to plot every integer ?

... and yet that is not what was in the code you posted.

That's what the breaks argument does. Simply specify what values you wish to plot as a vector. By default the plot will be zoomed into the data, so you may need to zoom out if you want all specified values to be shown.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.