Auré
Auré

Reputation: 161

Add a line to a bar plot. Not a lm or a smooth line

I would like to add a line to my barplot. This line is built with one of the variable of my dataframe "graphOR"). Here's my data :

graphOR1 <- structure(list(STYear_name = structure(c(2L, 4L, 5L, 1L, 3L), .Label = c(" Q4-2016", 
  "Q1-2016", "Q1-2017", "Q2-2016", "Q3-2016"), class = "factor"), 
  Obj = c(6.13, 6.06, 6.07, 6.07, 6.07), Real = c(7.8, 8.07, 
    7.71, 7.62, 8.33)), .Names = c("STYear_name", "Obj", "Real"
    ), row.names = c(NA, -5L), class = "data.frame")

graphOR1
    STYear_name Workareabis  Obj Real
11     Q1-2016     Overall 6.13 7.80
9      Q2-2016     Overall 6.06 8.07
8      Q3-2016     Overall 6.07 7.71
7      Q4-2016     Overall 6.07 7.62
10     Q1-2017     Overall 6.07 8.33



    dput(graphOR1)
        structure(list(STYear_name = structure(c(31L, 33L, 34L, 35L, 
32L), .Label = c("1-01-2017", "19-12-2016", "2-01-2017", "20-12-2016", 
"2015", "2016", "2017", "21-12-2016", "22-12-2016", "23-12-2016", 
"24-12-2016", "25-12-2016", "26-12-2016", "27-12-2016", "28-12-2016", 
"29-12-2016", "30-12-2016", "31-12-2016", "M01-2017", "M02-2016", 
"M03-2016", "M04-2016", "M05-2016", "M06-2016", "M07-2016", "M08-2016", 
"M09-2016", "M10-2016", "M11-2016", "M12-2016", "Q1-2016", "Q1-2017", 
"Q2-2016", "Q3-2016", "Q4-2016", "W01-2017", "W48-2016", "W49-2016", 
"W50-2016", "W51-2016", "W52-2016"), class = "factor"), Workareabis = c("Overall", 
"Overall", "Overall", "Overall", "Overall"), Obj = c(6.13, 6.06, 
6.07, 6.07, 6.07), Real = c(7.8, 8.07, 7.71, 7.62, 8.33)), .Names = c("STYear_name", 
"Workareabis", "Obj", "Real"), class = "data.frame", row.names = c(11L, 
9L, 8L, 7L, 10L))

I would like to add to my barplot a line with the variable "Obj". I tried :

    ggplot(data = graphOR1, aes(x=STYear_name, y= Real))+geom_bar(stat="identity",aes(fill =  Real))+scale_fill_gradient(low="#56B4E9", high="blue")+ggtitle("Plot") 
lines(graphOR1$STYear_name,graphOR1$Obj,col="blue")

or

    p1 <- ggplot(data = graphOR1, aes(x = STYear_name, y = Real)) + geom_bar(stat = "identity",aes(fill = Real))+scale_fill_gradient(low="#56B4E9", high="blue")+ geom_line(colour = "orange",size=1)

but it isn't working.

Thank you in advance!

Upvotes: 1

Views: 331

Answers (2)

Drew
Drew

Reputation: 108

Is this what you're looking for? You just need to add aes(y = Obj, group = 1) to your call to geom_line(). More info in the ggplot2 documentation. I couldn't exactly recreate your data frame (you would use dput(graphOR1) and paste its output into your post so others can properly recreate your problem), but I don't think it's necessary to solve your specific problem here.

graphOR1 <- data.frame(STYear_name = factor(c("Q1-2016", "Q2-2016", "Q3-2016", "Q4-2016", "Q1-2017"), 
                                            levels = c("Q1-2016", "Q2-2016", "Q3-2016", "Q4-2016", "Q1-2017")),
                       Workareabis = as.character(rep("Overall", times = 5)),
                       Obj         = as.numeric(c(6.13, 6.06, 6.07, 6.07, 6.07)),
                       Real        = as.numeric(c(7.8, 8.07, 7.71, 7.62, 8.33)),
                       stringsAsFactors = F)

str(graphOR1)

#'data.frame':  5 obs. of  4 variables:
# $ STYear_name: Factor w/ 5 levels "Q1-2016","Q2-2016",..: 1 2 3 4 5
# $ Workareabis: chr  "Overall" "Overall" "Overall" "Overall" ...
# $ Obj        : num  6.13 6.06 6.07 6.07 6.07
# $ Real       : num  7.8 8.07 7.71 7.62 8.33

ggplot(data = graphOR1, aes(x = STYear_name, y = Real)) + 
    geom_bar(stat = "identity",aes(fill = Real)) + 
    scale_fill_gradient(low="#56B4E9", high="blue") + 
    geom_line(aes(y = Obj, group = 1), colour = "orange",size=1) # note aes()

Obj as orange line

Upvotes: 1

Sandipan Dey
Sandipan Dey

Reputation: 23101

Try this:

ggplot(data = graphOR1, aes(x = STYear_name, y = Real)) + 
  geom_bar(stat = "identity",aes(fill = Real))+
  scale_fill_gradient(low="#56B4E9", high="blue")+ 
  geom_line(aes(x=as.integer(STYear_name), y=Obj), col = "orange", lwd=2)+
  ggtitle("Plot") 

enter image description here

Upvotes: 3

Related Questions