首页 > 解决方案 > 如果第一列中没有标题,如何使用 ggplot 创建折线图?

问题描述

我的代码在下面,我应该在哪里输入“x”的位置?另外,如果我想在不同列中显示的同一文件中绘制多个代码,我该怎么做?

ggplot(portfolioprices,aes(x = , y = INO)) + geom_line()

以下是数据的样子: 数据集

这就是我希望它看起来的样子,但带有标签: Graph generated with R

标签: rggplot2

解决方案


访问您的数据和您打算绘制的图表类型使我更容易理解您的目标,现在我想我为您的问题找到了解决方案。尝试重现以下代码:

# load environment
library(lubridate)
library(ggplot2)
library(reshape2)
# define dataframe
df = data.frame(
  date = c('1/3/2020', '1/6/2020', '1/7/2020', '1/8/2020', '1/9/2020'),
  ino = c(2.98, 3.14, 3.15, 3.14, 3.1),
  mrna = c(18.89, 18.13, 17.78, 17.98, 18.4),
  nio = c(3.83, 3.68, 3.24, 3.39, 3.46),
  fcntx = c(13.8, 13.92, 13.9, 14, 14.12)
)
# transform date strings to date class
df$date = dmy(df$date)
class(df$date)
# tidy the dataframe to plot it
df = melt(df, id.vars = 1, measure.vars = 2:5)
# plot the graph
ggplot(df, aes(x = date, y = value, colour = variable)) +
  geom_line(size = 1.5) +
  scale_x_date(date_breaks = '1 month', date_labels = '%m/%Y')

这是输出:

在此处输入图像描述

让我知道它是否是您要查找的图表。


推荐阅读