首页 文章

full_join使用dplyr制作一个数据帧 - 与melt和ggplot2有关

提问于
浏览
1

我使用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)

enter image description here

1 回答

  • 0

    我不确定你想要绘制的样子,但我认为你融化之后你已经有了正确格式的日期 .

    我仍然从您之前发布的问题中获得数据方便,所以这是一个例子 .

    library(readr)
    library(dplyr)
    library(tidyr)
    library(lubridate)
    library(ggplot2)
    
    df1 <- readr::read_csv(
    'artdate,artprice
    19870330,"$83.60"
    19871111,"$113.60"
    19881128,"$78.00"
    19890509,"$92.50"
    19890531,"$68.00"
    19890801,"$115.90"'
    )
    
    df2 <- readr::read_csv(
    'SP500close,SP500date
    289.20,19870330
    291.70,19870331
    292.39,19870401
    293.63,19870402
    300.41,19870403
    301.95,19870406'
    )
    
    full_join(df1, df2, by = c("artdate" = "SP500date")) %>% 
      gather("var", "val", -artdate) %>% 
      mutate(val = readr::parse_number(val),
             date = lubridate::ymd(artdate)) %>% 
      drop_na(val) %>% 
      ggplot(aes(date, val, color = var)) +
      geom_point()
    

相关问题