简体   繁体   English

使用ggplot facet_grid R图添加图例

[英]R plot using ggplot facet_grid add legend

Using ggplot and faced_grid , I have a problem to visualize the entire name of type1 variable. 使用ggplotfaced_grid ,我无法可视化type1变量的整个名称。 The name is too long. 名称太长。 How I can add a legend for avoid this problem? 如何添加图例以避免此问题?

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)

This is the results: 结果如下:

代码结果

Here is one option where we fill by type1 . 这是我们按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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM