Question

I am trying to build a simple scatter plot: This is what my data looks like (dlong):

     Spender variable value  
1       1      IFN     2  
2       2      IFN     6  
3       3      IFN     5   
4       1      iL2  <NA>  
5       2      iL2     4  
6       3      iL2     8  

which I originally melt from my dataframe

Since I am a selftaught, I made it so far:

  ggplot(na.omit(dlong), aes(x=Spender, y=value, coulor=variable, group=variable)) +
      geom_point(size=2, aes(shape=variable))

resulting in a graph

reaction to proteins

with enumerated test persons on the x axis, with reactions to different time points (=value) (y-axis). These reactions differ by the typ of proteins (=groups = variable).

My real data looks aktually like this:

    Spender variable value  
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  

The values are actually ranked data
(with ZP0 < ZP2 < ZP4 < ZP7 ... ).

And here is my question. How can I implement ranked data onto the y axis?

What I did next:

dlong$value <- factor(dlong$value,   
levels=c("ZP0", "ZP2", "ZP4", "ZP7", "ZP14", "ZP21", "ZP28", "ZP35", "ZPM9", "ZPM91"))  
ggplot(na.omit(dlong), aes(x=Spender, y=value, group=variable)) +  
geom_point(size=3, aes(shape=variable))    
  1. Using R it wont open me the plot: so I added >png(filename="") with dev.off in front off and behind. Why wont the window wont pop up? new scatter

  2. on the y axis it wont show me all requirde time points - it leaves also real points out (eg. Spender 1, iL2, ZP4)

  3. How can I have marks on x axis for each "Spender" with white thick lines in the grey field?

Was it helpful?

Solution

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))

enter image description here

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) 

enter image description here

OTHER TIPS

You could provide the ranks instead of the values:

dlong$dummy <- as.numeric(gsub("ZP","",as.character(dlong$value)))

ggplot(na.omit(dlong), aes(x=Spender, y=dummy, colour=variable, group=variable)) + geom_point(size=2, aes(shape=variable))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top