4
\$\begingroup\$

So far I was successful to get what I need using below mentioned script, but I'm sure my script is far from being perfect (if it is possible at all). So, I'd greatly appreciate your comments on how would it have been done differently in order to improve it.

p6_ch1 <- read.xlsx("BEO_charts.xlsx", sheet = 7, startRow = 2, colNames = T, rowNames = F, detectDates = T, skipEmptyCols = T, skipEmptyRows = T) 

p6_ch1$date <- as.Date(seq(as.Date("2009/1/1"), as.Date("2016/07/1")-1, by = "quarter"))
p6_ch1$date <- as.yearqtr(as.Date(p6_ch1$date, format = "Q%q %Y"))

p6_ch1_stack <- subset.data.frame (p6_ch1, select = c(1,3:5,7:9), p6_ch1$date >= "2013 Q1")
p6_ch1_line <- subset(p6_ch1, select = c(1,10), p6_ch1$date >= "2013 Q1")
p6_ch1_stack_melt <- melt(p6_ch1_stack, id.vars = "date", measure.vars = c(2:7)) 
p6_ch1_line_melt <- melt(p6_ch1_line, id.vars = "date")


p6.1 <- ggplot(data = p6_ch1_stack_melt, aes (x = date, y = value)) +
scale_x_yearqtr(breaks = seq(from = min(p6_ch1_stack$date, na.rm = T), to = max(p6_ch1_stack$date, na.rm = T), by=0.25), format="Q%q %Y", expand=c(.01,0)) +
  scale_y_continuous(limits = c(-35,35), breaks = seq(-35,35,5)) + 
  xlab ("") +
  ylab ("") +

  geom_bar(data = p6_ch1_stack_melt, 
        aes(
          group = variable,
          fill=variable),
          stat = 'identity') +

  scale_fill_manual(
    guide = "legend", 
    name = NULL, 
    breaks= c(
      "tax_inc",
      "ved_inc",
      "oth_inc",
      "current_exp",
      "debt_service",
      "capex"), 
    labels= c(
      "Налоговые доходы*", 
      "Доходы от ВЭД", 
      "Прочие доходы", 
      "Текущие расходы**", 
      "Обслуживание госдолга", 
      "Капитальные расходы"),
    values = c(
      "#88CCEE", 
      "#44AA99", 
      "#117733", 
      "#DDCC77", 
      "#CC6677", 
      "#AA4499"))

p6.1 <- p6.1 + geom_hline(aes(yintercept = 0),colour="#ABABAB")
p6.1 <- p6.1 + 
  geom_line(data = p6_ch1_line_melt, aes(x = date, y = value, colour = variable), size = 1) + 
  geom_point(data = p6_ch1_line_melt, aes(x = date, y = value, colour = variable), size = 1) +

  scale_colour_manual(
    guide = "legend", 
    name = NULL, 
    breaks = c("balance"), 
    labels = c("Сальдо"), 
    values = c("#332288", "#332288")) +

    theme(axis.text.x = element_text(angle = 90, vjust = .5, size = 5, colour = "black"),
        axis.text.y = element_text(size = 5, colour = "black"),
        #panel.grid.minor.x = element_line(colour = "#EDEDED", linetype = 2),
        panel.grid.major.y = element_line(colour = "#EDEDED", linetype = 2, size = .3),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_line(colour = "#EDEDED"),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line.y = element_line(colour="#ABABAB"), 
        axis.ticks.length = unit(0,"cm"),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.spacing.x = unit(-.2,"cm"),
        legend.key = element_blank(),
        legend.key.height = unit(.3, "cm"),
        legend.text = element_text(size = 5),
        legend.box.margin = unit (c(-9,1,1,1), "mm"),
        legend.background = element_rect(fill = "transparent", colour = NA),
        plot.margin = unit (c(1,1,0,-4), "mm"))  

p6.1

CairoPDF("plot6.1.pdf", width=5.23622, height=2.83465)
plot(p6.1)
dev.off()

The script gives me a chart I need:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.