简体   繁体   English

R中的同一图上绘制了2个具有相同x轴和facet_wrap的数据框

[英]2 dataframes with same x-axes and facet_wrap plotted on the same graph in R

This file shows the code I wrote so far. 该文件显示了我到目前为止编写的代码。 I have the following two data frames and I would like to plot Var1 and Var2, which have the same x axis but different y axes on a single plot and split those over the 4 quarters. 我有以下两个数据框,我想绘制Var1和Var2,它们在同一图上具有相同的x轴但不同的y轴,并将它们分成四个季度。

Currently I am using the following code but as an output it gives me 8 graphs rather than 4. 目前,我正在使用以下代码,但作为输出,它提供了8个图形而不是4个图形。

Create my two data frames and they have the say weekdays and same quarters, however the scales used to measure each shall be different on the graph 创建我的两个数据框,它们具有说的工作日和相同的季度,但是用于度量每个数据的比例在图表上应不同

weekday <- c(1,2,3,4,5,6,7)
quarter <- c("15Q1", "15Q2", "15Q3", "15Q4")
Var1 <- c(0.03, 0.5, 0.9, 2.3, 6, 3.4, 3.89, 0.08, 0.9, 1.2, 2.8, 8, 3.9, 5, 0.8, 2.7, 0.1, 7, 2, 3, 10, 2, 3, 1.9, 3.3, 4, 5.4, 6.89 )
dataframe1 = data.frame(weekday, quarter, Var1)

Var2 <- c(1000, 10001, 1500, 3000,2000, 3642, 2687, 2008, 5209, 4589, 3642,5336, 5342, 8962, 2301, 1365, 2300, 7412,3642, 3548,2355, 5698, 6538, 9856, 3142, 5962, 1253, 2100 )
dataframe2 = data.frame(weekday, quarter, Var2)

## I am plotting the first data frame here
## I assign the graph to a variable so it is easier to plot them. Its a bar chart            

g <-ggplot(dataframe1, aes(x = dataframe1$weekday, y = dataframe1$Var1)) + 
            geom_bar(stat="identity", position=position_dodge()) + 
            facet_wrap(~quarter) + ##I am trying to separate them here
            labs(x = "Purchase Order Day", y = "Cycle Time", title = "Cycle Time Weekly Seasonality") ##I just rename the axes here

##I am plotting the second data frame here
##The second graph I am plotting should be a line

z <- ggplot(dataframe2, aes(x = dataframe2$weekday, y = dataframe2$Var2)) +
            geom_line() + 
            facet_wrap(~quarter) +
            labs(x = "Purchase Order Day", y = "Number of Orders per Day", title = "Number of Purchase Orders per Week")

##I am trying to bind the two graphs in here

grid.newpage()
grid.draw(bind(ggplotGrob(g), ggplotGrob(z), size = "last"))

Here is a solution for combining the two plot formats. 这是组合两种绘图格式的解决方案。 But with your data the problem is, that the y-values of the line plot are much higher than the bar plots, so you do not see them. 但是对于您的数据,问题在于,线图的y值比条形图高得多,因此您看不到它们。 I made a normalisation to the data, you can decide how to deal with it: 我对数据进行了归一化,您可以决定如何处理它:

ggplot(dataframe1, aes(x = weekday, y = Var1)) + 
  geom_bar(stat="identity", position=position_dodge()) + 
  labs(x = "Purchase Order Day", y = "Cycle Time", title = "Cycle Time Weekly Seasonality")  +
  facet_wrap(~quarter) + 
  geom_line(data = dataframe2, aes(x = weekday, y = Var2/max(Var2)))

BTW: you can of course combine the two dataframes to one. 顺便说一句:您当然可以将两个数据帧组合为一个。

This is the result: 结果如下:

在此处输入图片说明

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

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