R - Create a scatter plot from a data frame

I have a data frame allthat looks like this:

http://pastebin.com/Xc1HEYyH

Now I want to create a scatter plot with the column headings on the x axis and the corresponding values ​​as data points. For instance:

7|                 x  
6|          x      x  
5|  x       x      x     x    
4|  x       x            x 
3|                             x      x  
2|                             x      x
1|
 ---------------------------------------
    STM    STM    STM   PIC   PIC    PIC
   cold   normal  hot  cold  normal  hot

It should be easy, but I can’t figure out how to do it.

Hi

+5
source share
3 answers

The basic idea, if you want to build using Hadley ggplot2, is to get your form data:

        x          y
col_names     values

And this can be done using a function meltfrom Hadley reshape2. Do ?meltto see possible arguments. However, since we want to melt the entire data.frame, we just need to,

melt(all) 
# this gives the data in format:
#   variable value
# 1 STM_cold   6.0
# 2 STM_cold   6.0
# 3 STM_cold   5.9
# 4 STM_cold   6.1
# 5 STM_cold   5.5
# 6 STM_cold   5.6

x variable, y value.

require(ggplot2)
require(reshape2)
ggplot(data = melt(all), aes(x=variable, y=value)) + 
             geom_point(aes(colour=variable))

, aes(colour=variable) geom_point, geom_point().

enter image description here

: , , , geom_point geom_jitter, , , :

enter image description here

+8

. dotplot "":

library(lattice)
dotplot(values ~ ind, data = stack(all))

enter image description here

dotchart R- . dotchart, data.frame as.matrix:

dotchart(as.matrix(all), labels = "")

, "", , . - , - . , , 16 . . , "STM_cold" NA, , .

, , , , .

enter image description here

+5

, R .

:

test <- read.table(text="STM_cold STM_normal STM_hot PIC_cold PIC_normal PIC_hot
6.0 6.6 6.3 0.9 1.9 3.2
6.0 6.6 6.5 1.0 2.0 3.2
5.9 6.7 6.5 0.3 1.8 3.2
6.1 6.8 6.6 0.2 1.8 3.8
5.5 6.7 6.2 0.5 1.9 3.3
5.6 6.5 6.5 0.2 1.9 3.5
5.4 6.8 6.5 0.2 1.8 3.7
5.3 6.5 6.2 0.2 2.0 3.5
5.3 6.7 6.5 0.1 1.7 3.6
5.7 6.7 6.5 0.3 1.7 3.6
NA  NA  NA  0.1 1.8 3.8
NA  NA  NA  0.2 2.1 4.1
NA  NA  NA  0.2 1.8 3.3
NA  NA  NA  0.8 1.7 3.5
NA  NA  NA  1.7 1.6 4.0
NA  NA  NA  0.1 1.7 3.7",header=TRUE)

:

plot(
     NA,
     ylim=c(0,max(test,na.rm=TRUE)+0.3),
     xlim=c(1-0.1,ncol(test)+0.1),
     xaxt="n",
     ann=FALSE,
     panel.first=grid()
     )

axis(1,at=seq_along(test),labels=names(test),lwd=0,lwd.ticks=1)

Select multiple dots with some x jittering axis so that they do not print on top of each other.

invisible(
  mapply(
        points,
        jitter(rep(seq_along(test),each=nrow(test))),
        unlist(test),
        col=rep(seq_along(test),each=nrow(test)),
        pch=19
        )
)

Result:

enter image description here

Edit

Here's an example of using alpha transparency in dots and getting rid of it jitter, as described in the following comments with Ananda.

invisible(
  mapply(
        points,
        rep(seq_along(test),each=nrow(test)),
        unlist(test),
        col=rgb(0,0,0,0.1),
        pch=15,
        cex=3
        )
)

enter image description here

+3
source

All Articles