【问题标题】:Having trouble with facet wrap in ggplotggplot 中的 facet wrap 有问题
【发布时间】:2018-11-28 21:25:57
【问题描述】:

我在 ggplot 中设置一个包含三个变量的分面包装有点困难,其中两个是点,第三个是一条线。应该有 12 个图(每个月一个,这是 facet wrap),沿 x 的位置和沿 y 的 numparts。这三个变量是 OSM、DRP 和需求。这是样本数据(实际数据由每月 30 个地点的 12 个月组成)。我将其添加为代码,因为我不知道如何将 Excel 数据添加到 Stack Overflow。

到目前为止我生成的代码如下,但它不起作用。我也不知道如何将这三个变量变成图例。任何帮助将不胜感激。

library(ggplot2)
parts$Location<-as.factor(parts$Location)
parts$OSM<-as.factor(parts$OSM)
parts$DRP<-as.factor(parts$DRP)
parts$Demand<-as.factor(parts$Demand)
parts$Month<-as.factor(parts$Month)
data=data.frame(parts$Location,parts$Month,parts$OSM,parts$DRP,parts$Demand)

# Faceting
ggplot(data)+geom_point(stat = "identity", aes(y=parts$OSMparts, x=parts$Location,pch=4,col="red" )) + 
  geom_point(stat = "identity", aes(y=parts$DRPparts, x=parts$Location,pch=16,col="blue" ))+
  geom_line(stat = "identity", aes(y=parts$Demandparts, x=parts$Location,col="green" ))
  facet_wrap(~parts$Month)



Location    Month   OSM Demand  DRP
1   January 0   0   1
2   January 2   0   2
3   January 0   1   0
4   January 0   0   1
5   January 2   0   2
1   February    0   0   0
2   February    2   2   2
3   February    0   0   0
4   February    1   1   0
5   February    1   1   2
1   March   0   0   0
2   March   2   4   2
3   March   0   1   0
4   March   2   2   1
5   March   2   2   2
1   April   0   0   NA
2   April   2   2   NA
3   April   0   0   NA
4   April   1   1   NA
5   April   2   4   NA
1   May 0   0   NA
2   May 1   0   NA
3   May 0   0   NA
4   May 2   2   NA
5   May 2   0   NA
1   June    0   0   0
2   June    3   6   2
3   June    0   0   0
4   June    1   3   1
5   June    2   7   2
1   July    0   0   0
2   July    3   3   3
3   July    0   1   0
4   July    3   4   2
5   July    3   4   3
1   August  0   1   0
2   August  3   3   3
3   August  0   0   0
4   August  4   6   3
5   August  3   6   3
1   September   0   0   1
2   September   3   0   3
3   September   0   0   1
4   September   2   3   3
5   September   3   1   4
1   October 0   0   0
2   October 2   1   2
3   October 0   0   0
4   October 4   3   3
5   October 3   1   3
1   November    0   0   0
2   November    2   1   2
3   November    0   1   0
4   November    5   7   3
5   November    4   5   3
1   December    0   0   0
2   December    2   5   2
3   December    0   0   0
4   December    2   0   4
5   December    5   13  3

【问题讨论】:

  • 请将代码或数据以文本形式发布,并not as images。另请参阅如何制作great reproducible example。
  • 欢迎来到 SO(我这么说,因为您发布了一张数据图片)。图片既不是代码也不是数据,除非主题是图像处理。请删除图片,点击问题下方的“r”,点击“信息”并研究如何发布正确的问题。

标签: r ggplot2 charts facet-wrap


【解决方案1】:

解决方案远比你想办法简单得多。

ggplot(parts) +
  geom_point(aes(x=Location, y=OSM, col="blue"), pch=16)+
  geom_point(aes(x=Location, y=DRP, col="red"), pch=16)+
  geom_line(aes(x=Location, y=Demand, col="green"))+
  facet_wrap(~factor(Month, levels = month.name))+
  scale_colour_discrete(name  ="Label",
                        labels=c("DRP", "Demand","OSM"))

【讨论】:

  • 谢谢,但是如何按正确的顺序排列月份?
  • 抱歉,只需将 levels = month.name 添加到因子中即可。查看更新的代码。
猜你喜欢
  • 2019-08-04
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
相关资源
最近更新 更多