-2

Below is my code. I am tried to add one line (data from a different csv file) on top of a stacked barplot however it wont work, the error says "object variable not found". Without added the geom_line the stacked barplot works so I assume it is the line that is creating the issue. Any ideas on how I fix this?

a <- read.csv("data.csv", header=TRUE, sep=",")

line1 <- read.csv("data1.csv", header=TRUE, sep=",")

line2 <- data.frame(line1)

library(reshape2)
c <- melt(a, id.var="day")

library(ggplot2)
a <- ggplot(c, aes(x=day, y=value, fill=variable)) +

 geom_bar(stat="identity", aes(x=day, y=value), width=0.7) +

 geom_line(data=line2, aes(x=day, y=value), color="black", stat="identity") 
 +

 scale_fill_manual(values = c("black", "grey47", "grey")) +

 scale_x_continuous(breaks = round(seq(min(m$day), max(m$day), by = 1),0))

 print(a)
  • 1
    1) `read.csv` returns a data.frame, you don't need `line2 <- data.frame(line1)`. 2) Can you post sample data? Please edit **the question** with the output of `dput(line1)`. Or, if it is too big with the output of `dput(head(line1, 20))`. – Rui Barradas Nov 21 '18 at 11:44
  • This is my sample data for data.csv (grouped barplot)day,emigration,security,checkin 1,6,5,4 2,6,5,6 3,6,5,9 4,6,5,1 5,5,6,3 6,3,6,5 7,1,8,7 8,9,9,9 9,8,9,8 10,7,9,6 11,6,8,4 12,4,8,2 13,3,5,1 14,1,7,3 15,2,7,4 16,4,6,5 17,5,5,6 18,6,5,7 19,8,4,8 20,7,3,8 21,5,2,2 22,4,2,1 23,1,2,2 24,2,2,3 25,4,4,4 26,9,9,5 27,8,7,7 28,7,6,8 29,6,4,9 30,4,3,1 31,3,2,1 – Lydia Blakley Nov 21 '18 at 12:08
  • This is the sample data for data1.csv (line plot) day,value 1,12 2,11 3,10 4,8 5,7 6,6 7,6 8,6 9,7 10,8 11,14 12,6 13,6 14,6 15,8 16,8 17,10 18,10 19,12 20,12 21,12 22,13 23,13 24,14 25,15 26,15 27,10 28,10 29,10 30,10 31,12 – Lydia Blakley Nov 21 '18 at 12:08
  • my guess is that you don't have a column name ```variable``` in ```line2```, if you move ```fill = variable``` to the ```aes``` in geom_bar it might solve the porblem – DS_UNI Nov 21 '18 at 12:14
  • @LydiaBlakley: please add the data to your question for better formatting – Tung Nov 21 '18 at 13:23
  • @LydiaBlakley: You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with creating a [reproducible example](https://stackoverflow.com/q/5963269). See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5). – Tung Nov 21 '18 at 13:25

3 Answers3

0

Based on your comments of your data structure, I suppose it might help joining your dataframes first and then building the plot using one dataset. You can try:

library(dplyr)
c <- c %>%
  left_join(line2 %>%
              rename(value_line2 = value),
            by="day")

Then adjust geom_line():

geom_line(data=c, aes(x=day, y=value_line2), color="black", stat="identity")

This might help. Please tell me if joining the data doesn't work as intended.

alex_555
  • 1,092
  • 1
  • 14
  • 27
0

In case it wasn't clear, this is what I meant in my comment above:

library(ggplot2)
a <- ggplot(c, aes(x=day, y=value)) +
     geom_bar(stat="identity", aes(x=day, y=value, fill=variable), width=0.7) +
     geom_line(data=line2, aes(x=day, y=value), color="black", stat="identity")
DS_UNI
  • 2,600
  • 2
  • 11
  • 22
0

The following is a complete code example to produce the graph below.
I have changed your variables' names, in order to make them more consistent. You had named both the data.frame in file "data.csv" and the result of your ggplot instruction a.

library(reshape2)
library(ggplot2)

a <- read.csv("~/data.csv")
line1 <- read.csv("~/data2.csv")

long <- melt(a, id.var = "day")

g <- ggplot(long, aes(x = day, y = value)) +
  geom_bar(aes(x = day, y = value, fill = variable), 
           stat = "identity", width = 0.7) +
  geom_line(data = line1, 
            aes(x = day, y = value), 
            color = "black") +
  scale_fill_manual(values = c("black", "grey47", "grey")) +
  scale_x_continuous(breaks = min(long$day):max(long$day))

print(g)

enter image description here

Data in dput format.

a <-
structure(list(day = 1:31, emigration = c(6L, 6L, 6L, 6L, 5L, 
3L, 1L, 9L, 8L, 7L, 6L, 4L, 3L, 1L, 2L, 4L, 5L, 6L, 8L, 7L, 5L, 
4L, 1L, 2L, 4L, 9L, 8L, 7L, 6L, 4L, 3L), security = c(5L, 5L, 
5L, 5L, 6L, 6L, 8L, 9L, 9L, 9L, 8L, 8L, 5L, 7L, 7L, 6L, 5L, 5L, 
4L, 3L, 2L, 2L, 2L, 2L, 4L, 9L, 7L, 6L, 4L, 3L, 2L), checkin = c(4, 
6, 9, 1, 3, 5, 7, 9, 8, 6, 4, 2, 1, 3, 4, 5, 6, 7, 8, 8, 2, 1, 
2, 3, 4, 5, 7, 8, 9, 1, 1)), class = "data.frame", 
row.names = c(NA, -31L))

line1 <-
structure(list(day = 1:31, value = c(12, 11, 10, 8, 7, 6, 6, 
6, 7, 8, 14, 6, 6, 6, 8, 8, 10, 10, 12, 12, 12, 13, 13, 14, 15, 
15, 10, 10, 10, 10, 12)), class = "data.frame", 
row.names = c(NA, -31L))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66