Change axes X and Y graphs ggplot in R

Asked

Viewed 5,543 times

0

I would like to leave the graph in the ggplot so that the y-axis scale, is in millions of tons, so that the values become smaller, and also in the x-axis, appears every month and year.

I used this code to run the graph

Data=seq(as.Date("2018-01-01"), by="1 month", length.out=18)

PAbr=cbind(Data,PAbr)

ggplot(data = PAbr, aes(x = Data, y = Produção)) +

  geom_line(size=1)+ 

  scale_x_date(date_labels = "%b/%Y") + theme_minimal()

And my database is billions of tons of data, so the data doesn’t appear whole on the y-axis, the way I’d like it, so the idea of changing the scale. inserir a descrição da imagem aqui

If anyone can help me, I’d appreciate it.

3 answers

4

# Dados de exemplo
set.seed(123)
PAbr <- data.frame(
  Data = seq(as.Date("2018-01-01"), by = "1 month", length.out = 18),
  Produção = rnorm(18, 10^9, 10^8) )

library(ggplot2)

ggplot(PAbr, aes(Data, Produção/10^9)) +
  geom_line(size = 1) + 
  scale_x_date(NULL, date_labels = "%b/%y", date_breaks = "1 month") +
  scale_y_continuous("Produção (em milhões de toneladas)") +
  theme_minimal()

inserir a descrição da imagem aqui

  • Thank you very much!!

  • Carlos, could you explain better the argument date_labels = %b/%y inside the scale_x_date( ) ? A "%" followed by "b" classifies that position as?

  • date_breaks specifies the time interval to be used on labels; for semester it would be "6 months". date_labels indicates the display format; check ?strptime to see the options.

2

Just giving another option using the great example of Carlos. If you want something more complicated/different changing the axes you can use the package scales, which is what the ggplot uses to create the axes.
Here I create a function transf_eixo which specifies that the axis will be transformed into 1e-9, rounded to the first decimal place and comma as separator. And then I apply the function to the y-axis.
The function and the package have more options and formats to work with, so they give more power to leave the axes as you want.

# Dados de exemplo de Carlos
set.seed(123)
PAbr <- data.frame(
  Data = seq(as.Date("2018-01-01"), by = "1 month", length.out = 18),
  Produção = rnorm(18, 10^9, 10^8) )

library(ggplot2)
library(scales)

transf_eixo <- number_format(scale = 1e-9, accuracy = .1, decimal.mark = ",")

ggplot(PAbr, aes(Data, Produção)) +
  geom_line(size = 1) + 
  scale_x_date(NULL, date_labels = "%b/%y", date_breaks = "1 month") +
  scale_y_continuous(name = "Produção (em milhões de toneladas)",
                     labels = transf_eixo) +
  theme_minimal()

0

Try to use this

p1 + scale_y_continuous(breaks = c(2, 4, 6))

Break are the scales, try to put manually.

  • He is not accepting when I put scale_y_continuous(breaks = c()), in the parentheses of c, would have to put for example, 10 9.10 8.. it returns that message: Scale for 'y' is already present. Adding Another Scale for 'y', which will replace the existing Scale.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.