【问题标题】:Scatterplot in R with ggplot2:带有ggplot2的R中的散点图:
【发布时间】:2019-02-16 12:33:57
【问题描述】:

我有以下数据集。

> y
                  DF           GE
2006-12-21 -0.0004659447 -0.009960682
2006-12-22 -0.0009323238 -0.005295208
2006-12-26 -0.0032661785  0.003726377
2006-12-27  0.0042131332  0.002121453
2006-12-28 -0.0009323238 -0.008203228
2006-12-29 -0.0135313109 -0.007203842

当我强化它时,它变成了这个。

> fortify(y, melt=TRUE)
    Index     Series         Value
1  2006-12-21     DF -0.0004659447
2  2006-12-22     DF -0.0009323238
3  2006-12-26     DF -0.0032661785
4  2006-12-27     DF  0.0042131332
5  2006-12-28     DF -0.0009323238
6  2006-12-29     DF -0.0135313109
7  2006-12-21     GE -0.0099606815
8  2006-12-22     GE -0.0052952078
9  2006-12-26     GE  0.0037263774
10 2006-12-27     GE  0.0021214532
11 2006-12-28     GE -0.0082032284
12 2006-12-29     GE -0.0072038420

现在我想在 ggplot2 中运行散点图。目前,我使用的功能如下。

x <- fortify(x, melt=TRUE)
ggplot(data=x) +
geom_point(size=10, aes(x=Series, y=Value, colour=Index))

但我真正想要的是一个散点图,其中 DF 和 GE 的值作为 X 和 Y 轴上的坐标,并以颜色显示日期。

我根本不知道如何修改数据结构来实现这一点。

【问题讨论】:

    标签: r ggplot2 rstudio scatter-plot


    【解决方案1】:

    你的意思是这样的吗?

    library(tidyverse)
    df %>%
        rownames_to_column("date") %>%
        mutate(date = as.Date(date)) %>%
        ggplot(aes(x = DF, y = GE)) +
        geom_point(aes(colour = date))
    


    样本数据

    df <- read.table(text =
        "                 DF           GE
    2006-12-21 -0.0004659447 -0.009960682
    2006-12-22 -0.0009323238 -0.005295208
    2006-12-26 -0.0032661785  0.003726377
    2006-12-27  0.0042131332  0.002121453
    2006-12-28 -0.0009323238 -0.008203228
    2006-12-29 -0.0135313109 -0.007203842", header = T)
    

    【讨论】:

    • 正是我需要的。谢谢。
    【解决方案2】:

    我不会使用fortify()。我会按原样保留您的数据并执行以下操作:

    library(tidyverse)
    
    y %>%
    rownames_to_column() %>%   
    ggplot(aes(x = DF, y = GE, color = index) + 
    geom_point()
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 1970-01-01
      • 2021-09-29
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多