如何在条形图中绘制两个变量

时间:2018-03-15 23:56:10

标签: r bar-chart

我需要帮助将下面的数据框绘制在我将添加的条形图中。

      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

我目前的代码并不是很好用:

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")

这就是我想要做的。 非常感谢任何帮助。

Bar Graph Example

2 个答案:

答案 0 :(得分:2)

一个有用的技巧是将数据从“宽”更改为“长”。继续使用tidyverse(因为您正在使用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

所以绘制它有点简单:

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

使用的数据:

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')

答案 1 :(得分:0)

如果不能轻松访问您的数据来重现这一点,我所能做的就是从我使用的其中一个数据集中提供一些示例,所以希望这会很有用。方法1:ts.plot;方法2:Plotly;方法3:ggplot。

方法1:我想绘制V17&amp; V18在一起:

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

Method 1

方法2:Plotly; V29包含V17和V18的x坐标

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

方法3:ggplot; V29包含V17和V18的x坐标

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