Hiding values from the chart

Asked

Viewed 30 times

0

I would like to leave plotted in the graph below only the values referring to the months of JAN and FEV of the x-axis.

The idea is to keep every month of the X-axis evident in the graph and over time go feeding the variables with future values.

I was unsuccessful in the polls.

Follows the command used and a model illustrating how to plot wish.

dados <- read.table(text = 
              "Mes    local1 captura1  local2 captura2
              Jan     15      0         17      -0.002
              Fev     15      0         17      -0.02
              Mar     15   1500         17      85
              Abr     15    500         17      78
              Mai     15   1490         17      80
              Jun     15   1500         17      87
              Jul     15   1600         17      82
              Ago     15   1750         17      64
              Set     15   1800         17      86
              Out     15   1450         17      73
              Nov     15   1600         17      61
              Dez     15   1400         17      92
                ", header = T)
head(dados)
levels(dados$Mes)
dados$Mes = factor(dados$Mes, levels=c("Jan", "Fev", "Mar", "Abr", "Mai", "Jun", 
                                   "Jul", "Ago", "Set", "Out", "Nov", "Dez"))
ggplot(dados, aes(x = Mes)) +
geom_line(aes(y = captura1, color="15"), size=1, group = 1, linetype=1) +
geom_line(aes(y = captura2*14, color="17"), size=1, group = 2, linetype=4) +
geom_point(aes(y = captura1), color="purple", size=4, group = 1, shape=18) +
geom_point(aes(y = captura2*14), color="red", size=4, group = 2) +
scale_y_continuous(sec.axis = sec_axis(~./14, name = "Total de capturas 2")) +
labs(y = "Total de capturas 1", x = "Tempo (meses)") + 
scale_color_manual(name="LOCAL", values=c("#35978f", "#003c30"), 
                 guide = guide_legend(override.aes=aes(fill=group))) +
theme(legend.position=c(.8, 0.3))

Imagem ilustrativa

1 answer

3


Can use subset to specify the subset of the data to be plotted and expand the axis boundaries with the option limits of scale_*. Here’s a simplified example:

ggplot(subset(dados, Mes %in% c("Jan", "Fev")), aes(Mes)) +
  geom_line(aes(y = captura1), group = 1) +
  geom_point(aes(y = captura1), group = 1) +
  scale_x_discrete(limits = levels(dados$Mes)) +
  scale_y_continuous(limits = range(dados$captura1))

inserir a descrição da imagem aqui

Browser other questions tagged

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