如何旋转完整的 facet_wrap ggplot?
How can I rotate a complete facet_wrap ggplot?
我将 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")
很好的例子。这是这个
的一个版本
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
变量,以便它们按顺序出现。
我将 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")
很好的例子。这是这个
的一个版本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
变量,以便它们按顺序出现。