用传说绘制两个y

时间:2018-04-23 08:25:52

标签: r plot ggplot2

我想使用两个y轴在同一图表中绘制两个变量。

我的主要问题在于审美部分以及如何以我想要的方式传递图例并在轴上拼贴

以下是我的代码:

# plots for uk debt wealth inequality 
p1<-ggplot(uk, aes(x = year))+
  scale_y_continuous(expand = c(0, 0), limits = c(20,250))+
  scale_x_continuous(expand = c(0, 0), breaks= c(1950, seq(1950,2010,10)))+
  geom_line(aes(y = debt_sm, colour="Debt"), size=1.2, na.rm = TRUE)+
  labs( y="Public Debt   [% GDP]",x = "year", title = "Public Debt and Wealth Inequality", subtitle = "United Kingdom 1950-2009")+
  scale_colour_manual("", 
                      breaks = c("Debt"),
                      values = c("black")) +
  guides(color=guide_legend(override.aes=list(fill=NA)))+
  theme_set(theme_gray() + theme(legend.key=element_blank())) +
  theme_set(theme_bw() + theme(legend.key=element_blank())) +
  theme(text =element_text(family="Bell MT"),
        #panel.background = element_blank(), # backgrounf theme 
        plot.title    = element_text(colour = "navy", size = 10), # title    setting
        plot.subtitle = element_text(colour = "firebrick4"),   # subtitle setting 
        plot.caption  =element_text(size = 6, hjust=0), # hjust 1(right), 0.5 (center), 0(left)
        legend.position = c(0.9, 0.8)) 



p2<-ggplot(uk, aes(x = year))+
  scale_y_continuous(expand = c(0, 0), sec.axis = sec_axis(~.,name = "Top 10 Wealth Shares [%]"))+
  scale_x_continuous(expand = c(0, 0), breaks= c(1950, seq(1950,2010,10)))+
  geom_line(aes(y = Top10_sm*100, colour="Top Shares"), size=1.2, na.rm = TRUE)+
  labs( y="Top 10% Shares",x = "year", title = "Public Debt and Wealth Inequality", subtitle = "United Kingdom 1950-2009")+
  scale_colour_manual("", 
                      breaks = c("Top Shares"),
                      values = c("navy")) +
  theme_set(theme_gray() + theme(legend.key=element_blank())) +
  theme_set(theme_bw() + theme(legend.key=element_blank())) +
  theme(text =element_text(family="Bell MT"),
        panel.background = element_blank(), # backgrounf theme 
        plot.title    = element_text(colour = "navy", size = 8), # title    setting
        plot.subtitle = element_text(colour = "firebrick4"),   # subtitle setting 
        plot.caption  =element_text(size = 6, hjust=0),
        legend.position = c(0.9, 0.9),
        legend.key = element_blank()) # hjust 1(right), 0.5 (center), 0(left)


# make gtable objects from ggplot objects
# gtable object shows how grobs are put together to form a ggplot
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# get the location of the panel of p1 
# so that the panel of p2 is positioned correctly on top of it
pp <- c(subset(g1$layout, name == "panel", se = t:r))

# superimpose p2 (the panel) on p1
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# extract the y-axis of p2
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]

# flip it horizontally
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)

# add the flipped y-axis to the right
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)


grid.draw(g)

使用这个我得到的图片就是这个

enter image description here

两个范围:

  1. 在右侧y轴上添加标题
  2. 从图例&#34; Debt&#34;中删除框。适合图表的背景
  3. 将另一个反映第二个变量的图例反映在我已有的图例上
  4. 有人可以帮我吗?

0 个答案:

没有答案