箱线图:如何将x轴转换为连续值尺度?

问题描述 投票:0回答:1

如何将 x 轴转换为连续值。在建议的示例中,我希望箱线图位于 0 处,远离与 2.5 和 3 相关的箱线图。我不想有 3 个等距箱线图。

我预先感谢您的帮助。

library(ggplot2)

set.seed(123)
data <- data.frame(
  Group = rep(c("0", "2.5", "3"), each = 50),
  Value = c(rnorm(50), rnorm(50, mean = 1), rnorm(50, mean = 2))
)
ggplot(data, aes(x = Group, y = Value, group = Group)) + 
  geom_boxplot(fill = "lightblue", orientation = "x") + 
  scale_x_discrete(limits = as.factor(c("0", "2.5", "3"))) + 
  labs(x = "numbers", y = "Value") +
  theme(axis.text.x = element_text(angle = 0, hjust = .5))
r ggplot2 boxplot
1个回答
0
投票

也许可以省略

Group
变量的引号,并将
scale_x_discrete()
替换为
scale_x_continuous()

set.seed(123)
data <- data.frame(
  Group = rep(c(0, 2.5, 3), each = 50),
  Value = c(rnorm(50), rnorm(50, mean = 1), rnorm(50, mean = 2))
)

ggplot(data, aes(x = Group, y = Value, group = Group)) + 
  geom_boxplot(fill = "lightblue", orientation = "x") + 
  scale_x_continuous(breaks = c(0, 2.5, 3)) + 
  labs(x = "numbers", y = "Value") +
  theme(axis.text.x = element_text(angle = 0, hjust = .5))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.