Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为ggplot facet\U wrap设置数据_R_Ggplot2_Facet Wrap - Fatal编程技术网

为ggplot facet\U wrap设置数据

为ggplot facet\U wrap设置数据,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,数据: 我想做的是使用四个段的facet\u wrap,其中每个段包含三个图,其中x轴标记为月-年,y轴标记为值,第一段中的三个图为L1、DRP-L1和OSM-L1等 每个段的顶部标签为L1、L2、L3和L4。我的问题是,我可以用上面显示的数据来设置它吗?或者我必须创建另一个标记为“Location”的列,该列将列出L1、L2、L3、L4,并且每一行为每个位置指定一个值 这是我到目前为止生成的代码,但(1)我不确定如何在每个段中添加其他行,即DRP_1、OSM_1等;(2)我不知道为什么它不画

数据:

我想做的是使用四个段的
facet\u wrap
,其中每个段包含三个图,其中x轴标记为月-年,y轴标记为值,第一段中的三个图为L1、DRP-L1和OSM-L1等

每个段的顶部标签为L1、L2、L3和L4。我的问题是,我可以用上面显示的数据来设置它吗?或者我必须创建另一个标记为“Location”的列,该列将列出L1、L2、L3、L4,并且每一行为每个位置指定一个值

这是我到目前为止生成的代码,但(1)我不确定如何在每个段中添加其他行,即DRP_1、OSM_1等;(2)我不知道为什么它不画零值

library(ggplot2,scales)
month=rep(c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),4)
numparts=c(0,   0,  0,  0,  0,  0,  52000,  2000,   0,  0,  0,  0,
           0,   0,  0,  0,  35000,  5000,   0,  20000,  0,  0,  0,  0,
           0,   0,  7500,   17000,  625,    0,  50, 0,  0,  2500,   0,  0,
           0,   0,  250,    0,  1800,   900,    800,    500,    600,    0,  0,  11390)
location=c("Location 1","Location 2","Location 3","Location 4")
data=data.frame(month,numparts,location)

# Faceting
ggplot(data, aes(y=numparts, x=month)) + 
  geom_line( stat="identity") + 
  expand_limits(y=0)+
  scale_x_discrete(labels = month)+
  facet_wrap(~location,scales="free_y")+
  labs(x="Month in 2017",y="Number of Parts")

您的数据代码与屏幕截图中的代码不匹配

这是从屏幕截图中选取前5行的示例数据:

要添加其他行,您需要组
aes
。看看这篇文章

库(ggplot2)
图书馆(比例尺)

val您的数据代码与屏幕截图中的代码不匹配

这是从屏幕截图中选取前5行的示例数据:

要添加其他行,您需要组
aes
。看看这篇文章

库(ggplot2)
图书馆(比例尺)

如果有人能告诉我如何将其他线添加到每个线段,以及如何绘制零线,我将不胜感激。如果有人能告诉我如何将其他线添加到每个线段,以及如何绘制零线,我将不胜感激。其余的我都能搞定。,
library(ggplot2)
library(scales)

val <- c(0, 0, 0, 0, 0,
       0, 0, 0, 0, 35000,
       0, 0, 7500, 17000, 625, 
       0, 0, 250, 0, 1800,
       1548, 500, 0, 0, 0,
       430, 2857, 0, 0, 0,
       0, 0, 0, 0, 0,
       0, 0, 0, 0, 0)

location <-   c(rep("Location 1", 5),
              rep("Location 2", 5),
              rep("Location 3", 5),
              rep("Location 4", 5))

part <- c(rep("L", 20),
          rep("DRP", 20))


data <- data.frame(location, part, val)
data$month <- factor(c("Jan","Feb","Mar","Apr", "May"), levels = c("Jan","Feb","Mar","Apr", "May"))


ggplot(data, aes(y = val, x = month, group = part)) + 
  geom_line(aes(color = part)) + scale_x_discrete(labels = month) + facet_wrap(~location)