使用ggplot facet_grid R图添加图例

时间:2018-10-01 09:26:37

标签: r ggplot2 legend

使用ggplotfaced_grid,我在可视化type1变量的整个名称时遇到了问题。名称太长。如何添加图例以避免此问题?

DF <- data.frame("value" =  runif(50, 0, 1),
                 "type1" = c(rep("AAAAAAAAAAAAAAAAAAAAAA", 25), 
                             rep("BBBBBBBBBBBBBBBBB", 25)),
                 "type2" = rep(c("c", "d"), 25), 
                 "number" = rep(2:6, 10))

ggplot(DF, aes(y = value, x = type1)) + 
  geom_boxplot(alpha = .3) + 
  ggtitle("TITLE") + 
  facet_grid(type2 ~ number)

这是结果:

CODE RESULT

1 个答案:

答案 0 :(得分:3)

这是我们用type1填充的一种选择。

ggplot(DF, aes(y=value, x=type1)) + 
  geom_boxplot(alpha=.3, aes(fill = type1)) + 
  ggtitle("TITLE") + facet_grid(type2 ~ number) +
  scale_x_discrete(name = NULL, breaks = NULL) + # these lines are optional
  theme(legend.position = "bottom")

enter image description here

相关问题