如何使折线图的数据点准确地放置在具有2个Y轴的同一张图表的堆栈图的中间?

时间:2018-12-26 13:01:10

标签: r gtable

一段时间以来,我一直在努力解决以下问题。尚未找到任何解决方案。 我正在建立许多带有2个y轴的图表。通常,一个数据行以条形图表示,而多个数据行是线。我为此使用了“ tidyverse”和“ gtable”包。从技术上讲,我得到了所需的东西,只是将图表合并在一起时放置不正确。也就是说,折线图的数据点位于相应条形的左侧或右侧,而不是其中心。结果,每个这样的情节看起来都是令人误解的。以下是我所得到的例子。

enter image description here

这是构建图表的代码:

Chart_24_col <- subset.data.frame (Chart_24, select = c("date","HTP_lossmaking")) %>% 
  melt(id.vars = "date") %>% 
  ggplot(aes (x = date, y = value)) +
  scale_y_continuous(breaks = seq(0,15,3), expand = c(0,0), limits = c(0,15))+
  scale_x_continuous(breaks = seq(2009,2016,1))+
  labs(x=NULL, y=NULL)+

  geom_bar(stat="identity", size = 1)+
  geom_bar( 
        aes(
          group = variable,
          fill=variable),
          stat = 'identity') +

  scale_fill_manual(
    guide = "legend", 
    name = NULL, 
    breaks= "HTP_lossmaking", 
    labels= "Количество убыточных резидентов (левая ось)",
    values = "#4477AA")+

  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.major.y = element_line(colour = "#EDEDED", linetype = 2, size = .2),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_line(colour = "#EDEDED", size = .2),
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = "transparent", colour = NA),
        axis.line.y = element_line(colour=NA), 
        axis.line.x=element_line(colour="#ABABAB"),
        axis.ticks.length = unit(0,"cm"),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.title = element_blank(),
        legend.spacing.x = unit(-.2,"cm"),
        legend.key = element_blank(),
        legend.key.height = unit(.5, "cm"),
        legend.text = element_text(size = 5),
        legend.box.margin = unit (c(-3,1,1,1), "mm"),
        plot.margin = unit (c(1,0,3,0), "mm"))

Chart_24_col


Chart_24_line <- subset.data.frame (Chart_24, 
  select = c(
  "date", 
  "HTP_selfmanufacturing",
  "HTP_revenue", 
  "HTP_NI")) %>% 
  melt(
    id.vars = "date") %>%
  ggplot(aes (
    x = date, 
    y= value, 
    colour = variable))+
  geom_path(size = 1)+ 
  scale_colour_manual(
    guide = "legend", 
    breaks = c(
      "HTP_selfmanufacturing",
      "HTP_revenue",
      "HTP_NI"),
    label = c(
      "Отгруженная продукция собственного производства, млрд.рублей 2006 года (правая ось)",
      "Выручка, млрд.рублей 2006 года (правая ось)",
      "Чистая прибыль, млрд.рублей 2006 года (правая ось)"),
    values = c(
      "#DDCC77",
      "#CC6677",
      "#117733"))+
  labs(x=NULL, y=NULL)+
  scale_x_continuous(breaks = seq(2009,2016,1)) +
  scale_y_continuous(breaks = seq(0,2200,100), expand = c(0,0), limits = c(0,2200)) +       
  theme(axis.text.x = element_text(angle = 90, vjust = .5, size = 5, colour = "black"),
        axis.text.y = element_text(size = 5, colour = "black"),
        panel.background = element_rect(fill = NA),
        panel.grid = element_blank(),
        axis.line.y = element_line(colour=NA), 
        axis.ticks.length = unit(0,"cm"),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.title = element_blank(),
        legend.spacing.x = unit(-.2,"cm"),
        legend.key = element_blank(),
        legend.key.height = unit(.5, "cm"),
        legend.text = element_text(size = 5),
        legend.box.margin = unit (c(-3,1,1,1), "mm"),
        plot.margin = unit (c(1,0,3,0), "mm"))

Chart_24_line


g1 <- ggplot_gtable(ggplot_build(Chart_24_col))
g2 <- ggplot_gtable(ggplot_build(Chart_24_line))

gpp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t,
                     pp$l, pp$b, pp$l)
# axis tweaks
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
#ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]

leg = gtable:::cbind_gtable(leg1, leg2, "first")            
leg$widths[5:6] = unit(0, "cm")

g$grobs[[which(g$layout$name == "guide-box")]] <- 
                                  gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g)

CairoPDF("Pic24.pdf", width=3, height=3)
plot(g)

我需要将点放置在每个小节的中间,右边是整个日期。

感谢您的帮助。

0 个答案:

没有答案