Ends of Density Plot

536 views
Skip to first unread message

Dario Strbenac

unread,
May 22, 2014, 2:00:15 AM5/22/14
to ggp...@googlegroups.com
When a density starts or ends at a non-zero value, a vertical line is drawn to the x-axis. There's also always a horizontal line at y = 0 across the range of x axis values. All examples I have seen have this characteristic. How can I only plot the curve of the density ?

Roman Luštrik

unread,
May 22, 2014, 6:07:30 AM5/22/14
to Dario Strbenac, ggplot2
If I understand correctly, you can modify some elements of the theme. See http://docs.ggplot2.org/current/theme.html, for example axis.line.*.


Cheers,
Roman


On Thu, May 22, 2014 at 8:00 AM, Dario Strbenac <dario....@gmail.com> wrote:
When a density starts or ends at a non-zero value, a vertical line is drawn to the x-axis. There's also always a horizontal line at y = 0 across the range of x axis values. All examples I have seen have this characteristic. How can I only plot the curve of the density ?

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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Dario Strbenac

unread,
May 22, 2014, 8:00:22 PM5/22/14
to Roman Luštrik, ggplot2
Thanks. It didn't solve my problem. I used axis.line = element_blank() inside theme(). I attach a picture of what I want to do.

My command is :
ggplot() + geom_density(aes(x = expr, colour = "Poor"), data = poorExprs) + geom_density(aes(x = expr, colour = "Good"), data = goodExprs) + scale_colour_manual("", limits = c("Poor", "Good"), values = c("red", "blue")) + theme(axis.title.x = element_blank(), axis.line = element_blank()) + scale_x_continuous(limits = c(5, 16))

I also have a related question. If I add name = "Custom Title" into scale_colour_manual(), it makes the entire legend disappear. How can a custom title be added to that legend ?
density.png

Dennis Murphy

unread,
May 22, 2014, 8:55:53 PM5/22/14
to Dario Strbenac, ggplot2
Here's a hack solution that overwrites white lines on the existing
plot. The following is what is known as a "reproducible example",
which includes both code and *data*.

library(ggplot2)

# Generate some fake mixture data
DF <- data.frame(grp = factor(rep(c("Good", "Poor"), each = 1000),
levels = c("Poor", "Good")),
y = c(rnorm(250, 13, 2), rnorm(750, 15, 0.5),
rnorm(750, 13, 1), rnorm(250, 15, 0.5)))
# Truncate the upper bound at 16
DF$y <- pmin(DF$y, 16)

ggplot(DF, aes(x = y, color = grp)) + geom_density() +
geom_hline(yintercept = 0, color = "white") +
geom_vline(xintercept = 16, color = "white") +
scale_color_manual("Custom\n title", values = c("darkorange", "blue"))

Note the following:

1. The two geoms that add the horizontal and vertical lines come
*after* geom_density().
2. This should cover your question about legend titles.
3. Overwriting the x-axis may obscure part of the density plot if the
density is sufficiently small over a localized region.

I noticed that you had thicker lines for the density curves. To
accommodate this, I increased the size of the hline and vline as well
as the major axis lines correspondingly - otherwise you'll get
partially uncovered h/vlines:

ggplot(DF, aes(x = y, color = grp)) + geom_density(size = 1) +
geom_hline(yintercept = 0, color = "white", size = 1) +
geom_vline(xintercept = 16, color = "white", size = 1) +
scale_color_manual("Custom\n title", values = c("darkorange", "blue")) +
theme(panel.grid.major = element_line(size = 1))

FYI, the red was changed to dark orange to help out our red-green
color blind friends.

Dennis

Dario Strbenac

unread,
May 25, 2014, 11:00:16 PM5/25/14
to ggp...@googlegroups.com, Dario Strbenac
Thank you. I understand the example. It's surprising that there is no easier way to reproduce the appearance of density plots made with base graphics functions.

Dennis Murphy

unread,
May 26, 2014, 2:55:30 PM5/26/14
to Dario Strbenac, ggplot2
The wraparound is just the way it's defined to be drawn in ggplot2
using geom_density(); I suspect the intention was to easily
accommodate fill colors, particularly when multiple density estimates
are compared in a single graphics panel. One alternative is to use the
stat_density() function instead and use a different geom to render it,
such as geom_path(). Here's a revised (and much less hackish) version
of my previous example:

DF <- data.frame(grp = factor(rep(c("Good", "Poor"), each = 1000),
levels = c("Poor", "Good")),
y = c(rnorm(250, 13, 2), rnorm(750, 15, 0.5),
rnorm(750, 13, 1), rnorm(250, 15, 0.5)))
# Truncate the upper bound at 16
DF$y <- pmin(DF$y, 16)

library(ggplot2)

ggplot(DF, aes(x = y, color = grp)) +
stat_density(aes(y = ..density..), geom = "path", size = 1) +
scale_color_manual("Custom\n title", values = c("darkorange", "blue"))

That should address your present concern.

Dennis

Maihemuti, Kare

unread,
May 30, 2014, 1:43:20 PM5/30/14
to Dario Strbenac, ggp...@googlegroups.com

Hi All,

I have a problem when I use the following code.

I wanted  to compare 4 rootstocks and all the line connected, cannot find starting point and ending point.

 

qplot(Length,data=Density,geom="density",colour=Tube, lwd=0.2,type="o",

                     key=list(x=0.1, y=0.9, corner=c(0.5,0.5)),rep=FALSE,

                              text=list(levels(Density$ube)),         

      xlab="Root length (mm)",

      ylab="Frequency")

 

Cheers,

Mahmut.

Charles Sturt University

|   ALBURY-WODONGA   |   BATHURST   |   CANBERRA   |   DUBBO   |   GOULBURN   |   MELBOURNE   |   ONTARIO   |   ORANGE   |   PORT MACQUARIE   |   SYDNEY   |   WAGGA WAGGA   |


LEGAL NOTICE
This email (and any attachment) is confidential and is intended for the use of the addressee(s) only. If you are not the intended recipient of this email, you must not copy, distribute, take any action in reliance on it or disclose it to anyone. Any confidentiality is not waived or lost by reason of mistaken delivery. Email should be checked for viruses and defects before opening. Charles Sturt University (CSU) does not accept liability for viruses or any consequence which arise as a result of this email transmission. Email communications with CSU may be subject to automated email filtering, which could result in the delay or deletion of a legitimate email before it is read at CSU. The views expressed in this email are not necessarily those of CSU.

Charles Sturt University in Australia The Grange Chancellery, Panorama Avenue, Bathurst NSW Australia 2795 (ABN: 83 878 708 551; CRICOS Provider Number: 00005F (National)). TEQSA Provider Number: PV12018
Charles Sturt University in Ontario 860 Harrington Court, Burlington Ontario Canada L7N 3N4 Registration: www.peqab.ca

Consider the environment before printing this email.

Disclaimer added by CodeTwo Exchange Rules 2007
www.codetwo.com

Dario Strbenac

unread,
Jun 2, 2014, 8:00:15 PM6/2/14
to ggp...@googlegroups.com, dario....@gmail.com, kmaih...@csu.edu.au
Mahmut,

Use the stat_density function instead. Dennis Murphy's example is good. Try it.
Reply all
Reply to author
Forward
0 new messages