我将facet_wrap应用于条形图,其中组是垂直轮廓的不同层/部分。将它们绘制在彼此的顶部而不是彼此并排是有意义的。换句话说,我想要一个facet_wrap条形图,其中各个框旋转90°并垂直地彼此绘制。
我尝试使用coord_flip,但是,它只会翻转构面中的坐标,而不是整个盒子中的坐标。
dat <- as.data.frame(
cbind(
c("Layer 1", "Layer 1", "Layer 1", "Layer 2", "Layer 2", "Layer 2", "Layer 3", "Layer 3", "Layer 3"),
c("group 1", "group 2", "group 3", "group 1", "group 2", "group 3", "group 1", "group 2", "group 3"),
c(2, 3, 6, 3, 4, 5, 4, 2, 3)
)
)
names(dat) <- c("ID", "Group", "Value")
ggplot(data = dat) +
geom_bar(mapping = aes(x= ID, y= Value, fill = "red", group = Group), stat="identity", position = "dodge") +
labs(title= "", x= "", y = "Value") +
theme(legend.position="bottom") +
facet_wrap(~ ID,scales="free")
答案 0 :(得分:0)
很好的例子。这是这个版本
dat %>%
mutate(Group = factor(Group, levels = rev(levels(Group)))) %>%
ggplot() +
geom_col(
mapping = aes(
x = Group,
y = Value,
fill = "red",
group = Group
),
stat = "identity",
position = "dodge"
) +
labs(title = "", x = "", y = "Value") +
theme(legend.position = "bottom") +
facet_grid(ID ~ ., scales = "free_y") +
coord_flip()
geom_col
的条形图可以在其中指定两个轴。
我对Group
变量重新排序,以便它们按顺序显示。