【问题标题】:full_join using dplyr to make one dataframe - issues with melt and ggplot2full_join 使用 dplyr 制作一个数据框-melt 和 ggplot2 的问题
【发布时间】: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


    【解决方案1】:

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

    我仍然有你之前发布的问题的数据,所以这里有一个例子。

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

    【讨论】:

    • 感谢您的回复(再次)。我想在 X 轴上绘制日期。然后将 SP500 绘制为折线图中的时间序列,以便绘制每条线。然后,由于艺术 / SP 现在在同一条线上匹配,将艺术绘制为同一图表上的散点图。所以一切都应该按日期排列并在同一条线上。这也是为什么想按日期在同一行加入 art / SP500 的主要原因。
    • 哦,好的。我不认为你能做到这一点,因为你不能在 ggplot 中有两个不同的 y 轴(除非它们是像磅和公斤这样的直接转换)。但理论上你不想在加入后重新格式化,只需在geom_line() 中使用一个,在geom_point() 中使用另一个
    • 啊,好吧——我想我明白发生了什么!没问题 - 也许我可以制作两个不同的图,分散在下方,然后在顶部划线!再次感谢!今天学到了一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2014-05-31
    • 2020-07-18
    相关资源
    最近更新 更多