Set coordinate axes in ggplot2

1

It turns out that I have a function written in R to print a box and whisker plot. What I do in question is this:

mytitle2 <- "Contenido = 1.0"
a <-subset(global_data,Contenido==1.0 & Rango==3)
ggplot(d, aes(fase)) +
  labs(title=mytitle2) +
  xlab("fase") +
  ylab("intensidad") +
  geom_point(position=position_jitter(width=.2,height=.1), aes(x=Fase, y=Intensidad, color=Interaccion)) + 
  scale_colour_gradient(low="red", high="blue") +
  scale_y_continuous(limits = c(0, 3))

Good. As it turns out, I want to get a series of graphs for different values of "Content". The fact is that given the data I have, sometimes the y-axis appears with the values between 0 and 3, while other times (when the datapoints accumulate close to 3), the default x-axis appears shortened, representing only values between 1 and 3.

I would like to know how to set the coordinate axes so that the graphics always represent the assigned values in the range of 0 to 3.

Thank you.

    
asked by pyring 09.11.2017 в 22:44
source

1 answer

1

Let's see what options we have to configure the axes in ggplot . First we generate a graphic similar to your example:

library(ggplot2)
set.seed(100)
mytitle2 <- "Contenido = 1.0"
global_data <- data.frame(Fase = runif(1000, min=0, max=3), 
                          Intensidad = runif(1000, min=0, max=3), 
                          Interaccion=sample(1:5, 1000, replace = TRUE))
a <-subset(global_data,Intensidad > 2 & Fase > 2)
ggplot(a, aes(fase)) +
    labs(title=mytitle2) +
    xlab("fase") +
    ylab("intensidad") +
    geom_point(position=position_jitter(width=.2,height=.1), aes(x=Fase, y=Intensidad, color=Interaccion)) + 
    scale_colour_gradient(low="red", high="blue") +
    scale_y_continuous(limits = c(0, 3))

In this example we have cropped the axis sample x e y to the values closest to 3 , if we see the graph:

We see that the axis x was self-adjusted to the sample, if we do not want this to happen, we must repeat what you have done for the axis y , that is:

+ scale_x_continuous(limits = c(0, 3))

or for the two axes we can also use:

 + xlim(0, 3)
 + ylim(0, 3)

On the other hand, what you mention about the axes labels can be solved by configuring axis.title.y and axis.title.x of theme() , you have to define a element_text and configure the margin defining the properties top, rigth , bottom and left.

+ theme(axis.title.y = element_text(margin = margin(t = 0, r = 30, b = 0, l = 0)),
      axis.title.x = element_text(margin = margin(t = 30, r = 0, b = 0, l = 0)))

For the title of the color indicator legend I have not found any option in the theme configuration, but you can always manually configure the title and add a line break, for example:

scale_colour_gradient(name="Title\n", low="red", high="blue") +

The final result would be something like this:

    
answered by 10.11.2017 / 03:01
source