如何更改 R 中箱线图的 x 轴组标签

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

R 给我的箱线图在 X 轴上没有标记为“城市”、“郊区”和“农村”的 3 个组,而且我对 R 中的箱线图不够熟悉,无法做到这一点。有人可以帮忙吗?所有代码附在下面,供任何人复制粘贴。对于“农村”向量,数字 1 表示其城市县,2 表示郊区,3 表示农村。

此外,有没有办法改变箱线图中方框的颜色,使其彼此不同?

Rurality<- c(1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         2.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0,
         3.0)

    CR<- c(895.3,
           692.5,
           834.6,
           571.0,
           618.8,
           727.9,
           286.8,
           391.5,
           440.9,
           442.5,
           343.3,
           545.3,
           205.9,
           207.5,
           259.7,
           495.2,
           689.8,
           502.2,
           434.9,
           325.6,
           185.3,
           399.4,
           218.7,
           274.5,
           461.4,
           367.5,
           337.0,
           308.5,
           334.2,
           309.2,
           236.6,
           278.3,
           349.0,
           425.3,
           225.7,
           369.1,
           297.7,
           277.7,
           425.7,
           344.3,
           287.5,
           190.0,
           182.6,
           302.8,
           262.8,
           290.0,
           339.5,
           204.0,
           333.4,
           266.9,
           276.4,
           210.4,
           299.7,
           247.1,
           237.4,
           217.0,
           247.4,
           234.2,
           305.2,
           170.0,
           339.5,
           237.7,
           215.0,
           166.4,
           283.4,
           173.0,
           185.1,
           217.2,
           237.0,
           178.6,
           280.3,
           272.4,
           133.8,
           245.6,
           367.3,
           246.3,
           338.5,
           187.7,
           167.2,
           296.4,
           262.4,
           259.3,
           153.6,
           189.7,
           241.9,
           88.7,
           153.8,
           175.8)
    
    
    data = data.frame(Rurality, CR)
    
    data$Factor_Rurality <- as.factor(data$Rurality)
    
    ggplot(data = data, aes(y = CR, x = Factor_Rurality))+
      geom_boxplot(width = 0.65)+
      stat_boxplot(geom = "errorbar", width = 0.2)+
      geom_point()+
      labs(y = "Chlamydia Rate",
           title = "Chlamydia Rate in Urban, Suburban, and Rural Ohio Counties",
           x= "County Types")+
      theme(plot.title = element_text(hjust = 0.5))+
      theme(plot.title = element_text(face = "bold"))
r ggplot2 colors boxplot axis-labels
1个回答
0
投票

您可以先将

as.factor()
更改为
factor()
,然后添加
levels
labels
属性:

library(ggplot2)

Rurality<- c(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0)

CR<- c(895.3, 692.5, 834.6, 571.0, 618.8, 727.9, 286.8, 391.5, 440.9, 442.5, 343.3, 545.3, 205.9, 207.5, 259.7, 495.2, 689.8, 502.2, 434.9, 325.6, 185.3, 399.4, 218.7, 274.5, 461.4, 367.5, 337.0, 308.5, 334.2, 309.2, 236.6, 278.3, 349.0, 425.3, 225.7, 369.1, 297.7, 277.7, 425.7, 344.3, 287.5, 190.0, 182.6, 302.8, 262.8, 290.0, 339.5, 204.0, 333.4, 266.9, 276.4, 210.4, 299.7, 247.1, 237.4, 217.0, 247.4, 234.2, 305.2, 170.0, 339.5, 237.7, 215.0, 166.4, 283.4, 173.0, 185.1, 217.2, 237.0, 178.6, 280.3, 272.4, 133.8, 245.6, 367.3, 246.3, 338.5, 187.7, 167.2, 296.4, 262.4, 259.3, 153.6, 189.7, 241.9, 88.7, 153.8, 175.8)

要添加颜色,您只需要指定美观即可:

ggplot(data = data, aes(x = Rurality, y = CR, fill = Rurality)) +
    geom_boxplot() +
    geom_point() +
    labs(title = "Chlamydia Rate in Urban, Suburban, and Rural Ohio Counties", fill = "County Types", y = "Chlamydia Rate") +
    theme(plot.title = element_text(hjust = 0.5, face = "bold"),
          axis.text.x = element_blank(),
          axis.ticks.x = element_blank(),
          axis.title.x = element_blank())
© www.soinside.com 2019 - 2024. All rights reserved.