R 用图例绘制两个y
我想用两个y轴在同一个图形中绘制两个变量 我的主要问题是关于审美的部分,以及如何把传说以我所希望的方式呈现出来,并把瓷砖放在轴线上 以下是我的代码示例:R 用图例绘制两个y,r,plot,ggplot2,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), b
# 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)
#英国债务财富不平等图
p1您的代码看起来非常复杂,许多步骤都不是必需的。请使用dput()
包含一些可复制的数据。这使帮助你变得更容易。是的,没错。我会试着复习。但是,部分复杂性是由于ggplot没有一种简单的方法来在两个y轴上绘制变量,而我发现在我的案例中使用的是gtables。ggplots之后的代码是最低要求的,取自stackflow的建议答案。我认为使用sec.axis=
而不是将多个绘图与gtables结合使用可以简化代码;为了一个好的教程。另外,关于简化代码,如果您的问题是关于在y轴上放置标题,我们不需要查看设置标题、自定义字体和其他与您的问题无关的格式的代码。@msh855您的问题仍然缺少数据。请确认。我将尝试使用@Jan Boyer建议的教程和我的数据,看看我是否成功。将发布更新。