【发布时间】:2017-06-03 02:31:22
【问题描述】:
我使用 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
melt 实际上将 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)
【问题讨论】:
标签: r