传说中的行区域图中不可见

问题描述 投票:0回答:1

我正在用不同的数据集线区域图,我无法得到传说可见与去年制作值的图表可见。

我的预期输出https://cdn.filestackcontent.com/99Ji0U7Q7ObTmZN7B0vw

使R线区域图

my_data <- c(0,     10.22, 28.12, 61.21, 73.11, 101.34, 109.09, 110.82, 111.69, 113.87, 115.34, 118.67, 122.56,
             #NULL, NULL,  NULL,  NULL,  NULL,   NULL, NULL,   NULL,   NULL,  NULL,    NULL,   NULL,   122.67,
             0,     7.89,  15.46, 26.31, 45.78, 53.12, 61.11, 63.76,   64.55,  64.89,   65.67, 66.11,  70.21,
             0,     3.57,  10.98, 22.31, 28.21, 30.23, 31.45, 31.56,   31.78,  32.01,   35.67, 38.98,  45.23,
             0,     13.21, 28.87, 48.34, 69.47, 84.36, 95.78, 98.67,   98.99, 100.97, 101.58,  103.70,  117.47 )


my_matrix <- matrix(my_data, nrow = 4, ncol = 13, byrow = TRUE)

colnames(my_matrix) <- c(" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
use_row_names <- c("Plan", "LE - ROT", "LE - CTD", "LE - OPS Capex")

my_frame <- as.tibble(my_matrix) %>%
  select("January":"December") %>%
  mutate(grp_names = use_row_names)

print.data.frame(my_frame)

my_frame_long <- my_frame %>%
  gather("January", "February", "March", "April", "May", "June", "July", "August", "September", 
         "October", "November", "December", key = "Month", value = "Numbers") %>%
  mutate(Month = parse_date_time(Month, orders = c("%b", "%B", "b", "B"))) %>%
  mutate(Month = month(Month)) #without the label = TRUE, we get an actual plot....# , label = TRUE


my_frame_long <- my_frame %>%
  gather("January", "February", "March", "April", "May", "June", "July", "August", "September", 
         "October", "November", "December", key = "Month", value="Numbers") #%>%
  #mutate(Month = parse_date_time(Month, orders = c("%b", "%B", "b", "B")))# %>%
  #mutate(Month = month(Month)) #without the label = TRUE, we get an actual plot....# , label = TRUE



le_rot <- my_frame_long %>%
  filter(grp_names == "LE - ROT") #%>%
  #mutate(month = as.character.Date(Month)) #can't change Month to character, bc plot won't work
le_ops <- my_frame_long %>%
  filter(grp_names == "LE - OPS Capex")

le_ctd <- my_frame_long %>%
  filter(grp_names == "LE - CTD")

plan <- my_frame_long %>%
  filter(grp_names == "Plan")



le_ctd$Month <- as.factor(le_ctd$Month)
le_ctd$Month <- factor(le_ctd$Month, levels=c("January", "February", "March", "April", "May", "June", "July", "August", "September", 
                                              "October", "November", "December"))

le_rot$Month <- factor(le_rot$Month, levels=c("January", "February", "March", "April", "May", "June", "July", "August", "September", 
                                              "October", "November", "December"))

le_ops$Month <- factor(le_ops$Month, levels=c("January", "February", "March", "April", "May", "June", "July", "August", "September", 
                                              "October", "November", "December"))

plan$Month <- factor(plan$Month, levels=c("January", "February", "March", "April", "May", "June", "July", "August", "September", 
                                              "October", "November", "December"))






plot_combined <- ggplot() +

                geom_area(data = le_ctd, aes(x = Month, y = Numbers, group=1), fill = "yellow") +
                geom_line(data = le_ctd, aes(x = Month, y = Numbers, group=1), color = "yellow") +

                geom_area(data = le_ops, aes(x = Month, y = Numbers, group=1), fill = "brown") +
                geom_line(data = le_ops, aes(x = Month, y = Numbers, group=1), color = "brown") +

                geom_area(data = le_rot, aes(x = Month, y = Numbers, group=1), fill = "darkgreen") +
                geom_line(data = le_rot, aes(x = Month, y = Numbers, group=1), color = "darkgreen") +

                geom_line(data = plan, aes(x = Month, y = Numbers, group=1), color = "dodgerblue3") +
                theme_bw()+theme(panel.grid.major = element_blank(),
                                 panel.grid.minor = element_blank(),
                                  plot.title = element_text(hjust=0.5))+
  labs(x="",y="", title="MAIN TITLE")+
  annotate("Text")
  #legend(legend=c("1","2,","3","4"),col=c("red","blue","yellow","black"))#+ theme(legend.position="bottom", legend.direction="vertical")

                # 
                # scale_x_continuous(limits = range(le_rot$Month),
                #                    breaks = 1:12,
                #                    labels = 1:12) +
                # scale_y_continuous(limits = c(0, 150),
                #                    labels = seq.int(from = 0, to = 125, by = 25),
                #                    breaks = seq.int(from = 0, to = 125, by = 25))

plot_combined

预计输出https://cdn.filestackcontent.com/99Ji0U7Q7ObTmZN7B0vw

r ggplot2 graph line annotate
1个回答
1
投票

有点fifling代码的后下方应提供你的愿望。我有点缺乏经验与tidyverse,因此我决定与data.table图书馆去代替。这修复日期缩写为好。这将需要一点点fifling获得禁区外的价值观和线为好。

library(data.table)
my_data <- data.table(
  Plan = c(10.22, 28.12, 61.21, 73.11, 101.34, 109.09, 110.82, 111.69, 113.87, 115.34, 118.67, 122.56),
  `LE - ROT` = c(7.89,  15.46, 26.31, 45.78, 53.12, 61.11, 63.76,   64.55,  64.89,   65.67, 66.11,  70.21),
  `LE - CTD` = c(3.57,  10.98, 22.31, 28.21, 30.23, 31.45, 31.56,   31.78,  32.01,   35.67, 38.98,  45.23),
  `LE - OPS Capex` = c(13.21, 28.87, 48.34, 69.47, 84.36, 95.78, 98.67,   98.99, 100.97, 101.58,  103.70,  117.47),
  month = seq(as.Date("2018-01-01"),as.Date("2018-12-01"),by="m")
)
my_data_long <- melt(my_data, id.vars = c("month"), measure.vars = c("Plan","LE - ROT","LE - CTD","LE - OPS Capex"))
my_data_long[, variable := factor(variable, #Reorder levels ensuring that the ribbons are created correctly
                                  levels = c("Plan","LE - OPS Capex","LE - ROT","LE - CTD"))]
ggplot() + 
  geom_line(aes(x = month, y = value, lty = variable), 
            col = "blue", data =  my_data_long[variable == "Plan"]) + #Plot line (only rows for which the variable is Plan
  geom_ribbon(aes(x = month, ymin = 0, ymax = value, 
                  fill = variable, 
                  color = variable), 
              alpha =  .8,
              data = my_data_long[variable!="Plan"]) + #Plot area curves, only the rows for which variable is not Plan
  scale_fill_manual(values = c("darkred", "darkgreen", "lightblue"), name = "") + #colour for fill in geom_ribbon
  scale_color_manual(values = c("darkred","darkgreen","lightblue"), name = "") + #colour of lines in geom_ribbon
  scale_x_date(date_breaks = "1 month", date_labels = "%b") + #set x axis to abbreviated months
  scale_linetype(name = "") + #Legend for the Plan line (badly 
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(hjust = 0.5)) +
  labs(x="",y="", title="MAIN TITLE") +
  theme(legend.position = "bottom", 
    legend.key.height = unit(.5, "lines"),
    legend.key.width = unit(1, "lines"),
    legend.spacing = unit(-.2, "lines")) +  #Negative spacing between margins 
  guides(linetype = guide_legend(override.aes = list(size = 2))) #make the line legend fatter
© www.soinside.com 2019 - 2024. All rights reserved.