Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

r - Using factor levels with geom_rect

I have factor levels like "node002" "node004" "node006" "node016" "node017" "node018" which are being plotted against dates. I am trying to underlay a given factor level for a period using geom_rect. For the dates, I can do something like ymin=as.Date("8-Apr-2014", format="%d-%b-%Y").

Is it possible to I stipulate, say ymin in terms of the factor levels? If so, how would it be done?

Here is some more information to try to clarify the issue:

The data look like this:

17-Mar-2014 node004 node016 node018 node020 node025 node034 node041
15-Sep-2014 node002 node016 node061 node098
8-Sep-2014 node002 node016 node020 node061
1-Sep-2014 node002 node016 node020 node061
25-Aug-2014 node002 node020 node061
11-Aug-2014 node006 node020
4-Aug-2014 node020
28-Jul-2014 node020
21-Jul-2014 node020
14-Jul-2014 node016 node020 node061
7-Jul-2014 node016 node020 node061
30-Jun-2014 node016 node020 node061
23-Jun-2014 node016 node020 node061
7-Apr-2014 node004 node018 node025 node034 node041 node057
10-Mar-2014 node004 node016 node018 node020 node025 node034 node041
3-Mar-2014 node004 node016 node018 node020 node025 node034 node041
19-Aug-2013 node016 node018 node025 node057 node065
12-Aug-2013 node025 node057 node080
5-Aug-2013 node020 node025 node080
29-Jul-2013 node020 node025 node080
22-Jul-2013 node025 node080
15-Jul-2013 node025 node080
8-Jul-2013 node025
1-Jul-2013 node025
24-Jun-2013 node025
17-Jun-2013 node025
27-May-2013 node057
13-May-2013 node018 node025 node057 node080
6-May-2013 node017 node018 node025 node057
29-Apr-2013 node018
22-Apr-2013 node018 node020 node025 node050 node080
15-Apr-2013 node018 node020 node025 node080
8-Apr-2013 node018 node020 node025 node080
1-Apr-2013 node018 node020 node025 node080
25-Mar-2013 node017 node080
18-Mar-2013 node047
11-Mar-2013 node025
25-Feb-2013 node034
18-Feb-2013 node034
11-Feb-2013 node025 node034 node061
4-Feb-2013 node025 node034 node061
28-Jan-2013 node025 node034 node061
21-Jan-2013 node025 node034 node050 node061
14-Jan-2013 node025 node034 node061
7-Jan-2013 node025 node061*emphasized text*
16-Jun-2014 node016 node020 node061
9-Jun-2014 node016 node020 node061
2-Jun-2014 node016 node020 node061
26-May-2014 node016 node020 node061
19-May-2014 node020 node061
12-May-2014 node016 node020 node061
5-May-2014 node020
28-Apr-2014 node004 node018 node025 node034 node041 node057
21-Apr-2014 node004 node016 node018 node025 node034 node041 node057
14-Apr-2014 node004 node016 node018 node025 node034 node041 node057
31-Mar-2014 node004 node018 node025 node034 node041 node057
24-Mar-2014 node004 node018 node020 node034 node041 node057
17-Feb-2014 node004 node016 node018 node034
10-Feb-2014 node004 node018 node020
3-Feb-2014 node004 node018 node025
27-Jan-2014 node004 node018 node020 node025
20-Jan-2014 node004 node018 node020 node025
13-Jan-2014 node004 node018 node020 node025 node041
6-Jan-2014 node016 node020 node025 node041 node047 node082
30-Dec-2013 node016 node020 node025 node041
23-Dec-2013 node016 node020 node025 node041 node057
16-Dec-2013 node020 node025 node041 node087
9-Dec-2013 node016 node018 node020 node025 node065
2-Dec-2013 node018 node020 node025
25-Nov-2013 node020 node025
18-Nov-2013 node020 node025
11-Nov-2013 node020
4-Nov-2013 node020
28-Oct-2013 node016 node020 node025
21-Oct-2013 node025
14-Oct-2013 node017 node020 node025
7-Oct-2013 node020 node025
30-Sep-2013 node016 node020 node025
23-Sep-2013 node020 node025 node057
16-Sep-2013 node025 node057
9-Sep-2013 node025
2-Sep-2013 node016 node025
26-Aug-2013 node016 node018 node025 node057 node065
18-Aug-2014 node002 node006 node020

The code I have tried looks like this

require(ggplot2)
require(reshape2)
require(scales)

df <- read.table("~/tmp/ipmi_data.txt",fill=T)
flatdata <- melt(df,id.vars="V1")
flatdata <- flatdata[!flatdata$value=="",]
flatdata <- flatdata[order(flatdata$value),]
flatdata$value <- factor(flatdata$value,levels=sort(levels(flatdata$value)))
ggplot(flatdata[flatdata$value != "",]) +
  geom_point(aes(x=value,y=as.Date(V1,format="%d-%b-%Y")),size=3,alpha=0.9) + 
  scale_x_discrete(name="Node") +
  ylab("Date") +
  geom_rect(aes(ymin=as.Date("8-Apr-2014", format="%d-%b-%Y"),
                ymax=as.Date("30-Apr-2014", format="%d-%b-%Y"),
                xmin="node002",xmax="node098"),
                fill="red", alpha=0.25) +
  coord_flip()

The resulting graph has a rectangle across all the factor levels:

Resulting Graph

However, I would like to have individual rectangle for specific levels, such a only for "node004" and "node057" in a given date interval.

So the question is whether is is possible to use the index of a level as a coordinate to define the vertical extent of the rectangle and, if so, how?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got some help after asking the same question on the comp.lang.r.general mailing list (http://permalink.gmane.org/gmane.comp.lang.r.general/313656). I ended up adding the following:

nodelist <- sort(levels(flatdata$value))

and then using

geom_rect(aes(ymin=as.Date("8-Apr-2014", format="%d-%b-%Y"),
            ymax=as.Date("30-Apr-2014", format="%d-%b-%Y"),
            xmin=which(nodelist=="node004")-0.5,
            xmax=which(nodelist=="node004")+0.5,
            fill="red", alpha=0.25))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...