ggplot2:图例和条形图顺序不匹配

时间:2017-07-03 04:45:10

标签: r ggplot2

以下是足以重现问题的数据:

    structure(list(sensorheight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), yrmonth = structure(c(1459468800, 
1459468800, 1459468800, 1459468800, 1459468800, 1459468800, 1459468800, 
1459468800, 1459468800, 1459468800, 1462060800, 1462060800, 1462060800, 
1462060800, 1462060800, 1462060800, 1462060800, 1462060800, 1462060800, 
1462060800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    SummIndex = structure(c(23L, 24L, 24L, 25L, 26L, 27L, 28L, 
    29L, 30L, 31L, 23L, 24L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
    31L), .Label = c("Non-vented", "Outside", "Long", "Open", 
    "Open Shaded", "Outside Main", "Outside Secondary", "Shaded", 
    "Unshaded", "AB Outside", "AB Shaded", "CD HT", "CD Mid-T", 
    "CD Outside HT", "CD Outside Mid-T", "EF East 3", "EF Spring Inside 1", 
    "EF Spring Inside 2", "EF Spring Outside", "EF West 3", "EF Winter Inside", 
    "EF Winter Outside", "VKH East", "VKH Outside", "VKH West", 
    "VKSc", "VKSc Outside", "VKSV", "VKSV Outside", "VKY ", "VKY Outside"
    ), class = "factor"), N = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1), TempC = c(37.5240740740741, 
    26.8324074074074, 26.8324074074074, 33.8166666666667, 46.5069444444444, 
    27.8731481481481, 49.3949074074074, 28.2796296296296, 47.3606481481481, 
    28.0898148148148, 36.8436379928315, 30.4063620071685, 30.4063620071685, 
    34.8875448028674, 53.7643369175627, 30.7871863799283, 47.6971326164875, 
    30.9341397849462, 39.9758064516129, 30.3969534050179), sd = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), se = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), ci = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), freq = c(1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 
    1, 1, 1, 1, 1, 1, 1), count = c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), CountIndex = c("VKH East 1", 
    "VKH Outside 1", "VKH Outside 2", "VKH West 1", "VKSc 1", 
    "VKSc Outside 1", "VKSV 1", "VKSV Outside 1", "VKY 1", "VKY Outside 1", 
    "VKH East 1", "VKH Outside 1", "VKH Outside 2", "VKH West 1", 
    "VKSc 1", "VKSc Outside 1", "VKSV 1", "VKSV Outside 1", "VKY 1", 
    "VKY Outside 1"), GraphIndex = structure(c(27L, 28L, 29L, 
    30L, 31L, 32L, 35L, 36L, 33L, 34L, 27L, 28L, 29L, 30L, 31L, 
    32L, 35L, 36L, 33L, 34L), .Label = c("Farm 1 HT", "Farm 1 HT", 
    "Farm 1 Mid-T", "Farm 1 Mid-T", "Farm 2 Non-vented", "Farm 2 Non-vented", 
    "Farm 3 Caterpillar", "Farm 3 Open", "Farm 3 Shaded", "Farm 3 Unshaded", 
    "Farm 3 Open Shaded", "Farm 3 Open", "Farm 3 Shaded", "Farm 3 Unshaded", 
    "Farm 3 Caterpillar", "Farm 3 Open Shaded", "AB Shaded", 
    "AB Shaded", "Farm 4 East 3", "Farm 4 Spring Inside 1", "Farm 4 Gothic", 
    "Farm 4 Spring Outside 1", "Farm 4 Gothic", "Farm 4 West 3", 
    "Farm 4 Gothic", "Farm 4 Gothic", "School 1 Caterpillar", 
    "School 1 Caterpillar", "School 1 Standard", "School 1 Standard", 
    "School 2 Standard", "School 2 Standard", "School 4 Standard", 
    "School 4 Standard", "School 3 Standard", "School 3 Standard"
    ), class = "factor")), .Names = c("sensorheight", "yrmonth", 
"SummIndex", "N", "TempC", "sd", "se", "ci", "freq", "count", 
"CountIndex", "GraphIndex"), row.names = c(NA, -20L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), vars = c("sensorheight", "yrmonth", 
"SummIndex"), drop = TRUE, indices = list(0L, 1:2, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11:12, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L), group_sizes = c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 2L, labels = structure(list(
    sensorheight = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), yrmonth = structure(c(1459468800, 
    1459468800, 1459468800, 1459468800, 1459468800, 1459468800, 
    1459468800, 1459468800, 1459468800, 1462060800, 1462060800, 
    1462060800, 1462060800, 1462060800, 1462060800, 1462060800, 
    1462060800, 1462060800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    SummIndex = structure(c(23L, 24L, 25L, 26L, 27L, 28L, 29L, 
    30L, 31L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L), .Label = c("Non-vented", 
    "Outside", "Long", "Open", "Open Shaded", "Outside Main", 
    "Outside Secondary", "Shaded", "Unshaded", "AB Outside", 
    "AB Shaded", "CD HT", "CD Mid-T", "CD Outside HT", "CD Outside Mid-T", 
    "EF East 3", "EF Spring Inside 1", "EF Spring Inside 2", 
    "EF Spring Outside", "EF West 3", "EF Winter Inside", "EF Winter Outside", 
    "VKH East", "VKH Outside", "VKH West", "VKSc", "VKSc Outside", 
    "VKSV", "VKSV Outside", "VKY ", "VKY Outside"), class = "factor")), .Names = c("sensorheight", 
"yrmonth", "SummIndex"), row.names = c(NA, -18L), class = "data.frame", vars = c("sensorheight", 
"yrmonth", "SummIndex"), drop = TRUE))
#####################################################

month_names <- c("2016-04-01" = "Apr 2016", "2016-05-01" = "May 2016", "2016-06-01" = "Jun 2016", "2016-07-01" = "Jul 2016",
                 "2016-08-01" = "Aug 2016", "2016-09-01" = "Sep 2016", "2016-10-01" = "Oct 2016", "2016-11-01" = "Nov 2016",
                 "2016-12-01" = "Dec 2016", "2017-01-01" = "Jan 2017", "2017-02-01" = "Feb 2017", "2017-03-01" = "Mar 2017",
                 "2017-04-01" = "Apr 2017")

school_colors <- c("VKH East" = "purple2", "VKH Outside" = "grey48",   
               "VKH West" = "sienna3", "VKSc" = "dodgerblue",               
               "VKSc Outside" = "grey48", "VKSV" = "green4",        
               "VKSV Outside" = "grey48", "VKY" = "darkgoldenrod2",
               "VKY Outside" = "grey48")

图表上的条形顺序与图中的条形图不匹配。我希望他们匹配我认为这些因素正在发生,但我一直无法弄明白。

这是我的情节代码:

ggplot(subset(df, sensorheight %in% c("1") & 
                GraphIndex %in% c("School 1 Caterpillar",
                                  "School 1 Standard",
                                  "School 2 Standard",
                                  "School 3 Standard",
                                  "School 4 Standard")),
       aes(x=GraphIndex, y=TempC, fill=SummIndex, color = SummIndex)) + 
  geom_bar(stat="identity", position = "identity",alpha = 0.5, size = 0) +
  geom_point(shape=21, stroke=0, size = 0) +
  scale_y_continuous(name = "", expand = c(0,0)) +
  scale_color_manual(name = " ", breaks = c("VKH East", "VKH West", "VKSc",              
                                            "VKSV", "VKY", "VKY Outside"), 
                     values = school_colors, labels = c("School 1 Caterpillar", "School 1 Standard", "School 2 Standard",              
                                                        "School 3 Standard", "School 4 Standard", "Outside")) +
  scale_fill_manual(name = " ", breaks = c("VKH East", "VKH West", "VKSc",              
                                           "VKSV", "VKY", "VKY Outside"), 
                    values = school_colors, labels = c("School 1 Caterpillar", "School 1 Standard", "School 2 Standard",              
                                                       "School 3 Standard", "School 4 Standard", "Outside")) +
  facet_grid(.~yrmonth, labeller = as_labeller(month_names), switch = 'x') +
  theme(plot.background = element_rect(fill = "white", colour = "black", size = 1), legend.text= element_text(size = 11), 
        plot.title = element_text(size = 20), axis.text.x =element_blank(),  axis.ticks.x = element_blank(), axis.title.x = element_blank(),
        axis.text.y =element_text(size = 11), axis.title.y = element_text(size = 13, margin = margin(r = 10)),
        plot.margin = unit(c(.5,.2,.2,.5), "cm"), panel.grid.major.x = element_blank())

这是我得到的: enter image description here 我希望3号学校为黄色,4号学校为绿色。与情节和传奇中的订单相匹配。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

df$GraphIndex <- as.character(df$GraphIndex)

情节和图例顺序现在匹配。