【问题标题】:Plot two histograms of two .csv data sets to compare the data in R (ggplot)绘制两个 .csv 数据集的两个直方图以比较 R (ggplot) 中的数据
【发布时间】:2019-11-08 00:10:35
【问题描述】:

我想在一张图表中比较两个数据集(以太坊价格和交易量)。我绘制了一个图表,但我认为 y 轴的比例有问题:

ETH_price <- read.table(file = '~/R/export-EtherPrice.csv' , header = T, sep=";")

transaction_volume <- read.csv(file = '~/R/export-TxGrowth.csv', header = T, sep=";")

head(ETH_price)

head(transaction_volume)

ETH_price$Date.UTC. <- as.Date(ETH_price$Date.UTC., format = "%m/%d/%Y")

str(ETH_price) # verify the date format

transaction_volume$Date.UTC. <- as.Date(transaction_volume$Date.UTC., format = "%m/%d/%Y") 

str(transaction_volume) # verify the date format

ggplot(ETH_price,aes(x = Date.UTC.,y = Value)) + 
  geom_point()+
  geom_line(aes(color="ETH_price")) +
  geom_line(data=transaction_volume,aes(x = Date.UTC.,y = Value, color="transaction_volume")) +
  labs(color="Legend") +
  scale_colour_manual("", breaks = c("ETH_price", "transaction_volume"),
                      values = c("blue", "brown")) +
  ggtitle("Correlation of ETH price and transaction volume") + 
  theme(plot.title = element_text(lineheight=.7, face="bold"))

出现以下错误:

Error in seq.int(0, to0 - from, by) : 'to' must be a finite number

数据如下所示(ETH_price):

> head(transaction_volume)

   Date.UTC. UnixTimeStamp Value
1 03.03.2017    1488499200 64294
2 04.03.2017    1488585600 58756
3 05.03.2017    1488672000 57031
4 06.03.2017    1488758400 57020
5 07.03.2017    1488844800 62589
6 08.03.2017    1488931200 55386

剧情如下:

有人知道可能出了什么问题吗?

我对每一个提示都很满意!:)

马尼亚克

/代码更新

【问题讨论】:

  • 嗨,在这部分geom_line(data=transaction_volume,aes(color="transaction_volume")),您是否需要在aes 中传递 x 和 y 参数以便 geom_line 知道要绘制什么?还是这些参数与 ETH 价格数据集中的参数相同? (不确定是否相关)
  • 您好,谢谢您的快速回复! x 和 y 参数的数据在这两种情况下都是“Date.UTC”。和“价值” - 我应该为 transaction_volume 重复它吗?我认为一开始就定义它就足够了?我收到一条错误消息:geom_path:每个组仅包含一个观察值。需要调整群体审美吗?
  • 我会尝试根据变量Value 的范围使用具有不同值的ylim(...)
  • 谢谢,但老实说,我不知道如何用我提到的两个数据集实现 ylim!数据集中有 3000 多个对象。
  • 重置数据源时,需要指定x和y美学。您可以首先将您的数据帧组合成一个单独的帧,然后 ggplot 将知道要查找的内容。或者为第二行指定 x 和 y。如果您可以发布数据集的 bot 示例,您将获得更好的帮助,尤其是采用一种让人们可以轻松读入系统的格式。

标签: r ggplot2


【解决方案1】:

总结解决问题的所有关键步骤。

1) 您必须操纵日期格式才能被 ggplot 正确绘制。

2) 由于您的 ETH_price 值和 transaction_volume 值不在同一范围内,为了将它们绘制在单个图表上,您必须使用@r2evans 在这篇文章中描述的技巧:two y-axes with different scales for two datasets in ggplot2 [duplicate]

所以,你的代码应该是这样的:

# Here I re-created a small part of your dataset here just for the example
Date.UTC. = c("03.03.2017","04.03.2017","05.03.2017","06.03.2017","07.03.2017","08.03.2017")
Value = c(64294,58756,57031,57020,62589,55386)
transaction_volume = data.frame(Date.UTC.,Value)

Value = c(19.54,19.45,20.45,22.67,23.34,21.89)
ETH_price = data.frame(Date.UTC.,Value)

# Managing Date format
ETH_price$Date.UTC. = as.Date(ETH_price$Date.UTC., format = "%m.%d.%Y")
transaction_volume$Date.UTC. = as.Date(transaction_volume$Date.UTC., format = "%m.%d.%Y")
str(ETH_price) # to check the correct format of your dataset
str(transaction_volume) # to check the correct format of your dataset

# Merging dataset
ETH_price$z = "ETH_price"
transaction_volume$z = "transaction_volume"

# Defining the scale factor (you can adapt this part according your preferences for plotting)
scale_factor = mean(transaction_volume$Value / ETH_price$Value)
df_temp = within(transaction_volume, {Value = Value / scale_factor})
df = rbind(ETH_price,df_temp)
df

# Plotting both datasets
library(ggplot2)
mycolors = c("ETH_price" = "blue", "transaction_volume" = "red")
ggplot(df, aes(x = Date.UTC., y = Value, group = z, color = z)) +
  geom_path() +
  geom_line() +
  scale_y_continuous(name = "ETH_price", sec.axis = sec_axis(~scale_factor*., name = "transaction_volume")) +
  scale_color_manual(name = "Datasets", values = mycolors) +
  theme(
    axis.title.y = element_text(color = mycolors["ETH_price"]),
    axis.text.y = element_text(color = mycolors["ETH_price"]),
    axis.title.y.right = element_text(color = mycolors["transaction_volume"]),
    axis.text.y.right = element_text(color = mycolors["transaction_volume"])
  )

因此,您应该得到以下情节:

所以,我认为它应该可以解决您的问题;)

【讨论】:

    【解决方案2】:

    感谢您的回复!

    我检查了数据集,发现有一些损坏的行被我丢弃了。现在我有一个非常基本的问题(抱歉刚刚开始使用 R),excel 中的数据如下所示: Excel_data

    如果我把它放回第一列,日期就消失了,因为该列没有日期格式,而是有一个随机数。我只是有数据集,其中包含我导入到 R 的第一列中的所有数据。我会尝试使用 R 中当前看起来像这样的新数据的原始代码:

        > head(transaction_volume)
    
       Date.UTC. UnixTimeStamp Value
    1 03.03.2017    1488499200 64294
    2 04.03.2017    1488585600 58756
    3 05.03.2017    1488672000 57031
    4 06.03.2017    1488758400 57020
    5 07.03.2017    1488844800 62589
    6 08.03.2017    1488931200 55386
    

    如何读取数据,以便 R 能够以与 .csv 第一列中的数据相同的方式进行识别?

    抱歉打扰了。

    【讨论】:

    • 从 Excel 中,您是否将数据导出为 csv 文件?
    • 是的,我以 csv 文件的形式读取它:ETH_price &lt;- read.csv('~/R/export-EtherPrice.csv')
    • 也许你可以试试:ETH_price &lt;- read.table(file = '~/R/export-EtherPrice.csv' , header = T, sep=";").
    • 太棒了,成功了!我之前尝试了一个稍微不同的版本。不幸的是,现在我仍然遇到群体审美的错误 - 我不确定在我原始帖子的代码中的何处包含您的日期格式调整,有什么想法吗?
    • 您可以编辑您的第一篇文章以反映您的版本略有不同吗?然后,我将能够编辑我的代码,向您展示在哪里包含日期格式调整
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多