You are almost there! You just need to turn the value
into a factor with defined levels.
The data
dlong <- read.table(textConnection("1 1 ZP0.IFN ZP0
2 2 ZP0.IFN
3 3 ZP0.IFN ZP0
4 4 ZP0.IFN
5 5 ZP0.IFN
6 6 ZP0.IFN
7 7 ZP0.IFN
8 8 ZP0.IFN ZP0
9 9 ZP0.IFN
10 10 ZP0.IFN
11 11 ZP0.IFN ZP0
12 12 ZP0.IFN ZP0
13 13 ZP0.IFN
14 14 ZP0.IFN
15 15 ZP0.IFN
16 16 ZP0.IFN ZP0
17 1 IFN ZP2
18 2 IFN ZP21
19 3 IFN
20 4 IFN
21 5 IFN ZP14
22 6 IFN
23 7 IFN ZP14
24 8 IFN ZP21
25 9 IFN ZP21
","r"),fill=NA,na.strings="")[,-1]
colnames(dlong) <- c("Spender","variable","value")
dlong
## Spender variable value
## 1 1 ZP0.IFN ZP0
## 2 2 ZP0.IFN <NA>
## 3 3 ZP0.IFN ZP0
## 4 4 ZP0.IFN <NA>
## 5 5 ZP0.IFN <NA>
## 6 6 ZP0.IFN <NA>
## 7 7 ZP0.IFN <NA>
## 8 8 ZP0.IFN ZP0
## 9 9 ZP0.IFN <NA>
## 10 10 ZP0.IFN <NA>
## 11 11 ZP0.IFN ZP0
## 12 12 ZP0.IFN ZP0
## 13 13 ZP0.IFN <NA>
## 14 14 ZP0.IFN <NA>
## 15 15 ZP0.IFN <NA>
## 16 16 ZP0.IFN ZP0
## 17 1 IFN ZP2
## 18 2 IFN ZP21
## 19 3 IFN <NA>
## 20 4 IFN <NA>
## 21 5 IFN ZP14
## 22 6 IFN <NA>
## 23 7 IFN ZP14
## 24 8 IFN ZP21
## 25 9 IFN ZP21
Convert to factor given defined ranks
## The values are actually ranked data
## (with ZP0 < ZP2 < ZP4 < ZP7 ... ).
require(gtools)
dlong$value <- as.character(dlong$value)
dlong$value <- factor(dlong$value,levels=mixedsort(unique(dlong$value)))
Then it is good!
require(ggplot2)
ggplot(na.omit(dlong),aes(x=Spender, y=value, coulor=variable, group=variable)) +
geom_point(size=2, aes(shape=variable))
Edit:2014/02/12
The 1)-3) questions in your edited post are regarding other aspects of plot/ggplot.
1) This is apparently an issue of the graphics device - either it is not installed or does not start properly. Assuming you have it installed, try the following to check if it will start/popup a device window,
X11() # starts/pops up a X11 graphics device
plot(1:10)
You may get an error, which means R
can not initialize such device. Then you need to use pdf
, png
(or etc.) instead of the popup approach.
2) To keep unused levels, use scale_y_discrete(drop=FALSE)
3) To have all Spender
displayed, convert them to factor similarly as we did for value
. And turn off the drop via scale_x_discrete(drop=FALSE)
.
So continue with The Data above
dlong$value <- as.character(dlong$value)
dlong$value <- factor(dlong$value,
levels=c("ZP0", "ZP2", "ZP4", "ZP7", "ZP14", "ZP21", "ZP28", "ZP35", "ZPM9", "ZPM91"))
dlong$Spender <- factor(dlong$Spender)
ggplot(na.omit(dlong), aes(x=Spender, y=value, group=variable)) +
geom_point(size=3, aes(shape=variable)) +
scale_y_discrete(drop=FALSE)+
scale_x_discrete(drop=FALSE)