以下資料有兩組診斷和預期。我正在創建一個克利夫蘭點圖來比較診斷值與預期值。我想重新排序繪圖以根據診斷率降序顯示概念名稱,這就是我設定資料集的方式。
concept_name rate grp
1 Disorder of bone 0.28 Diagnosed
2 Fatigue 0.28 Diagnosed
3 Low back pain 0.28 Diagnosed
4 Abnormal findings on diagnostic imaging of lung 0.29 Diagnosed
5 Anemia 0.34 Diagnosed
6 Raised prostate specific antigen 0.53 Diagnosed
7 Secondary malignant neoplasm of bone 0.58 Diagnosed
8 Hyperlipidemia 0.59 Diagnosed
9 Essential hypertension 0.76 Diagnosed
10 Primary malignant neoplasm of prostate 0.97 Diagnosed
11 Secondary malignant neoplasm of bone 0.01 Expected
12 Disorder of bone 0.06 Expected
13 Primary malignant neoplasm of prostate 0.11 Expected
14 Raised prostate specific antigen 0.13 Expected
15 Abnormal findings on diagnostic imaging of lung 0.15 Expected
16 Fatigue 0.19 Expected
17 Anemia 0.20 Expected
18 Low back pain 0.25 Expected
19 Hyperlipidemia 0.60 Expected
20 Essential hypertension 0.74 Expected
p <- ggplot(df, aes(rate, concept_name))
geom_line(aes(group = concept_name))
geom_point(aes(color = grp) )
theme_bw() theme (legend.title=element_blank())

該圖不遵循資料集的順序。我希望看到概念名稱 - 骨骼疾病,疲勞,腰痛..
資料
structure(list(concept_name = structure(c(3L, 5L, 7L, 1L, 2L,
9L, 10L, 6L, 4L, 8L, 10L, 3L, 8L, 9L, 1L, 5L, 2L, 7L, 6L, 4L), .Label = c("Abnormal findings on diagnostic imaging of lung",
"Anemia", "Disorder of bone", "Essential hypertension", "Fatigue",
"Hyperlipidemia", "Low back pain", "Primary malignant neoplasm of prostate",
"Raised prostate specific antigen", "Secondary malignant neoplasm of bone"
), class = "factor", scores = structure(c(`Abnormal findings on diagnostic imaging of lung` = NA_real_,
Anemia = NA_real_, `Disorder of bone` = NA_real_, `Essential hypertension` = NA_real_,
Fatigue = NA_real_, Hyperlipidemia = NA_real_, `Low back pain` = NA_real_,
`Primary malignant neoplasm of prostate` = NA_real_, `Raised prostate specific antigen` = NA_real_,
`Secondary malignant neoplasm of bone` = NA_real_), .Dim = 10L, .Dimnames = list(
c("Abnormal findings on diagnostic imaging of lung", "Anemia",
"Disorder of bone", "Essential hypertension", "Fatigue",
"Hyperlipidemia", "Low back pain", "Primary malignant neoplasm of prostate",
"Raised prostate specific antigen", "Secondary malignant neoplasm of bone"
)))), rate = c(0.28, 0.28, 0.28, 0.29, 0.34, 0.53, 0.58,
0.59, 0.76, 0.97, 0.01, 0.06, 0.11, 0.13, 0.15, 0.19, 0.2, 0.25,
0.6, 0.74), grp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Diagnosed",
"Expected"), class = "factor")), class = "data.frame", row.names = c(NA,
-20L))
uj5u.com熱心網友回復:
您可以使用fct_inorder來自forcats:
library(tidyverse)
ggplot(df, aes(rate, fct_inorder(concept_name)))
geom_line(aes(group = concept_name))
geom_point(aes(color = grp))
theme_bw()
theme (legend.title = element_blank())

或者,如果您想要從 y 軸頂部開始排序,那么我們可以先arrange獲取資料,然后使用fct_inorder:
df %>%
arrange(grp, desc(rate), desc(concept_name)) %>%
ggplot(aes(rate, fct_inorder(concept_name)))
geom_line(aes(group = concept_name))
geom_point(aes(color = grp))
theme_bw()
theme (legend.title = element_blank())

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/466412.html
下一篇:按2列對2D陣列進行排序
