R 如何在两种情况下排列ggplot2中的错误条

R 如何在两种情况下排列ggplot2中的错误条,r,plot,ggplot2,R,Plot,Ggplot2,我试图根据两列在ggplot中排列错误条。我已经看了一些以前提供的答案,这些答案对一个专栏很有帮助,但是当我想在两个专栏上排序时,我没有得到预期的结果 此代码将满足我的第一个愿望,即根据第一列结果订购钢筋 ddf$Outcome <- factor(ddf$Outcome, levels=c("Total", "cardva" , "ami" , "heartf" ,"dysrhy"), ordered=TRUE) 使用cat:ddf$cat中表示的级别使用示例数据集和代码,我在x轴

我试图根据两列在ggplot中排列错误条。我已经看了一些以前提供的答案,这些答案对一个专栏很有帮助,但是当我想在两个专栏上排序时,我没有得到预期的结果

此代码将满足我的第一个愿望,即根据第一列结果订购钢筋

ddf$Outcome <- factor(ddf$Outcome, levels=c("Total", "cardva" ,  "ami" ,  "heartf" ,"dysrhy"), ordered=TRUE)

使用cat:ddf$cat中表示的级别使用示例数据集和代码,我在x轴上得到一个带有结果的绘图,按照您在每个结果中为每个cat设置级别和点/条的顺序,也按照您设置级别的顺序,而不是字母顺序。这不是你想要的吗?@Henrik,你的建议满足了我的需要。谢谢
ddf$cat <- factor(ddf$cat, levels= c("Whole year",  "Cold","Warm"), ordered=TRUE)
dodge <- position_dodge(width=0.9) 
ggplot(ddf, aes(x = Outcome, y = pinc,ymin = lcinc, ymax = ucinc,  group=cat,color=cat,shape=cat))  +
  geom_point(position=dodge,size = 4) + geom_linerange(position=dodge,size =0.7) +
  geom_hline(aes(yintercept = 0))  + 
  scale_x_discrete("", labels = c("ami" = "AMI","cardva" = "Any CVD", "dysrhy" = "Dysrhythmia",
                                         "heartf" = "HF", "Total"= "All Subjects")) + theme_bw() +
  labs(colour="Period", shape="Period", y="Relative Difference (%)") +
  theme(axis.title=element_text(face="bold",size="14"),axis.text=element_text(size=14,face="bold"))
dput(ddf)
structure(list(Outcome = structure(c(1L, 1L, 1L, 2L, 3L, 4L, 
5L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L), .Label = c("Total", "cardva", 
"ami", "heartf", "dysrhy"), class = c("ordered", "factor")), 
    pinc = c(0.50389187458233, 0.579539836910437, 0.601685513579753, 
    0.28267013091543, 1.71852544919748, -0.284681087465499, -0.315288733679508, 
    -0.214290507233894, -0.0319464172748196, -1.0103617140803, 
    0.0669795902723536, 0.338718648843339, 2.46728604502957, 
    -0.626929184485836, -1.33251808343037), lcinc = c(0.124678813009127, 
    -0.000690299706695985, 0.101542783301833, -0.317021516853733, 
    0.436205248000321, -1.32202043292773, -1.93050761165515, 
    -1.18689153946393, -2.12567063251963, -2.7029110723661, -2.57800553889581, 
    -0.573641468633246, 0.539635356054902, -2.18756799993103, 
    -3.77359786707672), ucinc = c(0.884541170935749, 1.16313666688221, 
    1.10432713392166, 0.885969516714469, 3.01721768570142, 0.763563152101443, 
    1.32653303537096, 0.767883675589554, 2.10656667282445, 0.711630697043031, 
    2.78377546784718, 1.25945080162582, 4.43189566234674, 0.958610284817674, 
    1.17048722562318), cat = c("Whole year", "Cold season", "Warm season", 
    "Whole year", "Whole year", "Whole year", "Whole year", "Cold season", 
    "Cold season", "Cold season", "Cold season", "Warm season", 
    "Warm season", "Warm season", "Warm season")), .Names = c("Outcome", 
"pinc", "lcinc", "ucinc", "cat"), row.names = c("2", "8", "5", 
"41", "42", "44", "45", "31", "32", "34", "35", "26", "27", "29", 
"30"), class = "data.frame")