annotate "rect" in new ggplot2

1,229 views
Skip to first unread message

Johannes Radinger

unread,
Mar 27, 2012, 6:35:00 AM3/27/12
to ggp...@googlegroups.com
Hi,

in an older script I used following lines to expand the y-axis and to add a
grey rectangle manually to a plot (to make a similar look like facett plots).
The following lines were working before (I can't remember if in the dev-mode or the normal mode) but in the new version, no rectangle is plotted...

y <- runif(1000,1,50000)
x <- runif(1000,1,10)

df <- data.frame(x,y)
yrange <- c(0.01,1000000)


ggplot(df, aes(x=x,y=y))+
geom_point()+
scale_y_continuous("Y-Axis",breaks=c(0.1,1,10,100,1000,10000),labels=c(0.1,1,10,100,1000,10000),limits=yrange,trans="log")+
scale_x_continuous("X Axis",limits=range(df$x),trans="log")+
annotate("rect", xmin=0, xmax=10000, ymin=200000, ymax= 50000000, fill="grey80")+
annotate("text", 3 , 700000, label="X-Axis",size=4) +
theme_bw()+
opts(legend.position = "none",
axis.title.y = theme_blank(),
axis.ticks = theme_blank(),
axis.title.x = theme_blank())


Does anyone know how to do that?

/johannes
--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

Kohske Takahashi

unread,
Mar 27, 2012, 6:47:03 AM3/27/12
to Johannes Radinger, ggp...@googlegroups.com
Hi

I think this may be a bug, but if the x/y-min/max is out-of-range, the
the object will not be drawn.
try this:

...
annotate("rect", xmin=2, xmax=8, ymin=10, ymax= 50000, fill="grey80")+
...


2012年3月27日19:35 Johannes Radinger <JRad...@gmx.at>:


> y <- runif(1000,1,50000)
> x <- runif(1000,1,10)
>
> df <- data.frame(x,y)
> yrange <- c(0.01,1000000)
>
>
> ggplot(df, aes(x=x,y=y))+
> geom_point()+
> scale_y_continuous("Y-Axis",breaks=c(0.1,1,10,100,1000,10000),labels=c(0.1,1,10,100,1000,10000),limits=yrange,trans="log")+
> scale_x_continuous("X Axis",limits=range(df$x),trans="log")+
> annotate("rect", xmin=0, xmax=10000, ymin=200000, ymax= 50000000, fill="grey80")+
> annotate("text", 3 , 700000, label="X-Axis",size=4) +
> theme_bw()+
> opts(legend.position = "none",
> axis.title.y = theme_blank(),
> axis.ticks = theme_blank(),
> axis.title.x = theme_blank())

--
--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Johannes Radinger

unread,
Mar 27, 2012, 7:07:47 AM3/27/12
to Kohske Takahashi, ggp...@googlegroups.com
> Hi
>
> I think this may be a bug, but if the x/y-min/max is out-of-range, the
> the object will not be drawn.
> try this:
>
> ...
> annotate("rect", xmin=2, xmax=8, ymin=10, ymax= 50000,
> fill="grey80")+
> ...
>

Yes it seems that there is a bug with rectangles that exceed the max values. The problem is that I need a rect that reaches the upper and right border of my plot. Any suggestion how to do that? Or will that bug be fixed in the next time?

/johannes

>
> 2012年3月27日19:35 Johannes Radinger <JRad...@gmx.at>:
> > y <- runif(1000,1,50000)
> > x <- runif(1000,1,10)
> >
> > df <- data.frame(x,y)
> > yrange <- c(0.01,1000000)
> >
> >
> > ggplot(df, aes(x=x,y=y))+
> > geom_point()+
> >
> scale_y_continuous("Y-Axis",breaks=c(0.1,1,10,100,1000,10000),labels=c(0.1,1,10,100,1000,10000),limits=yrange,trans="log")+
> > scale_x_continuous("X
> Axis",limits=range(df$x),trans="log")+
> > annotate("rect", xmin=0, xmax=10000, ymin=200000, ymax=
> 50000000, fill="grey80")+
> > annotate("text", 3 , 700000, label="X-Axis",size=4) +
> > theme_bw()+
> > opts(legend.position = "none",
> > axis.title.y = theme_blank(),
> > axis.ticks = theme_blank(),
> > axis.title.x = theme_blank())
>
>
>
> --
> --
> Kohske Takahashi <takahash...@gmail.com>
>
> Research Center for Advanced Science and Technology,
> The University of Tokyo, Japan.
> http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
>

> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Kohske Takahashi

unread,
Mar 27, 2012, 7:28:41 AM3/27/12
to Johannes Radinger, ggp...@googlegroups.com
No, this would not be a bug.
For that purpose, you would better to set limits in coord_...

ggplot(df, aes(x=x,y=y))+
geom_point()+
scale_y_continuous("Y-Axis",breaks=c(0.1,1,10,100,1000,10000),labels=c(0.1,1,10,100,1000,10000),trans="log")+
scale_x_continuous("X Axis",trans="log", breaks = c(2, 4, 6, 8))+


annotate("rect", xmin=0, xmax=10000, ymin=200000, ymax=
50000000, fill="grey80")+
annotate("text", 3 , 700000, label="X-Axis",size=4) +
theme_bw()+
opts(legend.position = "none",
axis.title.y = theme_blank(),
axis.ticks = theme_blank(),

axis.title.x = theme_blank()) +
coord_cartesian(xlim = range(df$x), ylim = yrange, wise = TRUE)

2012年3月27日20:07 Johannes Radinger <JRad...@gmx.at>:

Johannes Radinger

unread,
Mar 27, 2012, 8:32:02 AM3/27/12
to Kohske Takahashi, ggp...@googlegroups.com
Thank you very much...

setting coord_cartesian in you example was what I was looking for...
...everything is working fine now!

cheers,
johannes

-------- Original-Nachricht --------
> Datum: Tue, 27 Mar 2012 20:28:41 +0900
> Von: Kohske Takahashi <takahash...@gmail.com>
> An: Johannes Radinger <JRad...@gmx.at>
> CC: ggp...@googlegroups.com
> Betreff: Re: annotate "rect" in new ggplot2

Johannes Radinger

unread,
Apr 24, 2012, 10:51:39 AM4/24/12
to Johannes Radinger, takahash...@gmail.com, ggp...@googlegroups.com
Hi,

I just want to reopen an issue we had some weeks ago about extending the plot and drawing rectangles etc. (see correspondence below)
Therefore Takahashi Kohske helped me by using coord_cartesian.
This worked for nearly all of my plots but now I realised that there
is still a little problem with one plot where my x-axis disappeared.
So I tried to find the problem and found it. Here small
example to reproduce it:


y <- runif(1000,0.01,50000)
x <- runif(1000,39,810)
df <- data.frame(x,y)

plot.length <- ggplot(df, aes(x=x,y=y))+
geom_point()+
scale_x_continuous(breaks=c(100,200,400,800),trans="log")+
annotate("rect", xmin=0, xmax=10000, ymin=80000, ymax=50000000, fill="blue")+
coord_cartesian(xlim = range(df$x), ylim = c(0.01,100000), wise = TRUE)


The problem with the missing x-axis seems to be related to the xlim parameter and the log-scale. Both together don't work in my case. The reason why it worked for my other plots (also with log scale) was related to different xlims. E.g. if I try to set xlim = c(1,810) it works.

Any suggestions how to solve that problem?

Best regards,
Johannes



-------- Original-Nachricht --------
> Datum: Tue, 27 Mar 2012 14:32:02 +0200
> Von: "Johannes Radinger" <JRad...@gmx.at>
> An: Kohske Takahashi <takahash...@gmail.com>
Ihr GMX Postfach immer dabei: die kostenlose GMX Mail App für Android.
Komfortabel, sicher und schnell: www.gmx.de/android

Johannes Radinger

unread,
May 4, 2012, 8:46:15 AM5/4/12
to Johannes Radinger, takahash...@gmail.com, ggp...@googlegroups.com
Hi,

I just wanted to ask again if there is any workaround for
the problem mentioned above? It was working in the dev-version
some weeks ago but it isn't working in the actual version.

best regards,

johannes

-------- Original-Nachricht --------
> Datum: Tue, 24 Apr 2012 16:51:39 +0200
> Von: "Johannes Radinger" <JRad...@gmx.at>
> An: "Johannes Radinger" <JRad...@gmx.at>, takahash...@gmail.com

Winston Chang

unread,
May 5, 2012, 4:06:54 PM5/5/12
to Johannes Radinger, takahash...@gmail.com, ggp...@googlegroups.com
You can set the limits without clipping the data if you use scale_x_continuous with `oob=rescale_none`:


library(ggplot2)

y <- runif(1000,0.01,50000)
x <- runif(1000,39,810)
df <- data.frame(x,y)

library(scales)
ggplot(df, aes(x=x,y=y))+
  geom_point()+
  scale_x_continuous(breaks=c(100,200,400,800), trans="log", 
    limits = range(df$x), oob=rescale_none) +
  scale_y_continuous(limits = c(0.01,100000), oob = rescale_none) +
  annotate("rect", xmin=0, xmax=10000, ymin=80000, ymax=50000000, fill="blue")

Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

Johannes Radinger

unread,
May 6, 2012, 2:49:26 AM5/6/12
to Winston Chang, takahash...@gmail.com, ggp...@googlegroups.com
Hi Winston,

thank you so much for helping me. Now everything works fine, and
the plots look really great!

Best regards,
Johannes
Reply all
Reply to author
Forward
0 new messages