geom_rect for category data

588 views
Skip to first unread message

Stefano Costa

unread,
Feb 28, 2010, 11:05:11 AM2/28/10
to ggplot2
Hi,
I'm starting to use ggplot2 for plotting quantitative and
chronological data about archaeological pottery.

One thing I need to do is to plot the time span (in years) for each
single type of pottery ("Tipo" is a factor in my data frame), and I
found that geom_rect is what I should use, because I have xmin
(beginning of production) and xmax values (end of production). The
problem comes out with ymin and ymax.

http://bitbucket.org/steko/pottery-ggplot2/src/d0a1742356f2/durata_forme.r
here is my script, I have tried using

ymin = Tipo, ymax = Tipo + 1
ymin = factor(Tipo), ymax = factor(Tipo)

and others, including the current version

ggplot(tsa) + geom_rect(aes(xmin=cron_inizio, xmax=cron_fine,
ymin=reorder(Tipo, cron_inizio), ymax=reorder(Tipo,cron_inizio),
alpha=1, colour="red", size=2)) + theme_bw()

So far, the only part of this that is working is colour="red" (it
draws the rectangle borders as a single segment), but I need to have a
true rectangle and not a straight line. Data is like the sample at
http://pastebin.com/NCKBzwtX

Thanks,
Stefano

Ben Bolker

unread,
Feb 28, 2010, 2:14:27 PM2/28/10
to Stefano Costa, ggplot2
Does this do what you need?

ggplot(tsa) +
geom_segment(aes(x=cron_inizio, xend=cron_fine,
y=reorder(Tipo, cron_inizio),
yend=reorder(Tipo,cron_inizio)),
alpha=1, colour="red", size=4)+
theme_bw()


--
Ben Bolker
Associate professor, Biology Dep't, Univ. of Florida
bol...@ufl.edu / people.biology.ufl.edu/bolker
GPG key: people.biology.ufl.edu/bolker/benbolker-publickey.asc

signature.asc

Stefano Costa

unread,
Feb 28, 2010, 3:28:44 PM2/28/10
to ggplot2
On 28 Feb, 20:14, Ben Bolker <bol...@ufl.edu> wrote:
>   Does this do what you need?
>
> ggplot(tsa) +
>   geom_segment(aes(x=cron_inizio, xend=cron_fine,
>                    y=reorder(Tipo, cron_inizio),
>                    yend=reorder(Tipo,cron_inizio)),
>                alpha=1, colour="red", size=4)+
>   theme_bw()

Yes, geom_segment is what I need, rather than geom_rect. Now I have to
play with my data and the options a little further, because apparently
different types are plotted on the same "row". Thanks!

Ciao,
Stefano

Stefano Costa

unread,
Mar 1, 2010, 1:32:15 AM3/1/10
to ggplot2
I ended up with just:

tsa <- na.omit(tsa)

ggplot(tsa) +
geom_segment(aes(x=cron_inizio, xend=cron_fine,
y=reorder(Tipo,cron_inizio),
yend=reorder(Tipo,cron_inizio)),
alpha = 1, size=2, fill="#ff0000") +
opts(title="Arco cronologico dei tipi") +
xlab("Cronologia") +
ylab("Tipi") +
theme_bw()

Your suggestion pretty worked good!

Ciao,
Stefano

Reply all
Reply to author
Forward
0 new messages