我使用dplyr使用fulljoin加入了两个数据帧 .
这是结果:
> head(newdf1)
spdate SP500close artprice
1 19870330 289.20 83.6
2 19870331 291.70 NA
3 19870401 292.39 NA
4 19870402 293.63 NA
5 19870403 300.41 NA
6 19870406 301.95 NA
然后使用reshape2融化:
库(reshape2)
df.melted <- melt(newdf1, id.vars = "spdate", na.rm = FALSE, value.name = "value", factorsAsStrings = TRUE)
融化之后...数据帧发生变化......
> head(df.melted)
spdate variable value
1 19870330 SP500close 289.20
2 19870331 SP500close 291.70
3 19870401 SP500close 292.39
4 19870402 SP500close 293.63
5 19870403 SP500close 300.41
6 19870406 SP500close 301.95
熔化实际上将artprice列附加到上面列的底部...但是我希望使用ggplot2 artprice列以及spdate和SP500close进行绘图 .
x轴想要是spdate .
两个Y轴.... SP500close,artprice .
我该如何正确融化?
谢谢
编辑**我找到了答案 . 答案是绘制为geom_point . 两个列可以共享相同的Y轴,因为缩放是相同的 . 以下是我的修复:
#Create Plot
library(ggplot2)
p1 <- ggplot(df.melted, aes(x=spdate, y=value, colour=variable,)) +
geom_point() +
theme_bw() +
labs(title = "Most Expensive Art Sales - S&P500 Plot",
subtitle = "1987 to Present",
y = "S&P500 Cose - Expensive Art Prices",
x = "Date") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
# Melt Dataframes For Plotting
library(reshape2)
df.melted <- melt(newdf, id.vars = "spdate", na.rm = FALSE)
#Save Plot
ggsave(filename="C:/R Projects/plot_1.png", plot=p1)
1 回答
我不确定你想要绘制的样子,但我认为你融化之后你已经有了正确格式的日期 .
我仍然从您之前发布的问题中获得数据方便,所以这是一个例子 .