Изменение порядка в гистограмме с накоплением - PullRequest
0 голосов
/ 22 ноября 2018

Я изо всех сил пытаюсь установить порядок моей гистограммы, несмотря на то, что я прочитал все примеры, которые я мог найти.Это определенно не стекирование, основанное на порядке в моем фрейме данных, который, кажется, является общим ответом.

Мне удалось изменить метки в правильном порядке, но сам график упрямый.

Я хотел бы ввести заказ вручную, поскольку он не следует шаблону.

enter image description here

данные

sdi_table_2<-read.table(text="'Over 50' 'Under 50'
'Intellectual Disability' 154814 214279
'Other Mental Health' 1387315 1012520
Injuries 240033 116167
Musculo-Skeletal 2041162 447212
'Nervous System' 530179 272756
'Circulatory, Respiratory, Endocrine, and Neoplasms' 1281261 260737
'Other and Unknown' 404973 212136", header=TRUE, check.names=F)
sdi_data_2<-as.data.frame.matrix(sdi_table_2)
sdi_data_2$type<-rownames(sdi_data_2)
sdi_data_2<-melt(sdi_data_2,id.vars = c("type"), value.name = "Beneficiaries")
sdi_data_2$variable <- as.character(sdi_data_2$variable)
colnames(sdi_data_2)[2] <- "Age"
factor(sdi_table_2$type, levels = sdi_table_2$type, ordered = TRUE)

участок

ggplot() +
  geom_bar(aes(y = Beneficiaries, x = Age, fill = type),
           data = sdi_data_2,
           stat = "identity") +
  theme_bw() +
  scale_y_continuous(label = comma) +
  scale_fill_manual(
    name = "",
    values = c(
      "Injuries" = "orange2",
      "Circulatory, Respiratory, Endocrine, and Neoplasms" =
        "sandybrown",
      "Nervous System" = "peachpuff",
      "Intellectual Disability" = "skyblue2",
      "Musculo-Skeletal" = "royalblue3",
      [enter image description here][1] "Other and Unknown" =
        "lightgoldenrod1",
      "Other Mental Health" = "royalblue1"
    ),
    breaks = c(
      "Other and Unknown",
      "Injuries",
      "Circulatory, Respiratory, Endocrine, and Neoplasms",
      "Nervous System",
      "Musculo-Skeletal",
      "Other Mental Health",
      "Intellectual Disability"
    )
  )

1 Ответ

0 голосов
/ 22 ноября 2018

Не знаю, понимаю ли я вашу проблему.Но вы можете заказать свой тип, используя уровень фактора.

sdi_data_2$type <- factor(sdi_data_2$type, levels = c("Circulatory, Respiratory, Endocrine, and Neoplasms","Nervous System","Injuries","Other Mental Health","Other and Unknown","Musculo-Skeletal","Intellectual Disability"))
sdi_data_2$Age <- factor(sdi_data_2$Age,levels = c("Under 50","Over 50"))

, а затем построить график:

ggplot() + geom_bar(aes(y = Beneficiaries, x = Age, fill = type), data = sdi_data_2, stat = "identity") + theme_bw()

Это результат: enter image description here

...