Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

How to use scale_x_date properly

$
0
0

I am a new user in R and I hope you can help me.

setwd("C:/Users/USER/Desktop/Jorge")
agua <- read_excel("agua.xlsx")
pbi <- read_excel("PBIagro.xlsx")
str(agua); 
names(agua)[2] <- "Variación";
agua[,1] <- as.Date(agua$Trimestre)

lagpbi <- lag(pbi$PBIAgropecuario, k=1)
pbi[,3]<- lagpbi; pbi <- pbi[-c(1),]; 
names(pbi)[3] <- "PBIlag"

growth <- ((pbi$PBIAgropecuario-pbi$PBIlag)/pbi$PBIlag)*100
Anual_growth <- data.frame(growth); Anual_growth[,2] <- pbi$Año; names(Anual_growth)[2] <- "Año"


# Plot
Agro <- ggplot(Anual_growth, aes(x=Año, y=growth)) +
  geom_line(color="steelblue") + 
  geom_point() +
  geom_text(aes(label = round(Anual_growth$growth, 1)),
            vjust = "inward", hjust = "inward", size=2.5, show.legend = FALSE) +
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) +
  ylim(-9.9,13.4) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.line.x = element_blank(), plot.margin = unit(c(1,1,0.5,1),"cm"),
        axis.line.y = element_blank(), axis.text.x=element_text(face = "bold", size=8, 
        angle=1,hjust=0.95,vjust=0.2),
        axis.text.y = element_blank(), axis.title.y=element_blank())+
        scale_x_continuous("Año", labels = as.character(Anual_growth$Año), breaks = Anual_growth$Año)

print(Agro)

enter image description here

The problem is that it shows all the years, but I only want pair years (in X-axis) or years with step equal to 2. I hope you can really help me. Thank you. Notice that the X-axis variable is a numeric string.


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>