R: Dotted “predicted” over solid bars in a bar chart

I have a dataset that displays the appearance of a certain natural phenomenon over the years to show that it is increasing over time. Data:

Year    Value
2012    10
2013    45
2014    212
2015    560
2016    570

However, 2016 is based on incomplete data (only until the end of July 2016). Thus, I would like, basically, a normal histogram, but with a “predicted” full value for 2016, a solid bar above it. I am using ggplot2 for the main chart, and this is what I still have:

test_DF = data.frame(Year = c("2012", "2013", "2014", "2015", "2016"),
                     Count = c(10, 45, 212, 560, 570))

base_graph = ggplot(test_DF, aes(x = test_DF$Year, y = test_DF$Count),
                    main = "Growth of Natural Phenomenon", xlab = "Year",
                    ylab = "Number of Occurences")

final_growth = base_graph + geom_bar(stat = 'identity', fill="#4F81BD") + theme_few() + labs(title="Growth of Natural Phenomenon", y="Number of Occurences",x="") + 
  geom_text(aes(label=test_DF$Count, vjust=-0.4)) + ylim(0, 1000)

But I have no idea how to add a dotted / otherwise textured “predicted” section above the final panel - I can’t find anything like it through Google. The graph I need looks something like this:

The graph I would like to do is apologize for the poor drawing, but hopefully gets the idea through

Thanks in advance for any advice!

+4
2

, . , 2016 . , , , .

library(tidyverse)

foo %>%
add_row(Year = 2016, Value = 230) %>%
mutate(group = c("a", "a", "a", "a", "a", "b")) -> out


ggplot(data = out, aes(x = Year, y = Value, fill = group)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("#4F81BD", "red")) +
labs(title = "Growth of Natural Phenomenon", x = "Year",
     y = "Number of Occurrences")

enter image description here

DATA

foo <- structure(list(Year = 2012:2016, Value = c(10L, 45L, 212L, 560L, 
570L)), .Names = c("Year", "Value"), class = "data.frame", row.names = c(NA, 
-5L))
+2

, :

library(ggplot2)
library(ggthemes)

test_DF = data.frame(Year = c(2015:2016, 2015:2016, 2012:2015),
                     Count = c(560, 570 + 230, 560, 570, 10, 45, 212, 560),
                     group=rep(c("predicted","measured: incomplete", "measured: complete"), 
                               c(2,2,4)))

test_DF$group = factor(test_DF$group, levels=c("predicted", "measured: incomplete","measured: complete"))

ggplot(test_DF, aes(Year, Count, colour=group, linetype=group, shape=group)) +
  geom_line(size=1) + 
  geom_point(size=2.5, stroke=1, fill="white") +
  theme_few() + labs(colour="", linetype="", shape="") +
  scale_colour_manual(values=hcl(c(15, 195,195),100,65)) +
  scale_linetype_manual(values=c(2,3,1)) +
  scale_shape_manual(values=c(16,21,16)) + 
  theme(legend.key.width=unit(2,"cm"))  +
  coord_cartesian(xlim=range(test_DF$Year) + c(-0.2,0.2), 
                  ylim=c(0, max(test_DF$Count)*1.04), expand=FALSE)

enter image description here

:

ggplot(test_DF, aes(Year, Count, colour=group, linetype=group, shape=group)) +
  geom_line(size=0.5, alpha=0.5) + 
  geom_text(aes(label=ifelse(Year==2015 & group != "measured: complete", NA, Count)), 
            size=3.8, fontface="bold", show.legend=FALSE) +
  theme_few() + labs(colour="", linetype="") +
  scale_colour_manual(values=hcl(seq(15,375,length=4)[1:3],100,65)) +
  scale_linetype_manual(values=c(2,3,1)) +
  theme(legend.key.width=unit(1.5,"cm")) +
  guides(colour=guide_legend(override.aes=list(alpha=1, size=0.8))) +
  coord_cartesian(xlim=range(test_DF$Year) + c(-0.2,0.25), 
                  ylim=c(0, max(test_DF$Count)*1.04), expand=FALSE)

enter image description here

+2

Source: https://habr.com/ru/post/1655135/


All Articles