如何更改条形图的顺序(两个分类变量)

时间:2019-02-03 14:59:20

标签: r ggplot2

所以我被这个问题困扰了一段时间,即使经过大量的研究和实验也无法解决,请在这里帮助我。

我正在尝试绘制教育水平与整体健康之间的关系,这是我的代码。

p <- ggplot(educa_genhlth, aes(x = educa, fill = genhlth)) +
  geom_bar(position = "fill")
q <- p +
  aes(stringr::str_wrap(educa, 10)) + 
  labs(title = "general health vs education background") +
  xlab(NULL)
r <- q+
  scale_fill_discrete(name="general health")
r

this is the image I got

请注意,我写了第aes(string::str_wrap(Educa, 10))行是因为x变量的标签太长,彼此之间被阻塞,难以阅读。我是根据该网站上另一篇文章的建议搜索此功能的。

但是,一个新的问题是,酒吧没有遵循合乎逻辑的顺序,即从“从没上过学”到“大学四年...”。我认为是按字母顺序组织的。因此,我进行了一些研究,意识到必须对因子变量educa_health $ educa进行排序。所以我添加了另一行代码

educa_genhlth$educa <- factor(educa_genhlth$educa,
                              ordered = TRUE,
                              c("Never attended school or only kindergarten",
                                "Grades 1 through 8 (Elementary)",
                                "Grades 9 though 11 (Some high school)",
                                "Grade 12 or GED (High school graduate)",
                                "College 1 year to 3 years (Some college or technical school)",
                                "College 4 years or more (College graduate)"))

p <- ggplot(educa_genhlth, aes(x = educa, fill = genhlth)) +
  geom_bar(position = "fill")
q <- p +
  aes(stringr::str_wrap(educa, 10)) +
  labs(title = "general health vs education background") +
   xlab(NULL)
r <- q +
  scale_fill_discrete(name = "general health")
r

But the result is the same!

但是事实证明它没有任何改变。

但是,如果我保留命令educa_health$educa的行,但删除第4行中将字符串包装的部分,则可以得到我想要的重组图。(请注意,为了看到它更清楚地讲,我通过添加coord_flip()来水平翻转该图。

educa_genhlth$educa <- factor(educa_genhlth$educa,
                              ordered = TRUE,
                              c("Never attended school or only kindergarten", 
                                "Grades 1 through 8 (Elementary)",
                                "Grades 9 though 11 (Some high school)",
                                "Grade 12 or GED (High school graduate)",
                                "College 1 year to 3 years (Some college or technical school)",
                                "College 4 years or more (College graduate)"))

p <- ggplot(educa_genhlth, aes(x = educa, fill = genhlth)) +
  geom_bar(position = "fill")
q <- p +
  labs(title = "general health vs education background") +
  xlab(NULL)
r <- q +
  scale_fill_discrete(name = "general health") +
  coord_flip()
r

the right picture

我绝对不知所措。我想要的是使绘图保持垂直,使标签可读并具有我分配的逻辑顺序。如果有人可以告诉我该怎么做以及为什么我的原始方法无效,我真的很感激。

这是我的数据集的一小部分样本:

structure(list(educa = structure(c(6L, 5L, 6L, 4L, 6L, 6L), .Label = c("Never attended school or only kindergarten", 
"Grades 1 through 8 (Elementary)", "Grades 9 though 11 (Some high school)", 
"Grade 12 or GED (High school graduate)", "College 1 year to 3 years (Some college or technical school)", 
"College 4 years or more (College graduate)"), class = "factor"), 
    genhlth = structure(c(4L, 3L, 3L, 2L, 3L, 2L), .Label = c("Excellent", 
    "Very good", "Good", "Fair", "Poor"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

假设您已经按照所需顺序订购了educa,则可以将fct_relabel包中的forcatsstr_wrap一起使用,以更改其中的因子标签一步,而无需再次将其从字符转换为因数:

ggplot(educa_genhlth, 
       aes(x = forcats::fct_relabel(educa,
                                    stringr::str_wrap,
                                    width = 10), 
           fill = genhlth)) +
  geom_bar(position = "fill") +
  labs(title = "general health vs education background") +
  xlab(NULL) +
  scale_fill_discrete(name = "general health")

plot

这种方法还可以将educa_genhlth$educa保留在原始格式的数据框中,使您可以灵活地将其包装为其他图中的其他长度。

答案 1 :(得分:0)

使用str_wrap重新排序您的因素。因此,您需要先包装一下,然后重新排列因子:

educa_genhlth$educa <- stringr::str_wrap(educa_genhlth$educa,10)
educa_genhlth$educa <-factor(educa_genhlth$educa,ordered=TRUE,
                             stringr::str_wrap(c("Never attended school or only kindergarten",
                               "Grades 1 through 8 (Elementary)",
                               "Grades 9 though 11 (Some high school)",
                               "Grade 12 or GED (High school graduate)",
                               "College 1 year to 3 years (Some college or technical school)",
                               "College 4 years or more (College graduate)"),10))




p<-ggplot(educa_genhlth,aes(x=educa,fill=genhlth))+geom_bar(position="fill")
q<-p+aes(educa)+labs(title="general health vs education background")+xlab(NULL)
r<-q+scale_fill_discrete(name="general health")
r

enter image description here