GGPLOT问题;代码将线路绘制为一些非常奇怪的条。为什么这是?

发布于 2025-02-13 10:02:12 字数 1112 浏览 1 评论 0原文

在绘制来自数据框架的一些数据时,我有一些问题。它似乎将多个系列视为同一行。有人知道为什么这是吗?

dataframe代码创建df:

df <- read.table(text="Date     Item            Purchased
01/01/08 Fruit              48
01/01/08 Confectionary      42
01/01/08 Appliance          11
01/06/08 Confectionary      16
01/06/08 Fruit              19
01/06/08 Appliance          50", header=TRUE)

我要绘制数据

test_df <- ggplot(df)+geom_line(aes(x=Date, y=Purchased, color=Item, group = 1),size=8)+
  theme(title=element_text(size=10),panel.background = element_rect(fill='white'),legend.position='top')+geom_hline(yintercept = 0,color="white")

test_df

的代码:我的绘图结果: “在此处输入图像说明”

我想绘制一些时间这些数据系列; “水果”的一条线,一个用于“糖果”的行,另一个用于“设备”。有人知道为什么这样做吗?

*编辑:添加x = lubridate :: mdy(date)产生您以下结果:

I'm having a bit of a problem when it comes to plotting some data from a dataframe. It seems to treat multiple series as being the same line. Does anyone know why this is?

Dataframe code to create df:

df <- read.table(text="Date     Item            Purchased
01/01/08 Fruit              48
01/01/08 Confectionary      42
01/01/08 Appliance          11
01/06/08 Confectionary      16
01/06/08 Fruit              19
01/06/08 Appliance          50", header=TRUE)

My code to plot data:

test_df <- ggplot(df)+geom_line(aes(x=Date, y=Purchased, color=Item, group = 1),size=8)+
  theme(title=element_text(size=10),panel.background = element_rect(fill='white'),legend.position='top')+geom_hline(yintercept = 0,color="white")

test_df

This result of my plot:enter image description here

I'd like to plot some time series of this data; one line for 'Fruit', one for 'Confectionary' and one for 'Appliance'. Does anyone know why it's doing this?

*EDIT: Adding x=lubridate::mdy(Date) produces thee following result:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2025-02-20 10:02:13

感谢MRFlick,问题包括该组= 1

With thanks to MrFlick, the issue was including the group=1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文