在叠加图中更改常用图例的顺序

时间:2018-06-14 11:26:42

标签: r ggplot2 legend

我想更改我的图例的顺序,而不是按字母顺序显示它们,如下所示。我想要

"NONE","LIGHT","MEDIUM","HEAVY","V_COLD","COLD","MEDIUM","HOT".

有可能吗?我试过几个论点但没有成功。

下面,我的表:

structure(list(SOUNAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "BALLYSHANNON (CATHLEENS FALL)", class = "factor"), 
    year_month = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
    6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L), .Label = c("2013-03", 
    "2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09", 
    "2013-10", "2013-12"), class = "factor"), pre_type = structure(c(4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 1L, 2L, 3L), .Label = c("HEAVY", "LIGHT", "MEDIUM", 
    "NONE"), class = "factor"), pre_value = c(13L, 2L, 11L, 5L, 
    9L, 3L, 10L, 7L, 2L, 6L, 13L, 10L, 10L, 1L, 15L, 4L, 16L, 
    2L, 7L, 5L, 2L, 2L, 17L, 9L, 7L, 3L, 13L, 6L, 5L, 2L, 10L, 
    14L, 1L, 5L, 19L, 6L), tem_type = structure(c(4L, 3L, 2L, 
    1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 
    2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 
    3L, 2L, 1L), .Label = c("COLD", "HOT", "MEDIUM", "V_COLD"
    ), class = "factor"), tem_value = c(0L, 7L, 0L, 23L, 0L, 
    29L, 0L, 1L, 0L, 29L, 2L, 0L, 0L, 21L, 9L, 0L, 0L, 5L, 25L, 
    0L, 0L, 18L, 13L, 0L, 0L, 21L, 9L, 0L, 0L, 26L, 5L, 0L, 0L, 
    24L, 0L, 7L), cnt_vehicle = c(NA, 2754406, NA, NA, NA, 2846039, 
    NA, NA, NA, 3149377, NA, NA, NA, 3058810, NA, NA, NA, 3362614, 
    NA, NA, NA, 3415716, NA, NA, NA, 3020812, NA, NA, NA, 3076665, 
    NA, NA, NA, 2775306, NA, NA), x = c(1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 
    6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L
    )), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value", 
"tem_type", "tem_value", "cnt_vehicle", "x"), row.names = c(NA, 
-36L), class = "data.frame")

在我的图表下方:

   ggplot(data = b_complet_2013, aes(x = x, y = pre_value*100000, fill = pre_type), stat = "identity") + 
      scale_x_continuous(breaks=(1:9)+0.2, labels=unique(b_complet_2013$year_month)) +
      geom_bar(stat = "identity", width=0.3) +
      xlab("date") + ylab ("Number of days of précipitations(left) and temperatures (ritght)") +
      ggtitle("Precipitation per month") +
      geom_bar(data=b_complet_2013,aes(x=x+0.4, y=tem_value*100000, fill=tem_type), width=0.3, stat = "identity") +
      xlab("date") + ylab("Number of days of precipitations(left) and temperatures (ritght)") +
      ggtitle("Impact of weather on road traffics")  + theme( axis.title.y = element_text(color = "blue", face = "bold")) + 
      theme(axis.text.y = element_text(color = "blue", face = "bold", size=9)) + theme( axis.title.y.right = element_text(color = "black", face = "bold")) + 
      theme(axis.text.y.right = element_text(color = "black", size = 9, face = "bold"))  +
      geom_line(mapping = aes(x= x+0.2, y = as.numeric(cnt_vehicle)), colour = I("blue"), size = 0.8) + 
      geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), colour = I("blue")), show.legend=FALSE, stat = "identity") +
      scale_y_continuous(sec.axis = sec_axis(~./100000,name="Number of days of precipitations(left) and temperatures (ritght)")) + 
      theme( plot.title = element_text(size = 17)) + theme(axis.title.x = element_text(size = 12)) + theme(axis.title.y = element_text(size = 12)) +
      labs(y = "Number of vehicles", color ="black") + 
      theme(panel.background = element_rect(linetype = "dashed", fill="white"), plot.background = element_rect(linetype = "dashed",fill="grey90" ))

0 个答案:

没有答案