合并共享x轴但不共享y的图

时间:2019-02-07 22:59:28

标签: r ggplot2

我想以某种方式组合两个图,它们具有一个公共的x轴,但一个是分类数据的多面条形图。另一个是连续数据,没有多面性,但与第一个图的两个方面都相关。

我有以下虚拟数据和代码:

farm<-  c(22,   33, 22, 33, 22, 33, 22, 33,  
22, 33, 22, 33, 22, 33, 22, 33, 22, 33,  
22, 33)
year<-  c(2010, 2010,   2011,   2011,   2012,   2012,   2013,   2013,    
2014,   2014,   2010,   2010,   2011,   2011,   2012,   2012,   2013,    
2013,   2014,   2014)
exp<-   c('a',  'a',    'a',    'a',    'a',    'a',    'a',    'a',     
'a',    'a',    'b',    'b',    'b',    'b', 'b',   'b',    'b',     
'b',    'b',    'b')
variable1<- c(3,    1,  3,  1,  2,  0,  2,   
1,  3,  0,  1,  1,  1,  0,  2,  0,  1,   
0,  0,  0)
variable2<- c(300,  100,    400,    123,    500,    100,    600,    100,     
700,    100,    700,    100,    600,    100,    700,    100,    600,     
100,    300,    100)
dwt<-data.frame(farm, year, exp, variable1)
dwt2<-data.frame(farm, year, variable2)
dwt$farm<- as.character(dwt$farm)
dwt %>%
mutate(as.character(farm))%>%
mutate(as.character(year))%>%
mutate(as.character(variable1))%>%
ggplot(aes(x=farm, fill = variable1)) +
geom_bar(stat = 'count') + facet_grid(exp~year) + 
guides(fill=guide_legend(title="Level")) +
coord_cartesian(ylim=c(0, 5))
dwt2$farm<- as.character(dwt2$farm)
dwt2 %>%
mutate(as.character(farm))%>%
mutate(as.character(year))%>%
ggplot(aes(x=farm, y = variable2)) +
geom_bar(stat = 'identity') + facet_grid(~year) + 
guides(fill=guide_legend(title="Level"))

这给出了以下图:

enter image description here

enter image description here

我还搜索了其他问题,并尝试了以下方法:      ggplot()+ geom_bar(data=dwt, aes(x=farm, fill=variable1))+ facet_grid(exp~year) + geom_bar(data = dwt2, aes(x=farm, y=variable2))+ facet_grid(~year)  但出现以下错误:美学的长度必须为1或与数据(20)相同:x,y

我认为这可能是由于刻面

任何帮助将不胜感激。另外,如果可能的话,我宁愿使用两个数据帧,也不愿合并。

1 个答案:

答案 0 :(得分:2)

这是您想要的吗?在这里,我使用了patchwork包,但是您可以使用several others too

library(tidyverse)
theme_set(theme_bw(base_size = 14))

farm <- c(
  22, 33, 22, 33, 22, 33, 22, 33,
  22, 33, 22, 33, 22, 33, 22, 33, 22, 33,
  22, 33
)
year <- c(
  2010, 2010, 2011, 2011, 2012, 2012, 2013, 2013,
  2014, 2014, 2010, 2010, 2011, 2011, 2012, 2012, 2013,
  2013, 2014, 2014
)
exp <- c(
  "a", "a", "a", "a", "a", "a", "a", "a",
  "a", "a", "b", "b", "b", "b", "b", "b", "b",
  "b", "b", "b"
)
variable1 <- c(
  3, 1, 3, 1, 2, 0, 2,
  1, 3, 0, 1, 1, 1, 0, 2, 0, 1,
  0, 0, 0
)
variable2 <- c(
  300, 100, 400, 123, 500, 100, 600, 100,
  700, 100, 700, 100, 600, 100, 700, 100, 600,
  100, 300, 100
)

dwt <- data.frame(farm, year, exp, variable1)
dwt2 <- data.frame(farm, year, variable2)
dwt$farm <- as.character(dwt$farm)
dwt2$farm <- as.character(dwt2$farm)

p1 <- dwt %>%
  mutate(as.character(farm)) %>%
  mutate(as.character(year)) %>%
  mutate(as.character(variable1)) %>%
  ggplot(aes(x = farm, fill = variable1)) +
  geom_bar(stat = "count") + facet_grid(exp ~ year) +
  guides(fill = guide_legend(title = "Level")) +
  coord_cartesian(ylim = c(0, 5)) +
  ### remove x-axis label and reduce bottom margin
  theme(
    axis.text.x = element_blank(),
    axis.title.x = element_blank(),
    axis.ticks.x = element_blank()
  ) +
  theme(plot.margin = margin(b = 2, unit = "pt"))


p2 <- dwt2 %>%
  mutate(as.character(farm)) %>%
  mutate(as.character(year)) %>%
  ggplot(aes(x = farm, y = variable2)) +
  geom_bar(stat = "identity") + facet_grid(~year) +
  guides(fill = guide_legend(title = "Level")) +
  ### remove strip and reduce top margin
  theme(strip.text = element_blank()) +
  theme(plot.margin = margin(t = 2, unit = "pt"))


library(patchwork)
p1 / p2

reprex package(v0.2.1.9000)于2019-02-07创建