使用facet_grid函数绘制带有ggplot2的条形图的问题

时间:2017-04-18 09:35:08

标签: r ggplot2 facet

关于绘制条形图的代码在这里

library(ggplot2)

fig_num <- ggplot(data=branch.fig, aes(y=branch.fig$num, x=branch.fig$film.type, fill = branch.fig$film.type)) + 
  geom_bar(position=position_dodge(), stat="identity", width = 0.3, colour = "gray35") +
  scale_fill_manual(values = colors) +  
  facet_grid(branch.type~.) +
  geom_errorbar(aes(ymin=branch.fig$num, ymax=branch.fig$num +branch.fig$SE), position="dodge", width=0.1, col = "gray40") + 
  theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
  labs(x = "Film Type",y = "Branch Number") + 
  ggtitle("DAS = 54") + coord_flip()
fig_num

我想要的结果应该是这样的,

enter image description here

但我得到的是这样的,R结合了部分数据,我不知道为什么会出现这个问题,这对我来说真的是一个很大的问题,真诚地寻找你的帮助,非常感谢!< / p>

enter image description here

以下是数据集

dput(subset(branch.fig))
structure(list(film.type = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("black", "ck", "dark-gray", 
"green", "red", "white"), class = "factor"), branch.type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", 
"V"), class = "factor"), num = c(14.6666666666667, 3.33333333333333, 
14, 2.66666666666667, 15.3333333333333, 2, 14.6666666666667, 
2.66666666666667, 16.6666666666667, 3.33333333333333, 20.3333333333333, 
1.66666666666667), SE = c(0.333333333333333, 0.333333333333333, 
1.15470053837925, 0.333333333333333, 0.881917103688197, 0, 0.881917103688197, 
0.666666666666667, 0.333333333333333, 0.666666666666667, 2.33333333333333, 
0.666666666666667), n = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), .Names = c("film.type", "branch.type", "num", "SE", 
"n"), row.names = c(NA, -12L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = list(film.type), drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L,
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
 film.type = structure(1:6, .Label = c("black", "ck", "dark-gray", 
   "green", "red", "white"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame", vars = list(film.type), drop = TRUE, .Names = 
"film.type"))

1 个答案:

答案 0 :(得分:1)

您的代码存在许多问题 1.您的dput生成了错误。以下命令生成数据 2.数据框以data=branch.fig命名。无需再将其命名为aes()的一部分 3.此图表中没有躲避,因此请dodgegeom_bar同时删除geom_errorbar命令。
4.据我所知,没有colors个对象。我为手动色标添加了一些颜色。

# Data
branch.fig = structure(list(film.type = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("black", "ck", "dark-gray", 
"green", "red", "white"), class = "factor"), branch.type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", 
"V"), class = "factor"), num = c(14.6666666666667, 3.33333333333333, 
14, 2.66666666666667, 15.3333333333333, 2, 14.6666666666667, 
2.66666666666667, 16.6666666666667, 3.33333333333333, 20.3333333333333, 
1.66666666666667), SE = c(0.333333333333333, 0.333333333333333, 
1.15470053837925, 0.333333333333333, 0.881917103688197, 0, 0.881917103688197, 
0.666666666666667, 0.333333333333333, 0.666666666666667, 2.33333333333333, 
0.666666666666667), n = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), .Names = c("film.type", "branch.type", "num", "SE", 
"n"), row.names = c(NA, -12L), class = "data.frame")


library(ggplot2)

# Plot
fig_num = ggplot(data = branch.fig, aes(y = num, x = film.type, fill = film.type)) + 
  geom_bar(stat = "identity", width = 0.3, colour = "gray35") +
  scale_fill_manual(values = c("black", "orange", "grey", "green", "red", "white")) +  
  facet_grid(branch.type ~ .) +
  geom_errorbar(aes(ymin = num, ymax = num + SE), width = 0.1, col = "gray40") + 
  theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
  labs(x = "Film Type", y = "Branch Number") + 
  ggtitle("DAS = 54") + coord_flip()

fig_num