简体   繁体   English

R 数据未正确绘制

[英]R data not being plotted correctly

I'm really struggling with plots in R with ggplot and dplyr.我真的在用 ggplot 和 dplyr 处理 R 中的图。

I have a dataframe with columns:我有一个带列的数据框:

Customer.Name, Customer.Code, Product, Date, Platform, Input.Records, Output.Records

I performed a grouping with dplyr, to group the input records by the date.我使用 dplyr 进行了分组,以按日期对输入记录进行分组。

df$Date <- as.Date(df$Date, format = "%Y-%m-%d")
dateGrouping <- df%>% group_by(`Date`) %>% summarise(`Input.Records` = sum(`Input.Records`))

Then I tried to plot that.然后我试图绘制它。

myPlot <- ggplot(data=dateGrouping, aes(x=factor(Date), y=`Input.Records`, group=1)) +
  geom_line() + ylim(0, 85000000)
myPlot

But the output looks wrong, it looks like a bar plot, there is no connection between the lines但是输出看起来不对,看起来像条形图,线条之间没有联系“线”图

So I swapped geom_line() with geom_path()所以我换geom_line()geom_path() 在此处输入图片说明

And this definitely looks wrong...why is the path going back and forth?这看起来肯定是错误的......为什么这条路来回走动? it should be a left-to-right linear trajectory shouldn't it?它应该是一个从左到右的线性轨迹,不是吗?

This is the code to replicate the example.这是复制示例的代码。 Including only the code for the grouped frame because the full file is too large and contains confidential data.仅包含分组帧的代码,因为整个文件太大且包含机密数据。

   library(ggplot2) 
   library(dplyr) 
   df <- data.frame("Date" = c( "2020-08-10", "2020-08-11", "2020-08-12", "2020-08-13", "2020-08-14", "2020-08-15", "2020-08-16", "2020-08-17", "2020-08-18",
 "2020-08-19", "2020-08-20", "2020-08-21", "2020-08-22", "2020-08-23", "2020-08-24"),
 "Input.Records" = c(19501675,19298520,75546425,90104271,34139598,35384083,11849216,21996019,241643844,55643434,20733736,46198249,9815057,78211864,103263783))
    
    myPlot <- ggplot(data=df, aes(x=factor(Date), y=`Input.Records`, group=1)) +
      geom_path() + ylim(0, 85000000)
    myPlot

I think this is what you are looking for.我想这就是你要找的。 I made some slight changes to your code:我对您的代码进行了一些细微更改:

library(ggplot2)
#Plot
ggplot(data=df, aes(x=Date, y=Input.Records, group=1)) +
  geom_line()

Output:输出:

在此处输入图片说明

Let me know if that works for you.让我知道这是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM