[R] Create Dot Chart

Joshua Wiley jwiley.psych at gmail.com
Sun Sep 19 22:02:25 CEST 2010


Ahoy Avi,

Sounds like you be wantin' a gander more explanation than 'til you get
your Arrr legs.

# Read in data
# I chose to specify explicit classes because the ids and ESHKOL_tert
# are really categorical data, so I made them factors
# also your data was tab delimited, which I specified using the sep = "\t"
dat <- read.table("http://r.789695.n4.nabble.com/file/n2546047/recmi.txt",
                  header = TRUE, sep = "\t",
                  colClasses = c("factor", "factor", rep("integer", 4))
                  )

# look at the STRucture of the newly created object
# notice the first two things are factors
# and the rest are 'int' or integer class
str(dat)

# load the reshape package for the melt function
# I find this function easier to demonstrate with than
# the reshape() function itself that David mentioned
library(reshape)

# reshape the data into a more amenable form
# I am being explicit in this call, generally
# one would do something much simpler: melt(dat, 1:2) would work
dat2 <- melt(data = dat, id.vars = c("num1", "ESHKOL_tert"),
             measure.vars = c("T1", "T2", "T3", "T4"),
             variable_name = "times")

# melt() calls this form "molten", many people refer to it as
# 'long' versus 'wide'
# traditionally, 'wide' data has one row per person/thing
# 'long' data has one row per time point
# since you had T1 - T4, each id now gets 4 rows
# so the table will be four times as long, checking this:
nrow(dat2) == 4 * nrow(dat)

# load the ggplot2 package to make the graph
library(ggplot2)

# Creating the actual graph
ggplot(data = dat2, aes(x = value, y = num1)) +
  geom_point() +
  facet_grid(ESHKOL_tert ~ ., scales = "free")

# This may seem complicated, but it is fairly straightforward
# ggplot() creates sort of the "base" of the graphic
# it defines what the dataset is and what goes on the X and Y axes
# geom_point() adds points to the graph, I could just as easily have used
# geom_line() to get lines, etc.
# facet_grid() facets the data by level of ESHKOL_tert (so you get 3 panels)
# You might prefer the 'quick' way of plotting using ggplot2

qplot(x = value, y = num1, data = dat2, geom = "point") +
  facet_grid(ESHKOL_tert ~ ., scales = "free")

# for further reading

?read.table # this is how we read the data in
?str # I cannot recommend this function enough
# Assuming the "reshape" package is loaded
?melt
# or for this particular case
?melt.data.frame
# for the graph parts
?ggplot
?qplot


You can also go to Hadley's website:
http://had.co.nz/ggplot2/

He has a book to which I recommend if you will be using ggplot2 a lot.
 Speaking of books, the R website has a list of books on R, and there
are a lot of really nice introductory ones there.
http://www.r-project.org/doc/bib/R-books.html

You can also find and learn a lot online and through the documentation
(like I showed above).

I am not really sure how you wanted to show time, id, the actual
values, and group (ESHKOL) in one plot.  So I did not do anything with
time (T1 - T4).

Cheers,

Makes-a-terrible-pirate-Josh

On Sun, Sep 19, 2010 at 10:49 AM, avsha38 <AVSHALO2 at post.tau.ac.il> wrote:
>
> Hi Josh,
>
> Great samples! Thanks a lot!
> I ran your code and saw the Dot Chart, looks like what I need.
>
> I would like to ask for your help with applying it to my file,
> coming from the Theoretical world making it tough for me to apply in the
> Code..
> attached below is part of my dataset
> num1 is ID variable
> Grouping variable = ESHKOL_tert (factor with 3 levels: 1-3)
> T1 - T4 are the recurrent events (time in months from index for each
> subject)
> http://r.789695.n4.nabble.com/file/n2546047/recmi.txt recmi.txt
> thanks in advance,
> Avi
> --
> View this message in context: http://r.789695.n4.nabble.com/Create-Dot-Chart-tp2545921p2546047.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list