0

i need help with plotting the dataframe below in a bargraph which i'll add as well.

      Month       Base       Advanced
   2008-01-01  20.676043   20.358472
   2008-02-01  -57.908706  -62.368464
   2008-03-01  -3.130082   -5.876791
   2008-04-01  20.844747   14.162446
   2008-05-01  39.882740   42.315828
   2008-06-01  -12.802920  -13.333419
   2008-07-01  -49.299693  -39.843041
   2008-08-01  -4.563942   10.995445
   2008-09-01  -100.018700 -77.054218
   2008-10-01  -42.056913 -30.485998

My current code which isnt working great:

ggplot(ResidualsDataFrame,aes(x=Base,y=Advanced,fill=factor(Month)))+
geom_bar(stat="identity",position="dodge")+
scale_fill_discrete(name="Forecast",breaks=c(1, 2),     
labels=c("Base", "Advanced"))+
xlab("Months")+ylab("Forecast Error")

This is what I'm trying to make. Any help is kindly appreciated.

Bar Graph Example

2 Answers 2

2

One trick that helps is to change the data from "wide" to "long". Continuing with the tidyverse (since you're using ggplot2):

library(dplyr)
library(tidyr)
library(ggplot2)

x %>%
  gather(ty, val, -Month)
#         Month       ty         val
# 1  2008-01-01     Base   20.676043
# 2  2008-02-01     Base  -57.908706
# 3  2008-03-01     Base   -3.130082
# 4  2008-04-01     Base   20.844747
# 5  2008-05-01     Base   39.882740
# 6  2008-06-01     Base  -12.802920
# 7  2008-07-01     Base  -49.299693
# 8  2008-08-01     Base   -4.563942
# 9  2008-09-01     Base -100.018700
# 10 2008-10-01     Base  -42.056913
# 11 2008-01-01 Advanced   20.358472
# 12 2008-02-01 Advanced  -62.368464
# 13 2008-03-01 Advanced   -5.876791
# 14 2008-04-01 Advanced   14.162446
# 15 2008-05-01 Advanced   42.315828
# 16 2008-06-01 Advanced  -13.333419
# 17 2008-07-01 Advanced  -39.843041
# 18 2008-08-01 Advanced   10.995445
# 19 2008-09-01 Advanced  -77.054218
# 20 2008-10-01 Advanced  -30.485998

So plotting it is a little simpler:

x %>%
  gather(ty, val, -Month) %>%
  ggplot(aes(x=Month, weight=val, fill=ty)) +
  geom_bar(position = "dodge") +
  theme(legend.position = "top", legend.title = element_blank())

split barplot with dodging

The data used:

x <- read.table(text='      Month       Base       Advanced
   2008-01-01  20.676043   20.358472
   2008-02-01  -57.908706  -62.368464
   2008-03-01  -3.130082   -5.876791
   2008-04-01  20.844747   14.162446
   2008-05-01  39.882740   42.315828
   2008-06-01  -12.802920  -13.333419
   2008-07-01  -49.299693  -39.843041
   2008-08-01  -4.563942   10.995445
   2008-09-01  -100.018700 -77.054218
   2008-10-01  -42.056913 -30.485998', header=TRUE, stringsAsFactors=FALSE)
x$Month <- as.Date(x$Month, format='%Y-%m-%d')
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the prompt feedback. Im trying to plot it but its not working. What format should the ty column be in? currently its "chr" whilst val is "num". Do I need to change the "chr" format to a different format in order to plot the data?
If you post dput(head(dat)) (or whatever your data's variable name is), I might be able to see things not apparent in how you posted it the first time. ty should be character, as it is created by tidyr::gather; naming it "ty" was completely arbitrary and temporary, it was used solely to transform from wide to long. val is the higher-class of the two data columns (Base, Advanced), where chr > num > int > lgl, I hope they are both num.
yes, they are both num. > dput(head(ResidualsDataFrame)) structure(list(Month = structure(c(13879, 13910, 13939, 13970, 14000, 14031), class = "Date"), mod = c("Base", "Base", "Base", "Base", "Base", "Base"), val = c(20.6760432566744, -57.9087058298989, -3.13008154971899, 20.8447473204268, 39.8827397785123, -12.8029198207723 )), .Names = c("Month", "mod", "val"), row.names = c(NA, 6L), class = "data.frame")
I just cant seem to depict it the way you have in the sample graph above. R is not plotting my dataframe. I keep getting the following error message: Warning message: Computation failed in stat_count(): invalid 'type' (character) of argument
It looks like you might be trying to gather an already-gathered object. In my code above, I showed x %>% gather(...) by itself to demonstrate what it was doing. Then, in the next code block, I'm assuming the x is still in the "wide" format, not the tall format as you just posted in your comment. With what you justed put in the comment, try the same plotting command without the gather command.
|
0

Without easy access to your data to reproduce this, all I can do is provide some examples from one of the datasets I work with, so hopefully this will be useful. Method 1: ts.plot; Method 2: Plotly; Method 3: ggplot.

Method 1: I want to plot V17 & V18 together:

ts.plot(c(data1t$V17), gpars=list(col=c("black"), ylab="msec")) # first series
lines(data1t$V18,col="red") # second

Method 1

Method 2: Plotly; V29 contains my x-coordinates for both V17 and V18

library(plotly)
plot_ly(x=~data1t$V29, mode='lines') %>% 
   add_lines(y=~data1t$V17,
   line=list(color='rgb(205,12,24')) %>% 
   add_lines(y=~data1t$V18,
   line=list(color='rgb(12,24,205'))

Method 2

Method 3: ggplot; V29 contains my x-coordinates for both V17 and V18

data1t %>% arrange(V29) %>% 
    ggplot(aes(x=V29,y=value,color=variable)) +
    geom_line(aes(y=V17,col='spkts')) +
    geom_line(aes(y=V18,col='dpkts',
    alpha=0.5))

Method 3

1 Comment

Similarly, without access to your data (at least a sample of it), it is difficult to apply this to any other dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.