使用scale =“ free_y”时如何设置相等的面板水平空间?

时间:2019-04-11 02:36:19

标签: r ggplot2 facet-wrap

set.seed(3)

data <- tibble(Group = c(rep("g1", 10), rep("g2", 10), rep("g3", 10)), 
    Value = c(runif(10, min = 1, max=5), runif(10, min = 1, max=5), runif(10, min = -5, max=5)))

ggplot(data, aes(Group, Value)) + 
    geom_point() + 
    facet_wrap(~ Group, scales = "free")

您可以看到y带有十进制/负值时,空间变大了。

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以设置fixed width for your y-axis labels

ggplot(data, aes(Group, Value)) + 
  geom_point() + 
  facet_wrap(~ Group, scales = "free") +
  scale_y_continuous(labels = function(label) sprintf("%10.1f", label))

或使用coor_flip()

翻转剧情
ggplot(data, aes(Group, Value)) + 
  geom_point() + 
  facet_wrap(Group ~ ., scales = "free") +
  coord_flip()

reprex package(v0.2.1.9000)于2019-04-10创建