Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用“时的文本对齐方式”;表达方式;在ggplot2轴文本中生成特殊字符的步骤_R_Ggplot2 - Fatal编程技术网

R 使用“时的文本对齐方式”;表达方式;在ggplot2轴文本中生成特殊字符的步骤

R 使用“时的文本对齐方式”;表达方式;在ggplot2轴文本中生成特殊字符的步骤,r,ggplot2,R,Ggplot2,我正在使用ggplot2绘制患者BMI的箱线图,但x轴上的文本有问题。我想包括体重指数(kg/m2)的单位,我想在“2”上加上上标。当我这样制作图表时: require(plyr) require(ggplot2) Weights <- data.frame(SubjectID = 1:500, Weight = rnorm(500, 28, 7)) Weights$Cat <- cut(Weights$Weight, breaks =

我正在使用ggplot2绘制患者BMI的箱线图,但x轴上的文本有问题。我想包括体重指数(kg/m2)的单位,我想在“2”上加上上标。当我这样制作图表时:

require(plyr)
require(ggplot2)

Weights <- data.frame(SubjectID = 1:500,
                      Weight = rnorm(500, 28, 7))
Weights$Cat <- cut(Weights$Weight, breaks = c(0, 18.5, 25, 30, 40, Inf),
                   right = FALSE)
Weights$BMIcat <- revalue(Weights$Cat, 
                           c("[0,18.5)" = "Underweight\n(<18.5 kg/m2)",
                             "[18.5,25)" = "Normal weight\n(18.5 to 24.9\nkg/m2)",
                             "[25,30)" = "Overweight\n(25 to 29.9\nkg/m2)",
                             "[30,40)" = "Obese\n(30 to 39.9\nkg/m2)",
                             "[40,Inf)" = "Severely obese\n(>40 kg/m2)"))

ggplot(Weights, aes(x = BMIcat, y = Weight)) +
      geom_boxplot() +
      xlab("BMI category") + ylab("Weight (kg)")


现在,我的2是上标的,但其他一切看起来都很糟糕。看起来它突然向左或可能完全对齐,而不是居中,2有时确实远离m,轴文本与图形重叠!有什么建议吗?

您可以使用

Weights$BMIcat
ggplot(Weights, aes(x = Cat, y = Weight)) +
      geom_boxplot() +
      scale_x_discrete(breaks=c("[0,18.5)", "[18.5,25)", "[25,30)",
                                "[30,40)", "[40,Inf)"),
                       labels=c(
                             expression("Underweight\n(<18.5 kg/m"^2*")"),
                             expression("Normal weight\n(18.5 to 24.9\nkg/m"^2*")"),
                             expression("Overweight\n(25 to 29.9\nkg/m"^2*")"),
                             expression("Obese\n(30 to 39.9\nkg/m"^2*")"),
                             expression("Severely obese\n(>40 kg/m"^2*")"))) +
      xlab("BMI category") + ylab("Weight (kg)")
Weights$BMIcat <- revalue(Weights$Cat, 
                           c("[0,18.5)" = "Underweight\n(<18.5 kg/m²)",
                             "[18.5,25)" = "Normal weight\n(18.5 to 24.9\nkg/m²)",
                             "[25,30)" = "Overweight\n(25 to 29.9\nkg/m²)",
                             "[30,40)" = "Obese\n(30 to 39.9\nkg/m²)",
                             "[40,Inf)" = "Severely obese\n(>40 kg/m²)"))
ggplot(Weights, aes(x = BMIcat, y = Weight)) +
      geom_boxplot() +
      xlab("BMI category") + ylab("Weight (kg)")