簡體   English   中英

使用ggplot facet_grid R圖添加圖例

[英]R plot using ggplot facet_grid add 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)

結果如下:

代碼結果

這是我們按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