ggplot2 中有一个图例的并排图

Side by Side plot with one legend in ggplot2

我想知道如何使用 ggplot2 创建具有一个常见图例的并排图。我见过一些类似的问题,但不确定如何将其直接应用于我的代码。我已经为图表提供了我的代码,其中包含图例和一些可用于重新创建图表的数据。

Stocks1<-c(2,1,0.8,0.7,0.6)
Bonds1<-c(1,0.8,0.7,0.6,0.5)
Cash1<-1-(Stocks1+Bonds1)
Stocks2<-c(0.6,0.5,0.4,0.3,0.2)
Bonds2<-c(0.3,0.2,0.2,0.15,0.1)
Cash2<-1-(Stocks2+Bonds2)
H<-length(Stocks1) #Change value to represent data
t <- seq(from = 0, to = H, 1) # time grid

这是两张图

pi_F<- data.frame(cash = Cash1,  bonds = Bonds1,   
                  stocks= Stocks1,time=t[-1])
melted_F   <- melt(pi_F, id.vars = 'time')

ggplot(melted_F, aes(x=time, y=value, group = variable)) +
  geom_area(aes(fill=variable)) +
  scale_fill_manual(values=c("#2E318F", "#DFAE41","#109FC6"),
                    name="Asset Type",
                    labels = c("Bank account","Bonds", "Stocks"))+
  scale_x_continuous(name = 'Age',
                     breaks = seq(1,H,1)) + 
  scale_y_continuous(name = 'Asset allocation (in %)',
                     labels=scales::percent,
                     breaks = seq(0,1,0.1),
                     sec.axis = sec_axis(~.*1,breaks =  seq(0,1,0.1),labels=scales::percent)) + 
  coord_cartesian(xlim = c(1,H), ylim = c(0,1), expand = TRUE) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

pi_F<- data.frame(cash = Cash2,  bonds = Bonds2,   
                  stocks= Stocks2,time=t[-1])
melted_F   <- melt(pi_F, id.vars = 'time')

ggplot(melted_F, aes(x=time, y=value, group = variable)) +
  geom_area(aes(fill=variable)) +
  scale_fill_manual(values=c("#2E318F", "#DFAE41","#109FC6"),
                    name="Asset Type",
                    labels = c("Bank account","Bonds", "Stocks"))+
  scale_x_continuous(name = 'Age',
                     breaks = seq(1,H,1)) + 
  scale_y_continuous(name = 'Asset allocation (in %)',
                     labels=scales::percent,
                     breaks = seq(0,1,0.1),
                     sec.axis = sec_axis(~.*1,breaks =  seq(0,1,0.1),labels=scales::percent)) + 
  coord_cartesian(xlim = c(1,H), ylim = c(0,1), expand = TRUE) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

理想情况下,我希望将这些与图例并排放置在适当的位置,可能在两张图的右侧。在此先感谢您的帮助!

将您的数据放在一起并使用构面:

## calling the first data `melted_F` and the second `melted_F2`
## put them in one data frame with a column named "data" to tell
## which is which
melted = dplyr::bind_rows(list(data1 = melted_F, data2 = melted_F2), .id = "data")

## exact same plot code until the last line
ggplot(melted, aes(x=time, y=value, group = variable)) +
  geom_area(aes(fill=variable)) +
  scale_fill_manual(values=c("#2E318F", "#DFAE41","#109FC6"),
                    name="Asset Type",
                    labels = c("Bank account","Bonds", "Stocks"))+
  scale_x_continuous(name = 'Age',
                     breaks = seq(1,H,1)) + 
  scale_y_continuous(name = 'Asset allocation (in %)',
                     labels=scales::percent,
                     breaks = seq(0,1,0.1),
                     sec.axis = sec_axis(~.*1,breaks =  seq(0,1,0.1),labels=scales::percent)) + 
  coord_cartesian(xlim = c(1,H), ylim = c(0,1), expand = TRUE) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  ##  facet by the column that identifies the data source
  facet_wrap(~ data)