simple plot question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

simple plot question

Maxim-4
Hi,

please excuse the most likely very trivial question, but I'm having no idea
where to find related information:
I try to recapitulate very simple plotting behavior of Excel within R but
have no clue how to get where I want.

I have tab delimited data like

cell treatment value
line a treat1 4
line a treat2 3
line b treat1 8
line b treat2 11


I'd like to have a plot (barplot), that specifies 2 scales on the x-axis
(cell and treatment condition). In future this might become more complex, so
basically I'd like to have a table/matrix as x-axis! Where do I have to look
for working examples, I really spent a lot of time  studying graph
galleries?

Wanted: the same look that you get when marking above data within Excel and
selecting "barplot"!  I have no clue how my search-term should look like in
order to find the necessary information.

The only thing I can get to work is to generate a "second X-axis" at
position 3:
read.delim(file='test')->x
plot(x$value,xaxt="n")
axis(3,1:4,x$treatment)
axis(1,1:4,x$cell)

Not nice, but ok!

Unfortunately this does not work with barplot as the axis does not align
with the bars!
plot(x$value,xaxt="n",beside=T)

Any help is appreciated!
Regards
Maxim

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: simple plot question

Michael Weylandt
Hi Maxim,

I notice no one has replied to you (on list at least) so I'll take a stab at
answering your question and giving some productive advice.

I believe the axis command will do what you want with a little tweaking: It
certainly lines things up for me.

x <-  data.frame(cell=paste("line",c("a","a","b","b")),treat =
paste("treat",c(1,2,1,2)),value=c(4,3,8,11)) # Next time please provide data
that can be directly entered
plot(x$value,xaxt = "n")
axis(1,at=1:4,label=paste(as.character(x$cell),"\n",as.character(x$treat),sep=""))

That said, I'd recommend against it. This sort of data with a
bivariate+categorical x-axis really isn't best viewed in this manner: in
fact, it's not really well-viewed in this manner as well.

Rather, I'd strongly suggest that you use some sort of conditional plotting:
either R's built in coplot() function or (even better) the ggplot2 or
lattice libraries. These two packages are truly outstanding and are both
well-documented on the web, but for just a silly little taste, try this

library(lattice)
x <- data.frame(x1 = sample(1:6,25,replace=T),x2 = sample(1:6,25,replace=T))
x <- data.frame(x, y = x$x1 + x$x2+runif(25)*3)

with(x, xyplot(y~x1|x2))

# Compare to this plot where no information can be gleaned
plot(x$y,xaxt="n")
axis(1,at=1:25,label=paste(x$x1,"\n",x$x2,sep=""))

Hopefully this shows you how the idea of conditioning on an independent
variable can yield a more easily interpreted graph. There's many great
examples of these two packages and I'd highly recommend them for this sort
of plot.

Hope this helps,

Michael Weylandt


On Tue, Aug 9, 2011 at 4:48 PM, Maxim <[hidden email]> wrote:

> Hi,
>
> please excuse the most likely very trivial question, but I'm having no idea
> where to find related information:
> I try to recapitulate very simple plotting behavior of Excel within R but
> have no clue how to get where I want.
>
> I have tab delimited data like
>
> cell treatment value
> line a treat1 4
> line a treat2 3
> line b treat1 8
> line b treat2 11
>
>
> I'd like to have a plot (barplot), that specifies 2 scales on the x-axis
> (cell and treatment condition). In future this might become more complex,
> so
> basically I'd like to have a table/matrix as x-axis! Where do I have to
> look
> for working examples, I really spent a lot of time  studying graph
> galleries?
>
> Wanted: the same look that you get when marking above data within Excel and
> selecting "barplot"!  I have no clue how my search-term should look like in
> order to find the necessary information.
>
> The only thing I can get to work is to generate a "second X-axis" at
> position 3:
> read.delim(file='test')->x
> plot(x$value,xaxt="n")
> axis(3,1:4,x$treatment)
> axis(1,1:4,x$cell)
>
> Not nice, but ok!
>
> Unfortunately this does not work with barplot as the axis does not align
> with the bars!
> plot(x$value,xaxt="n",beside=T)
>
> Any help is appreciated!
> Regards
> Maxim
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: simple plot question

Maxim-4
Hi,

thanks a lot for pointing me at "conditional" plotting!

I have to confess that I'm still not really convinced whether this type of
"philosophy" holds true in each and every situation, especially when there
appears to be a common sense in literature (even if it is not optimal) to
depict such data like requested!

For the conventional plot: thanks for the "newline"-trick!

I will play around with the ggplot library and dive a little deeper into the
literature, perhaps I have looked at the wrong papers!

Best
Maxim



2011/8/10 R. Michael Weylandt <[hidden email]>

> Hi Maxim,
>
> I notice no one has replied to you (on list at least) so I'll take a stab
> at answering your question and giving some productive advice.
>
> I believe the axis command will do what you want with a little tweaking: It
> certainly lines things up for me.
>
> x <-  data.frame(cell=paste("line",c("a","a","b","b")),treat =
> paste("treat",c(1,2,1,2)),value=c(4,3,8,11)) # Next time please provide data
> that can be directly entered
>
> plot(x$value,xaxt = "n")
>
> axis(1,at=1:4,label=paste(as.character(x$cell),"\n",as.character(x$treat),sep=""))
>
> That said, I'd recommend against it. This sort of data with a
> bivariate+categorical x-axis really isn't best viewed in this manner: in
> fact, it's not really well-viewed in this manner as well.
>
> Rather, I'd strongly suggest that you use some sort of conditional
> plotting: either R's built in coplot() function or (even better) the ggplot2
> or lattice libraries. These two packages are truly outstanding and are both
> well-documented on the web, but for just a silly little taste, try this
>
> library(lattice)
> x <- data.frame(x1 = sample(1:6,25,replace=T),x2 =
> sample(1:6,25,replace=T))
> x <- data.frame(x, y = x$x1 + x$x2+runif(25)*3)
>
> with(x, xyplot(y~x1|x2))
>
> # Compare to this plot where no information can be gleaned
> plot(x$y,xaxt="n")
> axis(1,at=1:25,label=paste(x$x1,"\n",x$x2,sep=""))
>
> Hopefully this shows you how the idea of conditioning on an independent
> variable can yield a more easily interpreted graph. There's many great
> examples of these two packages and I'd highly recommend them for this sort
> of plot.
>
> Hope this helps,
>
> Michael Weylandt
>
>
> On Tue, Aug 9, 2011 at 4:48 PM, Maxim <[hidden email]> wrote:
>
>> Hi,
>>
>> please excuse the most likely very trivial question, but I'm having no
>> idea
>> where to find related information:
>> I try to recapitulate very simple plotting behavior of Excel within R but
>> have no clue how to get where I want.
>>
>> I have tab delimited data like
>>
>> cell treatment value
>> line a treat1 4
>> line a treat2 3
>> line b treat1 8
>> line b treat2 11
>>
>>
>> I'd like to have a plot (barplot), that specifies 2 scales on the x-axis
>> (cell and treatment condition). In future this might become more complex,
>> so
>> basically I'd like to have a table/matrix as x-axis! Where do I have to
>> look
>> for working examples, I really spent a lot of time  studying graph
>> galleries?
>>
>> Wanted: the same look that you get when marking above data within Excel
>> and
>> selecting "barplot"!  I have no clue how my search-term should look like
>> in
>> order to find the necessary information.
>>
>> The only thing I can get to work is to generate a "second X-axis" at
>> position 3:
>> read.delim(file='test')->x
>> plot(x$value,xaxt="n")
>> axis(3,1:4,x$treatment)
>> axis(1,1:4,x$cell)
>>
>> Not nice, but ok!
>>
>> Unfortunately this does not work with barplot as the axis does not align
>> with the bars!
>> plot(x$value,xaxt="n",beside=T)
>>
>> Any help is appreciated!
>> Regards
>> Maxim
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.