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.
这就是我想要做的。任何帮助都非常感谢。
2 个解决方案
#1
2
One trick that helps is to change the data from "wide" to "long". Continuing with the tidyverse
(since you're using ggplot2
):
一个有用的技巧是将数据从“宽”更改为“长”。继续使用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
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())
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')
#2
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.
如果没有轻松访问您的数据来重现这一点,我所能做的就是从我使用的其中一个数据集中提供一些示例,所以希望这会很有用。方法1:ts.plot;方法2:Plotly;方法3:ggplot。
Method 1: I want to plot V17 & V18 together:
方法1:我想将V17和V18一起绘制:
ts.plot(c(data1t$V17), gpars=list(col=c("black"), ylab="msec")) # first series
lines(data1t$V18,col="red") # second
Method 2: Plotly; V29 contains my x-coordinates for both V17 and V18
方法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 3: ggplot; V29 contains my x-coordinates for both V17 and V18
方法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))
#1
2
One trick that helps is to change the data from "wide" to "long". Continuing with the tidyverse
(since you're using ggplot2
):
一个有用的技巧是将数据从“宽”更改为“长”。继续使用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
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())
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')
#2
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.
如果没有轻松访问您的数据来重现这一点,我所能做的就是从我使用的其中一个数据集中提供一些示例,所以希望这会很有用。方法1:ts.plot;方法2:Plotly;方法3:ggplot。
Method 1: I want to plot V17 & V18 together:
方法1:我想将V17和V18一起绘制:
ts.plot(c(data1t$V17), gpars=list(col=c("black"), ylab="msec")) # first series
lines(data1t$V18,col="red") # second
Method 2: Plotly; V29 contains my x-coordinates for both V17 and V18
方法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 3: ggplot; V29 contains my x-coordinates for both V17 and V18
方法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))