gpt4 book ai didi

R 绘图使用 ggplot facet_grid 添加图例

转载 作者:行者123 更新时间:2023-12-05 03:04:56 25 4
gpt4 key购买 nike

使用 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

最佳答案

这是一个选项,我们用 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

关于R 绘图使用 ggplot facet_grid 添加图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52588071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com