使用facet_wrap函数将3个图绘制成同一个图形

使用facet_wrap函数将3个图绘制成同一个图形,r,ggplot2,R,Ggplot2,有人能给我解释一下如何使用ggplot中的facet\u wrap层在同一个图形中绘制3个图,这样每个图都应该排成一行,并且每个图的比例可以自由变化。 以下是我制作的3张图表,因为我想看看这三个协变量之间的关系:收入中值,pct\u移民,收入不平等,以及Marine Le Pen的得票率。 准确地说,这三个协变量不是变量,这就是为什么我必须首先过滤数据帧选举\u 2017\u long\u metrop\u协变量\u lepen\u long,以便我只能保留变量协变量中的每个观察值 如果您有任何

有人能给我解释一下如何使用
ggplot
中的
facet\u wrap
层在同一个图形中绘制3个图,这样每个图都应该排成一行,并且每个图的比例可以自由变化。

以下是我制作的3张图表,因为我想看看这三个协变量之间的关系:
收入中值
pct\u移民
收入不平等
,以及Marine Le Pen的得票率。 准确地说,这三个协变量不是变量,这就是为什么我必须首先过滤数据帧
选举\u 2017\u long\u metrop\u协变量\u lepen\u long
,以便我只能保留变量
协变量中的每个观察值

如果您有任何改进图形可视化的建议

graph1 = filter(elections_2017_long_metrop_covariates_lepen_long, covariates == "pct_immigrant")       
ggplot(graph1,aes(x = value,y = pct_votes)) + geom_point(size = 3, alpha = 0.5,colour = "#d90502") + expand_limits(x = 0, y = 0:100) +labs(x = "share of immigrants",y = "percentage of votes for Marine Le Pen")


您没有给出一个完整的可复制示例,但我认为这应该适合您

要创建facet,您应该将filter变量传递到
facet\u wrap()
函数中

elections_2017_long_metrop_covariates_lepen_long %>% 
  filter(covariates %in% c('pct_immigrant', 'median_income', 'income_inequality')) %>% 
  ggplot(aes(x = value,y = pct_votes)) +
  geom_point(alpha = 0.5) +
  facet_wrap(~covariates)

考虑颜色的完整解决方案可以是:

library(ggplot2)
#Plot
ggplot(subset(elections_2017_long_metrop_covariates_lepen_long
              covariates %in% c('pct_immigrant', 'median_income', 'income_inequality')),
       aes(x = value,y = pct_votes,color=covariates))+
  geom_point(size = 3, alpha = 0.5)+
  expand_limits(x = 0, y = 0:100) +
  labs(x = "share of immigrants",y = "percentage of votes for Marine Le Pen")+
  facet_wrap(.~covariates,scales = 'free',ncol = 1)+
  scale_color_manual(values=rep("#d90502",3))
或:

由于缺乏数据,没有产出

elections_2017_long_metrop_covariates_lepen_long %>% 
  filter(covariates %in% c('pct_immigrant', 'median_income', 'income_inequality')) %>% 
  ggplot(aes(x = value,y = pct_votes)) +
  geom_point(alpha = 0.5) +
  facet_wrap(~covariates)
library(ggplot2)
#Plot
ggplot(subset(elections_2017_long_metrop_covariates_lepen_long
              covariates %in% c('pct_immigrant', 'median_income', 'income_inequality')),
       aes(x = value,y = pct_votes,color=covariates))+
  geom_point(size = 3, alpha = 0.5)+
  expand_limits(x = 0, y = 0:100) +
  labs(x = "share of immigrants",y = "percentage of votes for Marine Le Pen")+
  facet_wrap(.~covariates,scales = 'free',ncol = 1)+
  scale_color_manual(values=rep("#d90502",3))
library(dplyr)
library(ggplot2)
#Code 2
elections_2017_long_metrop_covariates_lepen_long %>%
  filter(covariates %in% c('pct_immigrant', 'median_income', 'income_inequality')) %>%
  ggplot(aes(x = value,y = pct_votes,color=covariates))+
  geom_point(size = 3, alpha = 0.5)+
  expand_limits(x = 0, y = 0:100) +
  labs(x = "share of immigrants",y = "percentage of votes for Marine Le Pen")+
  facet_wrap(.~covariates,scales = 'free',ncol = 1)+
  scale_color_manual(values=rep("#d90502",3))