r - 如果第一列中没有标题,如何使用 ggplot 创建折线图?
问题描述
我的代码在下面,我应该在哪里输入“x”的位置?另外,如果我想在不同列中显示的同一文件中绘制多个代码,我该怎么做?
ggplot(portfolioprices,aes(x = , y = INO)) + geom_line()
以下是数据的样子: 数据集
这就是我希望它看起来的样子,但带有标签: Graph generated with R
解决方案
访问您的数据和您打算绘制的图表类型使我更容易理解您的目标,现在我想我为您的问题找到了解决方案。尝试重现以下代码:
# 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')
这是输出:
让我知道它是否是您要查找的图表。
推荐阅读
- r - 按列值对 R 数据框进行子集,并将与特定值匹配的单元格更改为新值
- python - 组合从 TDA-API python 库返回的多个数据帧
- windows - Windows cmd:如何连接文件名?
- git - Git Merge Branch-对其他分支的特定更改,忽略合并
- postman - 如何分享 Newman htmlextra 报告?
- f# - 如何在 F# Program.fs 中隐藏记录?
- reactjs - 在 react-router-v6 中访问元素外的 URL 参数
- bigdata - Star Schema(数据建模)是否仍然与使用 Databricks 的 Lake House 模式相关?
- python - 使用服务主体访问 Azure Blob 存储但收到错误“无法获取本地颁发者证书”
- r - R方法提取像素数据