简体   繁体   English

ggplot facet_wrap图

[英]ggplot facet_wrap graph

my data set looks lik this: 我的数据集看起来像这样:

x X

date server cpu
1/1/2012 A  80
1/1/2012 B  20
1/1/2012 C  10
1/2/2012 A  80
1/2/2012 B  20
1/2/2012 C  10
1/3/2012 A  80
1/3/2012 B  20
1/3/2012 C  10
1/4/2012 A  80
1/4/2012 B  20
1/4/2012 C  10
1/5/2012 A  80
1/5/2012 B  20
1/5/2012 C  10
1/6/2012 A  80
1/6/2012 B  20
1/6/2012 C  10

I like to create a separate graph for each server using ggplot facet_wrap (unless there is a better way to do this?) 我喜欢使用ggplot facet_wrap为每个服务器创建一个单独的图(除非有更好的方法来做到这一点?)

my code is this: 我的代码是这样的:

ggplot(x, aes(date, cpu, group=server, colour=server)) 
+ geom_point() 
+ facet_wrap(~server) 
+ ylim(0,100) 
+ theme_bw() 
+ geom_smooth(method="lm", se=TRUE, size=1) 
+ xlab("Date") 
+ ylab("CPU") 
+  opts(title="% CPU"),plot.title=theme_text(size=14, colour="navyblue"),axis.title.x = theme_text(face="bold", size=12, colour="#990000"), axis.text.x  = theme_text(angle=90, size=9, face="bold"),axis.title.y = theme_text(face="bold", size=12, colour="#990000", angle=90), axis.text.y=theme_text(size=9, face="bold", hjust=1)) + opts(legend.position = "none") 
+ opts(strip.text.x = theme_text(size=10, face="bold", colour="navyblue"), strip.background = theme_rect(colour="blue", fill="#98CAFF"))

This kinda works but I have two questions: 这种方法有效,但是我有两个问题:

  1. if the number of hosts is not evenly aligned, meaning sometimes, the last row may have less grids than previous rows due to number of hosts may be in odd number. 如果主机的数量不是均匀对齐的,这意味着有时,由于主机的数量可能是奇数,因此最后一行的网格可能比前一行的网格少。 So, last row grid graph have dates but the row before the last row does not have dates. 因此,最后一行网格图具有日期,但最后一行之前的行没有日期。 How do I make sure that the row right before the last row has dates? 如何确保最后一行之前的行有日期?
  2. Since I will be scripting this, some data frames may have greater number of Servers. 由于我将为此编写脚本,因此某些数据帧可能具有更多的服务器。 Depending on the number of of servers, I need to adjusted the png size. 根据服务器的数量,我需要调整png大小。 For example, if the number of Servers is 20, I need height of the image to be 500, width to be 200. Is there a way to do this in ggplot2? 例如,如果服务器数为20,则我需要图像的高度为500,宽度为200。在ggplot2中有没有办法做到这一点? You probably have the idea. 您可能有这个主意。 Other than ggplot2, is there any other way to do this? 除了ggplot2之外,还有其他方法吗?

In regard to question 2, you can use ggsave to save the last produced plot, some pseudocode which bases the plot width on the number of servers: 关于问题2,您可以使用ggsave保存最后生成的图,一些伪代码将图宽度基于服务器数量:

ggplot(...) + 
   etc()
if(noServers > 20) plot_width = 500
ggsave("name.png", width = plot_width)

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

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