Translate R basic plot to ggplot2

22 views
Skip to first unread message

Maria....@yahoo.co.uk

unread,
Aug 17, 2011, 9:42:15 AM8/17/11
to ggplot2
Dear ggplot users,

I have created the following graph using basic R commands

A <- subset(Summary, A1 == 0 & A2 == 0)
B <- subset(Summary, A1 >= 1)


A.tmp <- hist(A[,'A1'], breaks = (1,2,3,4,5,10), plot = FALSE)
B.tmp <- hist(B[,A1'], breaks = (1,2,3,4,5,10), plot = FALSE)

plot.new()

plot(A.tmp$counts/sum(A.tmp$counts), type = 'b',
axes = FALSE, ylim = c(0, 0.45), pch = 20, xlim = c(0.5, 4.5), cex =
0.8,
lty = 1, col = 'black)

lines(B.tmp$counts/sum(B.tmp$counts), type = 'b',
axes = FALSE, ylim = c(0, 0.45), pch = 20, xlim = c(0.5, 4.5), cex =
0.8, lty = 2, col = 'red')

axis(1, at = seq(0.5, 5, by = 1), labels = FALSE)
axis(1, at = seq(1, 4, by = 1), labels =
c("1-2","2-3","3-4","4-5","5-10") tick = FALSE)
axis(2)

legend(x = 3, y = 0.1, c("A" , "B"), cex = 0.8, col = c("black",
"red"), lty = c(1,2));

abline(v = 2.5, col = "black")

I made some changes to names so it's clearer. The values in A1 are
integers from 1-300. I tried to do the same with ggplot but keep
getting errors like ggplot does not know how to deal with this data
etc. Is there a way to plot something exactly like in the example
using ggplot? Thanks for help


Maria

Brian Diggs

unread,
Aug 18, 2011, 2:09:36 PM8/18/11
to ggplot2
On 8/17/2011 6:42 AM,
Maria.763294-/E1597aS9LT...@public.gmane.org wrote:
> Dear ggplot users,
>
> I have created the following graph using basic R commands
>
> A<- subset(Summary, A1 == 0& A2 == 0)

> B<- subset(Summary, A1>= 1)

If you give us Summary, you will have a better chance of getting an
answer. Preferably the result of dput(Summary), though possibly only a
subset of that if Summary is large (over 100 rows, say).

> A.tmp<- hist(A[,'A1'], breaks = (1,2,3,4,5,10), plot = FALSE)
> B.tmp<- hist(B[,A1'], breaks = (1,2,3,4,5,10), plot = FALSE)

There is at least a typo here; you are missing a quote before A1.

> plot.new()
>
> plot(A.tmp$counts/sum(A.tmp$counts), type = 'b',
> axes = FALSE, ylim = c(0, 0.45), pch = 20, xlim = c(0.5, 4.5), cex =
> 0.8,
> lty = 1, col = 'black)
>
> lines(B.tmp$counts/sum(B.tmp$counts), type = 'b',
> axes = FALSE, ylim = c(0, 0.45), pch = 20, xlim = c(0.5, 4.5), cex =
> 0.8, lty = 2, col = 'red')
>
> axis(1, at = seq(0.5, 5, by = 1), labels = FALSE)
> axis(1, at = seq(1, 4, by = 1), labels =
> c("1-2","2-3","3-4","4-5","5-10") tick = FALSE)
> axis(2)
>
> legend(x = 3, y = 0.1, c("A" , "B"), cex = 0.8, col = c("black",
> "red"), lty = c(1,2));
>
> abline(v = 2.5, col = "black")
>
> I made some changes to names so it's clearer. The values in A1 are
> integers from 1-300. I tried to do the same with ggplot but keep
> getting errors like ggplot does not know how to deal with this data
> etc. Is there a way to plot something exactly like in the example
> using ggplot? Thanks for help
>
>
> Maria
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Reply all
Reply to author
Forward
0 new messages