2

So, I've a t-dist plot created in R using curve and adding on the polygons onto that. It gives me a basic looking plot.

What I need is a more good looking plot where

  • X-axis starts from -6
  • Y-axis starts from 0
  • Background of the plot(except under the curve) is filled with some color which I need

I think I need to use the ggplot2 package for this, so answers based on ggplot2 usage is what I need. Or any answer that would return me that output is appreciated.

Here is my code

curve(dt(x, df = 7), from = -6, to = 6)
x <- seq(-1.96, -6, len = 100)
y <- dt(x, 7)
x1 <- seq(1.96, 6, len = 100)
y1 <- dt(x1, 7)

polygon(c(x1[1], x1, x1[100]), c(dt(-6, 7), y1, dt(6, 7)),
        col = "#b14025", border = "black")

polygon(c(x[1], x, x[100]), c(dt(-6, 7), y, dt(6, 7)),
        col = "#b14025", border = "black")

First Image is the current Output

Second Image is what I think it should look like

"Here is the current output"

"Here is what I think it should look like"

Aftab Khan
  • 3,853
  • 1
  • 21
  • 30
  • with `par(bg = 'blue')` you can change the complete background. I guess there are no other ways with standard graphs. But there are ways using for example `ggplot2` package. – drmariod Aug 14 '15 at 08:13
  • oh, sorry, i forgot to mention. I wanted the answer based on ggplot2 only, thats why ive added it among the tags – Aftab Khan Aug 14 '15 at 08:19
  • Oh, then it is easy... `theme(panel.background = element_rect(fill = "black"))` – drmariod Aug 14 '15 at 08:20

3 Answers3

1

Here is one way to obtain a similar result using the ggplot2 package:

library(ggplot2)
dt_tails <- function(x){
 y <- dt(x,7)
 y[abs(x) < 1.96] <- NA
 return(y)
}
dt_7 <- function(x) dt(x,7)
p <- ggplot(data.frame(x=c(-6,6)),aes(x=x)) + 
      stat_function(fun=dt_7, geom="area", fill="white", colour="black") 
p <- p + stat_function(fun=dt_tails, geom="area", fill='#b14025') 
p <- p + theme(panel.grid.major=element_blank(), 
               panel.grid.minor=element_blank(),
               panel.background=element_rect(fill="#eae9c8") )
plot(p)

enter image description here

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • Thanks a ton!! ggplot gives aesthetic looking plots, btw x and y axes arent aligned. I tried using xaxs and yaxs, it doesnt work. Just answer to that, ill use this instead of mine – Aftab Khan Aug 14 '15 at 11:32
  • You are right, there is an offset and I'm not sure how to remove it. Of course one can place a horizontal line at y=0 and a vertical one at x=-6, but it doesn't look good. I keep trying and will let you know if I find a solution. – RHertel Aug 14 '15 at 11:48
0

Since you expected a ggplot answer, just add + theme(panel.background = element_rect(fill = "yellow")) to your plot or what ever color you like.

drmariod
  • 11,106
  • 16
  • 64
  • 110
  • Can u edit to include the code from beginning? As in curve and plot works with the ggplot? I've never used ggplot2 before – Aftab Khan Aug 14 '15 at 08:25
  • Maybe you can adapt by using the following [link](http://stackoverflow.com/questions/12429333/how-to-shade-a-region-under-a-curve-using-ggplot2) – drmariod Aug 14 '15 at 08:27
  • Theme fills into the entire plotting area. I dont want the color to be filled under the curve, apart from the shaded region. Look at the expected output picture ive attached above – Aftab Khan Aug 14 '15 at 08:34
0

I finally managed to do it with the base plotting functions only.

  • For Shading the area outside curve: I just added one more polygon tracing the area outside the curve.

  • For fixing the graph to start at the required X and Y, I used another parameter of plot function xaxs & yaxs from this Link

Here is my attached code

curve(dt(x, df = 7), from = -6, to = 6,xaxs="i",yaxs="i",ylim=c(0,0.4))

t = seq(-6,6,len = 100)
yt = dt(t,7)

x <- seq(-1.96, -6, len = 100)
y <- dt(x, 7)
x1 <- seq(1.96, 6, len = 100)
y1 <- dt(x1, 7)

polygon(x = c(-6,-6,t,6,6),
        y = c(0.4,0,yt,0,0.4),
        col = "#eae9c8",
        border = "black")


polygon(x = c(x1[1], x1, x1[100]),
        y = c(dt(-6, 7), y1, dt(6, 7)),
        col = "#b14025",
        border = "black")

polygon(x = c(x[1], x, x[100]),
        y = c(dt(-6, 7), y, dt(6, 7)),
        col = "#b14025",
        border = "black")

Here is the attached output Final Output

Community
  • 1
  • 1
Aftab Khan
  • 3,853
  • 1
  • 21
  • 30