我想以某种方式结合两个地块,他们有一个共同的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"))
这给出了如下情节:
还我已经寻找其他问题,并尝试了以下: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
我认为这可能是由于小面
任何帮助将不胜感激。此外,我宁愿用两个数据帧,而不是如果可能的话结合起来。
这是你想要的吗?在这里,我用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创建于2019年2月7日(v0.2.1.9000)