条形图和在R中添加额外标签

条形图和在R中添加额外标签,r,R,这是我的部分数据 可变低-中-高 D1 0 37.68116 62.31884 D20 55.07246 44.92754 D3 1.449275 47.82609 50.72464 D4 0 33.33333 66.66667 D5 1.449275 39.13043 59.42029 D6 2.898551 46.37681 50.72464 D7 0 42.02899 57.97101 D8 2.898551 49.27536 47.82609 D9 xx xx xx D10 xx xx

这是我的部分数据

可变低-中-高
D1 0 37.68116 62.31884
D20 55.07246 44.92754
D3 1.449275 47.82609 50.72464
D4 0 33.33333 66.66667
D5 1.449275 39.13043 59.42029
D6 2.898551 46.37681 50.72464
D7 0 42.02899 57.97101
D8 2.898551 49.27536 47.82609
D9 xx xx xx
D10 xx xx xx
D11 xx xx xx
D12 xx xx xx xx
D13 xx xx xx xx
D14 xx xx xx
D15 xx xx xx xx
D16 xx xx xx
D17 xx xx xx
D18 xx xx xx
D19 xx xx xx xx
D20 xx xx xx xx
D21 xx xx xx
D22 xx xx xx xx
D23 xx xx xx xx
D24 xx xx xx
是的,可能:

df <- tribble(
  ~Variable, ~Low, ~Medium,  ~High,
  "D1", 0, 37.68116, 62.31884,
  "D2", 0, 55.07246, 44.92754,
  "D3", 1.449275, 47.82609, 50.72464,
  "D4", 0, 33.33333, 66.66667,
  "D5", 1.449275, 39.13043, 59.42029,
  "D6", 2.898551, 46.37681, 50.72464,
  "D999", 2.898551, 46.37681, 50.72464
)

df %>%
  pivot_longer(-1) %>%
  mutate(name = factor(name, levels = c("Low", "Medium", "High")),
         label =
           case_when(
             Variable %in% c("D1", "D2", "D3") ~ "Time1St1",
             Variable %in% c("D4", "D5", "D6") ~ "Time1St2",
             T ~ "none_from_specified"
           ),
         label = factor(label, c("Time1St1", "Time1St2", "none_from_specified")) # here you specify the ordering
  ) %>%
  ggplot(aes(Variable, value, fill = name)) +
  geom_bar(stat = "identity", position = "dodge") + 
  facet_wrap(~label, scales = "free", strip.position = "bottom") +
  scale_color_manual(values = c("blue", "orange", "grey"), aesthetics = "fill")
df%
枢轴长度(-1)%>%
突变(名称=因子(名称,级别=c(“低”、“中”、“高”)),
标签=
什么时候(
变量%c(“D1”、“D2”、“D3”)~“Time1St1”,
%c(“D4”、“D5”、“D6”)~“Time1St2”中的变量%,
T~“未指定来自\u的\u”
),
label=系数(label,c(“时间1st1”、“时间1st2”、“未指定”))35;这里您指定了顺序
) %>%
ggplot(aes(变量、值、填充=名称))+
几何图形栏(stat=“identity”,position=“dodge”)+
端面包裹(~标签,scales=“free”,strip.position=“bottom”)+
比例颜色手册(值=c(“蓝色”、“橙色”、“灰色”)、美学=“填充”)
是的,有可能:

df <- tribble(
  ~Variable, ~Low, ~Medium,  ~High,
  "D1", 0, 37.68116, 62.31884,
  "D2", 0, 55.07246, 44.92754,
  "D3", 1.449275, 47.82609, 50.72464,
  "D4", 0, 33.33333, 66.66667,
  "D5", 1.449275, 39.13043, 59.42029,
  "D6", 2.898551, 46.37681, 50.72464,
  "D999", 2.898551, 46.37681, 50.72464
)

df %>%
  pivot_longer(-1) %>%
  mutate(name = factor(name, levels = c("Low", "Medium", "High")),
         label =
           case_when(
             Variable %in% c("D1", "D2", "D3") ~ "Time1St1",
             Variable %in% c("D4", "D5", "D6") ~ "Time1St2",
             T ~ "none_from_specified"
           ),
         label = factor(label, c("Time1St1", "Time1St2", "none_from_specified")) # here you specify the ordering
  ) %>%
  ggplot(aes(Variable, value, fill = name)) +
  geom_bar(stat = "identity", position = "dodge") + 
  facet_wrap(~label, scales = "free", strip.position = "bottom") +
  scale_color_manual(values = c("blue", "orange", "grey"), aesthetics = "fill")
df%
枢轴长度(-1)%>%
突变(名称=因子(名称,级别=c(“低”、“中”、“高”)),
标签=
什么时候(
变量%c(“D1”、“D2”、“D3”)~“Time1St1”,
%c(“D4”、“D5”、“D6”)~“Time1St2”中的变量%,
T~“未指定来自\u的\u”
),
label=系数(label,c(“时间1st1”、“时间1st2”、“未指定”))35;这里您指定了顺序
) %>%
ggplot(aes(变量、值、填充=名称))+
几何图形栏(stat=“identity”,position=“dodge”)+
端面包裹(~标签,scales=“free”,strip.position=“bottom”)+
比例颜色手册(值=c(“蓝色”、“橙色”、“灰色”)、美学=“填充”)

更新了答案。谢谢,如果我得到一个错误,您可以将其运行到24个变量吗?当然,但您需要在这里的代码中指定它们,以便清楚地知道要使用什么标签:变量%in%c(“D1”、“D2”、“D3”)~“Time1St1”,变量%in%c(“D4”、“D5”、“D6”)~“Time1St2”我知道了,非常感谢,如何订购例如Time1St1、Time1St2、Time2St1和Time2St2……有多种方法可供订购。请提出一个新问题。更新了答案。谢谢,如果我得到一个错误,您是否可以将其运行到24个变量上?当然,但您需要在这里的代码中指定它们,以便清楚地知道要使用什么标签:变量%in%c(“D1”、“D2”、“D3”)~“Time1St1”,变量%in%c(“D4”、“D5”、“D6”)~“Time1St2”我知道了,非常感谢,我如何订购它,例如Time1St1、Time1St2、Time2St1和Time2St2……有多种方法可以做到这一点。请问一个新问题。