R由组中的一个重新排序ggplot
R reorder ggplot by one of the group
以下数据有两组诊断和预期。我正在创建一个克利夫兰点图来比较诊断值与预期值。我想根据诊断率降序对图重新排序以显示 concept_names,这就是我设置数据集的方式。
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))
您可以使用 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-axis的顶部开始排序,那么我们可以先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())
以下数据有两组诊断和预期。我正在创建一个克利夫兰点图来比较诊断值与预期值。我想根据诊断率降序对图重新排序以显示 concept_names,这就是我设置数据集的方式。
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))
您可以使用 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-axis的顶部开始排序,那么我们可以先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())