gpt4 book ai didi

r - 使用 ggplot 和 R 绘制多个层(条形图)

转载 作者:行者123 更新时间:2023-12-04 22:00:38 25 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .


6年前关闭。







Improve this question




我正在尝试使用列出全年库存和销售额的数据重新创建我在 Excel 中创建的条形图。这是我在 Excel 中的图表:

注:平均销售率是条形图中 13 个月的总销售额/总库存。


我通过 R 和 ggplot 包来做到这一点。我对此很陌生,但这是我迄今为止所管理的:

library(lubridate)
library(ggplot2)
library(scales)
library(reshape2)

COdata <- read.csv("C:/.../CenterOne.csv")

# Grab related data
# VIN refers to a unique inventory identifier for the item
# First Launch Date is what I use to count my inventory for the month
# Sale Date is what I use to count my sales for the month

DFtest <- COdata[, c("VIN", "First.Launch.Date", "Sale.Date")]

以下是数据的快照:
> head(DFtest)
VIN First.Launch.Date Sale.Date
1 4T1BF1FK4CU048373 22/04/2015 0:00
2 2T3KF4DVXCW108677 16/03/2015 0:00
3 4T1BF1FKXCU035935 19/03/2015 0:00 20/03/2015 0:00
4 JTDKN3DU3B1465796 16/04/2015 0:00
5 2T3YK4DV8CW015050
6 4T1BF1FK5CU599556 30/04/2015 0:00

我将日期转换为适当的格式,删除小时/秒并将它们分成每月间隔:
DFtest$First.Launch.Date <- as.Date(DFtest$First.Launch.Date, format = "%d/%m/%Y")
DFtest$Sale.Date <- as.Date(DFtest$Sale.Date, format = "%d/%m/%Y")
DFtest$month.listings <- as.Date(cut(DFtest$First.Launch.Date, breaks = "month"))
DFtest$month.sales <- as.Date(cut(DFtest$Sale.Date, breaks = "month"))

> head(DFtest)
VIN First.Launch.Date Sale.Date month.listings month.sales
1 4T1BF1FK4CU048373 2015-04-22 <NA> 2015-04-01 <NA>
2 2T3KF4DVXCW108677 2015-03-16 <NA> 2015-03-01 <NA>
3 4T1BF1FKXCU035935 2015-03-19 2015-03-20 2015-03-01 2015-03-01
4 JTDKN3DU3B1465796 2015-04-16 <NA> 2015-04-01 <NA>
5 2T3YK4DV8CW015050 <NA> <NA> <NA> <NA>
6 4T1BF1FK5CU599556 2015-04-30 <NA> 2015-04-01 <NA>

平均线图 - 我尝试创建一个
DF_Listings = data.frame(table(format(DFtest$month.listings)))
DF_Sales = data.frame(table(format(DFtest$month.sales)))
DF_Merge <- merge(DF_Listings, DF_Sales, by = "Var1", all = TRUE)

> head(DF_Listings)
Var1 Freq
1 2014-12-01 77
2 2015-01-01 886
3 2015-02-01 930
4 2015-03-01 1167
5 2015-04-01 1105
6 2015-05-01 1279

DF_Merge$Avg <- DF_Merge$Freq.y / DF_Merge$Freq.x

> head(DF_Merge)
Var1 Freq.x Freq.y Avg
1 2014-12-01 77 NA NA
2 2015-01-01 886 277 0.3126411
3 2015-02-01 930 383 0.4118280
4 2015-03-01 1167 510 0.4370180
5 2015-04-01 1105 309 0.2796380
6 2015-05-01 1279 319 0.2494136

ggplot(DF_Merge, aes(x=Var1, y=Avg, group = 1)) +
stat_smooth(aes(x = seq(length(unique(Var1)))),
se = F, method = "lm", formula = y ~ poly(x, 11))

enter image description here

条状图
dfm <- melt(DFtest[ , c("VIN", "First.Launch.Date", "Sale.Date")], id.vars = 1)
dfm$value <- as.Date(cut(dfm$value, breaks = "month"))

ggplot(dfm, aes(x= value, width = 0.4)) +
geom_bar(aes(fill = variable), position = "dodge") +
scale_x_date(date_breaks = "months", labels = date_format("%m-%Y")) +
theme(axis.text.x=element_text(hjust = 0.5)) +
xlab("Date") + ylab("")

enter image description here

所以我设法制作了一些情节,这给我带来了几个问题:
  • 我如何使用 ggplot 将它们组合成一个单一的图表?
  • 请注意我的条形图在第一个月和最后一个月是如何出现空白的?如何删除它(准确地说,如何从 x 轴上删除 11-2014 和 01-2016)?
  • 在我的条形图中,2014 年 1 月没有销售,因此库存条占用了更大的空间。如何减小其大小以适应图表的其余部分?
  • 我可以做些什么来将 x 轴从使用日期作为数字(即 12-2014)更改为使用单词中的月份年份(即 2014 年 12 月)。我试过使用 as.yearmon但这不适用于 scale_x_date我的 ggplot 函数的一部分。
  • 平均销售率线也存在问题,我可以放心地假设我将使用 geom_hline()但我不知道如何解决这个问题。
  • 最佳答案

    使用 mtoto 的建议使用 googleVis ,我尝试重新创建图表:

    # Testing Google Vis
    mytest <- DF_Merge

    library(zoo)
    library(plyr) # to rename columns
    library(googleVis)

    mytest$Var1 <- as.yearmon(mytest$Var1)
    mytest$Var1 <- as.factor(mytest$Var1) # googleVis cannot understand yearmon "class" so change it to factor

    # Rename columns to ensure comprehension
    mytest <- rename(mytest, c("Var1"="Date", "Freq.x"="Listings", "Freq.y"="Sales", "Avg"="Sales Rate"))

    # Prepare for values to be displayed right on the plot
    mytest$Listings.annotation <- mytest$Listings
    mytest$Sales.annotation <- mytest$Sales
    mytest$`Sales Rate.annotation` <- percent(mytest$`Sales Rate`) #Googlevis automatically understands that .annotation is used to display values in the graph

    # Create average rate line
    mytest$`Sales Rate` <- as.numeric(mytest$`Sales Rate`)
    mytest$AvgRate <- (sum(mytest$Sales) / sum(mytest$Listings))
    mytest <- rename(mytest, c("AvgRate"="Average Sales Rate"))


    # Create the annotation for the average line
    mytest$`Average Sales Rate.annotation` <- mytest$`Average Sales Rate`
    x = nrow(mytest) - 1
    mytest$`Average Sales Rate.annotation`[1:x] = "" # Ensures only the last row in this column has a value
    mytest$`Average Sales Rate.annotation` <- as.numeric(mytest$`Average Sales Rate.annotation`, na.rm = TRUE)
    mytest$`Average Sales Rate.annotation`[nrow(mytest)] <- percent(mytest$`Average Sales Rate.annotation`[nrow(mytest)]) # Transforms only the last row to a proper percentage!

    # Plot the graph
    column <- gvisComboChart(mytest, xvar= "Date",
    yvar=c("Listings", "Listings.annotation", "Sales", "Sales.annotation", "Sales Rate", "Sales Rate.annotation", "Average Sales Rate",
    "Average Sales Rate.annotation"),
    options=list(seriesType="bars",
    series="[{type: 'bars', targetAxisIndex:0, color:'orange'},
    {type: 'bars', targetAxisIndex:0, color:'green'},
    {type: 'line', targetAxisIndex:1, color:'red'},
    {type: 'line', targetAxisIndex:1, color:'purple', lineDashStyle:[2,2,20,2,20,2]}]",
    vAxes="[{format:'decimal', textPosition: 'out', viewWindow:{min:0, max:200}},
    {format:'percent', textPosition: 'out', viewWindow:{min:0, max:1}}]",
    hAxes="[{textPosition: 'out'}]",
    legend = "bottom",
    curveType="function",
    width=1500,
    height=800))

    plot(column)

    变量可以命名得更好,但我能够通过最终结果得到我想要的东西:

    enter image description here

    关于r - 使用 ggplot 和 R 绘制多个层(条形图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488237/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com