Gundala Viswanath
2008-Jun-16 06:25 UTC
[R] Superimposing Line over Histogram in Density Plot
Hi,
Currently I have a density plot generated with this
snippet.
Is there a way I can add a line curve on top of it?
I mean in one figure
__BEGIN__
myhist <- hist(x
col="blue",
main = "Density Plot",
xlab = "Exp Level",
)
__END__
- Gundala Viswanath
Jakarta - Indonesia
Dimitris Rizopoulos
2008-Jun-16 07:05 UTC
[R] Superimposing Line over Histogram in Density Plot
try something like this:
x <- rnorm(200)
hist(x, col = "blue", freq = FALSE)
lines(density(x), col = "red", lwd = 2)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Gundala Viswanath" <gundalav at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, June 16, 2008 8:25 AM
Subject: [R] Superimposing Line over Histogram in Density Plot
> Hi,
>
> Currently I have a density plot generated with this
> snippet.
>
> Is there a way I can add a line curve on top of it?
> I mean in one figure
>
>
> __BEGIN__
> myhist <- hist(x
> col="blue",
> main = "Density Plot",
> xlab = "Exp Level",
> )
> __END__
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> R-help at r-project.org 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Daren Tan
2008-Jun-16 12:10 UTC
[R] Very puzzled why swapping the columns doesn't swap the column names
How can I swap the column names at the same time ?> m <- cbind(x=1:3, y=2:4, z=3:5)> m x y z[1,] 1 2 3[2,] 2 3 4[3,] 3 4 5 > m[,c(1,2)] <- m[,c(2,1)]> m x y z[1,] 2 1 3[2,] 3 2 4[3,] 4 3 5_________________________________________________________________ [[alternative HTML version deleted]]
Look here: http://msenux.redwoods.edu/mathdept/R/CentralLimit.php D. On Jun 15, 2008, at 11:25 PM, Gundala Viswanath wrote:> Hi, > > Currently I have a density plot generated with this > snippet. > > Is there a way I can add a line curve on top of it? > I mean in one figure > > > __BEGIN__ > myhist <- hist(x > col="blue", > main = "Density Plot", > xlab = "Exp Level", > ) > __END__ > > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-help at r-project.org 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.
I do it a bit differently: Once you've got myhist, you can plot a line on the existing histogram plot with something like: smoo<-spline(myhist$breaks[1:length(myhist$breaks)-1],myhist$counts) lines(smoo$x,smoo$y,col='green') You'll have to muck a bit with scale factors to make the spline curve "match up" with the histogram plot's actual x-axis values. (actually, that snippet is part of my conversion from histogram data to a bar chart that "looks" like a histogram, with the spline curve overlaid). Carl try something like this: x <- rnorm(200) hist(x, col = "blue", freq = FALSE) lines(density(x), col = "red", lwd = 2) I hope it helps. Best, Dimitris > > Currently I have a density plot generated with this > snippet. > > Is there a way I can add a line curve on top of it? > I mean in one figure > > > __BEGIN__ > myhist <- hist(x > col="blue", > main = "Density Plot", > xlab = "Exp Level", > ) > __END__ > >