【问题标题】:How to plot only a selected number of rows in R using ggplot如何使用ggplot在R中仅绘制选定数量的行
【发布时间】:2021-04-11 07:30:18
【问题描述】:

我正在尝试仅从第一列和第三列的数据框中的 260 行中绘制前 220 行。

这是数据框的“切片”。我试图不在 ggplot2 代码中包含“Average”、“Pre.Covid”和“Change”行。

structure(list(Date = c("01-2019", "01-2019", "01-2019", "01-2019", 
                    "01-2019", "Average", "Pre.Covid","Change"),
          Region = c("South East", "South West", "London", "East of England", 
          "East Midlands", "West Midlands", "Yorkshire and The Humber", 
          "North West"),
          Unemployment.rate = c("3.13", "2.916523", "4.210277", 
                     "3.194942", "4.682473", "5.070812", "5.231136", "3.607693")), 
      row.names = c(NA, -260L), 
      class = c("tbl_df", "tbl", "data.frame"))

我尝试使用下面的代码,

ggplot(df, aes(df[1:220,1], df[1:220,3], color = Region, group = Region)) +
            geom_line() +
            labs(x = "Date", y = "Unemployment rate (%)")

但是,我收到以下错误消息:

不知道如何为 tbl_df/tbl/data.frame 类型的对象自动选择比例。默认为连续。

不知道如何为 tbl_df/tbl/data.frame 类型的对象自动选择比例。默认为连续。(是的,两次)

is.finite(x) 中的错误:没有为类型“列表”实现默认方法

知道如何解决这个问题吗? 另外,如果有人可以请告诉我如何将这些数值更改为带有 2 位小数的百分比,那就太好了。

提前感谢您提供的任何帮助!

【问题讨论】:

  • 请调试您的输出

标签: r ggplot2 aes linegraph


【解决方案1】:

您可以在绘图之前对数据框进行子集化,而无需更改 aes

library(ggplot2)

ggplot(df[1:220, ], 
       aes(Date, Unemployment.rate, color = Region, group = Region)) +
  geom_line() +
  labs(x = "Date", y = "Unemployment rate (%)")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2020-11-19
    相关资源
    最近更新 更多