【问题标题】:How to plot several rows from a dataframe with ggplot?如何使用 ggplot 从数据框中绘制几行?
【发布时间】:2019-08-27 15:21:40
【问题描述】:

我尝试用 ggplot2 绘制 1845-1848 年每个农业年度法国的月度小麦价格。我得到了下表:

year,January,February,March,April,May,June,July,August,September,October,November,December
1845,,,,,,,,20.17,20.3,21.51,22.27,22.32
1846,22.36,22.65,22.42,22.26,22.48,22.93,22.92,24,24.9,25.97,27.59,28.01
1847,30.16,33.5,37.69,37.54,37.98,33.5,28.42,23.63,22.57,22.01,20.76,20.36
1848,20.01,19.34,18.12,16.59,16.58,15.88,15.67,,,,,

我想用以下方式用线和点绘制数据:

  1. x 为月份,y 为价格
  2. 按年分组:每年有自己的一行(四行)
  3. 没有数据 (NA) 的地方应该没有点和线

这个任务在 libreoffice calc 中非常容易解决,只需点击几下:选择所有表格 > 插入图表 > 线 > 点和线 > 下一个 > 数据系列在行中 + 第一行作为标签 + 第一列作为标签 > 完成(8 次点击)。

但我似乎找不到使用 R 和 ggplot2 来做同样事情的方法。

我需要能够在 R 中解决这个问题,以便对系列应用进一步的统计分析。

我尝试了以下解决方案:

# Reading the data
wheat <- read_csv("data/wheat.csv")

# Plotting
wheat %>%
  ggplot(aes(x=wheat[0,])) +
  geom_line(aes(y=as.numeric(wheat[1,]), group="year")) +
  geom_point()

我认为这样的代码会产生所需的情节。

但我得到了错误

"不知道如何自动选择对象类型的比例 tbl_df/tbl/data.frame。默认为连续。错误:美学 长度必须为 1 或与数据 (4) 相同:y, x"。

我知道 ggplot 看到一个 4x13 的小标题并等待 y 具有相同的长度 (4)。

但我想将表格行作为 y 值提供给他。

感谢您的帮助!

编辑

我的问题不是“Constructing a line graph using ggplot2”的重复。

虽然这是相同的一般问题 - 绘制一个数据帧的多个向量并准备数据以供 ggplot 使用 - 初始数据非常不同:我的是必须按时间顺序组织的历史数据,因此需要指定将在 x 上组织数据的levels。加上初始表是特殊的,需要与gather 进行特殊处理。

这里是整个工作代码供参考:

library(tidyverse)

# Reading into a tibble:
wheat <- read_csv("year,January,February,March,April,May,June,July,August,September,October,November,December
1845,,,,,,,,20.17,20.3,21.51,22.27,22.32
1846,22.36,22.65,22.42,22.26,22.48,22.93,22.92,24,24.9,25.97,27.59,28.01
1847,30.16,33.5,37.69,37.54,37.98,33.5,28.42,23.63,22.57,22.01,20.76,20.36
1848,20.01,19.34,18.12,16.59,16.58,15.88,15.67,,,,,")

# Tidying:
wheat_tidy <- wheat %>% gather(month, price, -year)

# Leveling:
wheat_tidy$month <- factor(wheat_tidy$month, levels = c("January","February","March","April","May","June","July","August","September","October","November","December"))

# Plotting:
wheat_tidy %>%
  ggplot(aes(x=month, y=price, group=year, color=as.factor(year))) +
  geom_line() +
  geom_point()

【问题讨论】:

  • 相关/可能重复:Stacked Bar Plot in R
  • 您需要将数据从宽格式更改为长格式。您可以使用dplyr::gather 执行此操作:df %&gt;% gather(month,value,-year,factor_key = T) %&gt;% ggplot(aes(month,value,group=factor(year),colour=factor(year))) + geom_line() + geom_point()

标签: r ggplot2


【解决方案1】:

这里有三个问题:

1)您的数据不整洁,这意味着月份不是变量。它只是一个列名。你可以使用gather 来帮助解决这个问题;

2) 在您的第一个aes() 语句中,您需要同时定义xy

3) 仅仅使用group 来定义年份并没有多大帮助;您仍然需要定义组中的每个值将如何不同——例如,使用color 使每一年的行具有不同的颜色。

这段代码对我有用(编辑:类似于上面 kstew 的评论,这是在我写答案时发布的):

library(tidyverse) #includes ggplot

wheat <-read_delim("year,January,February,March,April,May,June,July,August,September,October,November,December\n1845,,,,,,,,20.17,20.3,21.51,22.27,22.32\n1846,22.36,22.65,22.42,22.26,22.48,22.93,22.92,24,24.9,25.97,27.59,28.01\n1847,30.16,33.5,37.69,37.54,37.98,33.5,28.42,23.63,22.57,22.01,20.76,20.36\n1848,20.01,19.34,18.12,16.59,16.58,15.88,15.67,,,,,", delim = ",")

df <- wheat %>%
  gather(theMonth, wheatValue, -year)

plot <- ggplot(df, aes(x = theMonth, y = wheatValue, group = as.factor(year), color = as.factor(year))) +
  geom_line()

【讨论】:

  • 太好了,@mmyoung77!手工整理excel中的数据后,我能够得到相同的图。现在你已经向我展示了如何使用收集来做同样的事情,谢谢!但是结果图是错误的:x 轴应该按时间顺序排列月份,这里它们的顺序有些奇怪,因此线条是错误的。任何线索如何纠正 x 轴的顺序?
  • 好的,stackoverflow.com/questions/20041136/… 似乎已经回答了我的订购问题。 ggplot2 默认按字母数字顺序排列,可以更改。
  • as.factor 可能由于缺少数据而将月份排序错误。
  • 我确实需要使用wheat_tidy$month &lt;- factor(wheat_tidy$month, levels = c("January","February","March","April","May","June","July","August","September","October","November","December")) 明确影响月份名称作为级别
  • 感谢@mmyoung77,为您提供有效的解决方案!并感谢两位评论者的建议。非常感谢!
猜你喜欢
  • 2021-03-23
  • 1970-01-01
  • 2020-01-02
  • 2017-05-26
  • 2021-03-24
  • 2013-06-30
  • 2016-04-17
  • 1970-01-01
  • 2017-07-02
相关资源
最近更新 更多