Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a density plot with ggplot2 using a factor

Tags:

r

ggplot2

I'm using this data set (at the bottom) to create a density plot, but am having issue with the factor and getting it to aggregate properly. I want the graph to look like this:

ggplot(sample, aes(as.numeric(value), colour=shortname)) + geom_density()

But I want the x-axis to have the actual labels of the factors. But when I use this:

ggplot(sample, aes(value, colour=shortname)) + geom_density()

the graph doesn't aggregate them into the two distinct values of the shortname variable.

What am I doing wrong? I've read about using scale_x_discrete(), but I don't think I should need to since I already have a factor...

UPDATE: Even if I use scale_x_discrete in the following way:

ggplot(sample, aes(value, colour=shortname)) + geom_density() + scale_x_discrete(breaks=1:27, labels=c("<A",LETTERS))

that just removes the x-axis labels all together...

Thank you in advance!

sample <- structure(list(shortname = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1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= structure(c(7L, 17L, 8L, 15L, 
18L, 17L, 14L, 19L, 20L, 17L, 17L, 12L, 16L, 21L, 2L, 21L, 19L, 
22L, 12L, 15L, 22L, 19L, 16L, 13L, 19L, 24L, 15L, 24L, 23L, 12L, 
24L, 21L, 15L, 16L, 16L, 18L, 18L, 8L, 23L, 8L, 21L, 24L, 13L, 
10L, 18L, 1L, 7L, 14L, 13L, 21L, 16L, 10L, 15L, 21L, 17L, 18L, 
18L, 21L, 14L, 9L, 22L, 14L, 11L, 16L, 13L, 18L, 12L, 1L, 23L, 
8L, 15L, 18L, 11L, 10L, 20L, 16L, 12L, 10L, 22L, 25L, 24L, 7L, 
19L, 13L, 16L, 16L, 20L, 3L, 13L, 21L, 12L, 16L, 13L, 15L, 1L, 
19L, 12L, 20L, 12L, 11L, 20L, 7L, 22L, 18L, 19L, 9L, 10L, 24L, 
10L, 13L, 5L, 16L, 19L, 20L, 19L, 18L, 19L, 19L, 13L, 12L, 21L, 
20L, 13L, 21L, 3L, 12L, 19L, 17L, 16L, 9L, 21L, 18L, 24L, 2L, 
12L, 13L, 14L, 7L, 16L, 10L, 21L, 15L, 21L, 11L, 18L, 3L, 16L, 
15L, 22L, 10L, 16L, 21L, 19L, 17L, 20L, 22L, 17L, 20L, 2L, 24L, 
12L, 18L, 19L, 24L, 26L, 17L, 20L, 15L, 12L, 10L, 16L, 12L, 12L, 
15L, 19L, 14L, 22L, 12L, 7L, 16L, 1L, 20L, 18L, 24L, 19L, 22L, 
3L, 16L, 19L, 22L, 5L, 19L, 17L, 16L, 13L, 22L, 3L, 14L, 12L, 
9L, 5L, 16L, 14L, 15L, 12L, 2L, 12L, 19L, 20L, 18L, 10L, 3L, 
20L, 4L, 16L, 19L, 1L, 14L, 24L, 9L, 14L, 1L, 12L, 6L, 1L, 22L, 
11L, 13L, 19L, 16L, 22L, 25L, 3L, 21L, 21L, 22L, 3L, 21L, 18L, 
23L, 24L, 2L, 21L, 15L, 15L, 16L, 11L, 13L, 25L, 11L, 17L, 15L, 
7L, 23L, 21L, 4L, 1L, 14L, 19L, 13L, 10L, 18L, 3L, 13L, 17L, 
12L, 7L, 21L, 17L, 17L, 17L, 17L, 10L, 21L, 24L, 22L, 12L, 22L, 
12L, 24L, 17L, 16L, 21L, 19L, 16L, 16L, 16L, 21L, 13L, 1L, 7L, 
21L, 11L, 13L, 10L, 21L, 11L, 25L, 1L, 11L, 3L, 24L, 13L, 13L, 
15L, 7L, 21L, 16L, 24L, 16L, 8L, 19L, 13L, 18L, 18L, 22L, 19L, 
16L, 16L, 15L, 5L, 4L, 14L, 8L, 15L, 18L, 13L, 14L, 12L, 19L, 
16L, 3L, 16L, 17L, 1L, 19L, 20L, 19L, 1L, 19L, 20L, 22L, 8L, 
12L, 13L, 24L, 16L, 14L, 21L, 25L, 22L, 4L, 16L, 16L, 15L, 16L, 
8L, 14L, 12L, 11L, 5L, 13L, 19L, 27L, 3L, 18L, 12L, 13L, 19L, 
7L, 10L, 15L, 23L, 11L, 3L, 24L, 18L, 15L, 16L, 14L, 16L, 22L, 
11L, 11L, 20L, 18L, 14L, 20L, 21L, 3L, 10L, 19L, 14L, 16L, 8L, 
12L, 16L, 8L, 21L, 26L, 13L, 6L, 9L, 2L, 15L, 1L, 12L, 24L, 3L, 
21L, 24L, 8L, 18L, 20L, 3L, 19L, 12L, 15L, 8L, 18L, 14L, 19L, 
10L, 20L, 17L, 12L, 17L, 19L, 14L, 10L, 7L, 11L, 12L, 3L, 19L, 
1L, 16L, 11L, 8L, 3L, 10L, 15L, 21L, 27L, 3L, 3L, 19L, 5L, 17L, 
22L, 10L, 3L, 15L, 19L, 19L, 18L, 23L, 1L, 22L, 9L, 22L, 19L, 
12L, 18L, 10L, 10L, 9L, 14L, 2L, 27L, 21L, 4L, 18L, 1L, 2L, 16L, 
3L, 21L, 19L, 24L, 12L, 12L, 19L, 13L, 16L, 19L, 20L, 12L, 20L, 
13L, 9L, 15L, 22L, 14L, 5L, 22L, 15L, 3L, 9L, 3L, 12L, 2L, 12L, 
12L, 22L, 15L, 9L, 3L, 21L, 14L, 5L, 5L, 10L, 5L, 5L, 1L, 7L, 
21L, 19L, 22L, 1L, 9L, 1L, 21L, 18L, 15L, 14L, 21L, 6L, 19L, 
15L, 16L, 5L, 5L, 10L, 20L, 5L, 8L, 19L, 3L, 16L, 5L, 7L, 17L, 
16L, 19L, 2L, 20L, 15L, 9L, 17L, 21L, 19L, 13L, 3L, 13L, 12L, 
21L, 16L, 15L, 17L, 16L, 19L, 8L, 17L, 14L, 1L, 1L, 22L, 19L, 
24L, 20L, 10L, 17L, 1L, 17L, 1L, 17L, 13L, 15L, 21L, 6L, 3L, 
18L, 20L, 15L, 4L, 16L, 8L, 12L, 10L, 13L, 13L, 22L, 11L, 12L, 
1L, 21L, 21L, 5L, 5L, 16L, 11L, 20L, 21L, 20L, 21L, 20L, 19L, 
20L, 15L, 25L, 9L, 1L, 12L, 21L, 9L, 24L, 3L, 12L, 24L, 8L, 16L, 
15L, 9L, 20L, 15L, 5L, 10L, 1L, 16L, 16L, 12L, 9L, 20L, 10L, 
19L, 12L, 3L, 20L, 22L, 11L, 16L, 16L, 22L, 19L, 19L, 22L, 14L, 
14L, 12L, 5L, 14L, 19L, 18L, 19L, 18L, 3L, 10L, 20L, 14L, 1L, 
13L, 18L, 13L, 1L, 22L, 23L, 19L, 13L, 18L, 9L, 16L, 15L, 17L, 
21L, 15L, 18L, 1L, 14L, 14L, 1L, 14L, 9L, 16L, 12L, 22L, 14L, 
2L, 22L, 19L, 21L, 16L, 16L, 11L, 19L, 13L, 3L, 16L, 16L, 20L, 
18L, 1L, 19L, 11L, 17L, 19L, 12L, 15L, 10L, 11L, 13L, 7L, 14L, 
14L, 14L, 15L, 15L, 16L, 14L, 22L, 20L, 17L, 19L, 19L, 13L, 16L, 
12L, 15L, 20L, 22L, 17L, 20L, 16L, 10L, 15L, 15L, 12L, 12L, 14L, 
20L, 5L, 19L, 2L, 13L, 15L, 17L, 9L, 14L, 18L, 2L, 10L, 14L, 
12L, 14L, 12L, 18L, 17L, 13L, 8L, 22L, 12L, 21L, 12L, 13L, 3L, 
14L, 26L, 4L, 3L, 1L, 7L, 10L, 19L, 16L, 16L, 15L, 13L, 15L, 
16L, 11L, 21L, 12L, 11L, 15L, 1L, 16L, 1L, 17L, 6L, 1L, 16L, 
7L, 11L, 2L, 5L, 16L, 5L, 12L, 13L, 12L, 13L, 13L, 12L, 20L, 
21L, 21L, 12L, 19L, 21L, 18L, 12L, 15L, 22L, 19L, 16L, 16L, 3L, 
14L, 1L, 7L, 13L, 16L, 11L, 7L, 12L, 16L, 16L, 12L, 22L, 1L, 
13L, 4L, 8L, 16L, 5L, 11L, 10L, 1L, 21L, 10L, 19L, 12L, 13L, 
16L, 12L, 15L, 19L, 13L, 1L, 1L, 2L, 6L, 16L, 14L, 15L, 15L, 
16L, 4L, 12L, 16L, 10L, 19L, 12L, 5L, 6L, 10L, 3L, 14L, 1L, 12L, 
4L, 11L, 16L, 10L, 20L, 4L, 13L, 10L, 1L, 9L, 2L, 7L, 9L, 18L, 
10L, 26L, 14L, 2L, 14L, 10L, 11L, 13L, 1L, 21L, 16L, 9L, 22L, 
12L, 12L, 16L, 15L, 12L, 8L, 15L, 20L, 11L, 16L, 15L, 12L, 12L, 
16L, 2L, 9L, 12L, 14L, 20L, 1L, 10L, 7L, 10L, 18L, 16L, 12L, 
15L, 12L, 14L, 3L, 14L, 6L, 10L, 1L, 11L, 9L, 5L, 12L, 12L, 1L, 
8L, 20L, 7L, 21L, 20L, 22L, 20L, 7L, 12L, 9L, 7L, 13L, 19L, 15L, 
15L, 18L, 16L, 1L, 10L, 19L, 2L, 13L, 6L, 24L, 1L, 22L, 16L, 
11L, 7L, 5L, 19L, 15L, 14L, 12L, 19L, 14L, 12L, 15L, 24L, 15L, 
10L, 4L, 14L, 16L, 3L, 21L, 1L, 19L, 14L, 17L, 12L, 21L, 3L, 
12L, 16L, 18L, 14L, 15L, 15L, 14L, 1L, 2L, 17L, 1L, 14L, 16L, 
15L, 14L, 10L, 14L, 17L, 17L, 12L, 17L, 11L, 14L, 16L, 1L, 1L, 
19L, 12L, 24L, 15L, 19L, 14L, 8L, 3L, 22L, 1L, 16L, 15L, 19L, 
8L, 15L, 12L, 8L, 14L, 8L, 12L, 7L, 13L, 2L, 13L, 10L, 15L, 15L, 
17L, 1L, 26L, 24L, 21L, 25L, 14L, 10L, 13L, 9L, 13L, 18L, 19L, 
16L, 21L, 16L, 17L, 14L, 14L, 11L, 17L, 16L, 12L, 17L, 14L, 6L, 
24L, 11L, 11L, 11L, 12L, 15L, 13L, 22L, 11L, 17L, 3L, 12L, 17L, 
14L, 10L, 11L, 9L, 21L, 18L, 19L, 20L, 24L, 7L, 12L, 22L, 3L, 
17L, 10L, 1L, 20L, 1L, 1L, 12L, 2L, 14L, 2L, 17L, 19L, 1L, 10L, 
12L, 16L, 15L, 3L, 12L, 16L, 12L, 15L, 17L, 24L, 15L, 16L, 8L, 
12L, 14L, 21L, 9L, 23L, 3L, 19L, 16L, 19L, 16L, 16L, 13L, 13L, 
3L, 9L, 17L, 1L, 1L, 16L, 11L, 15L, 7L, 7L, 14L, 8L, 14L, 20L, 
15L, 16L, 1L, 12L, 9L, 16L), .Label = c("<A", "A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", 
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"), class = "factor")), .Names = c("shortname", 
"value"), row.names = c(NA, 1156L), class = "data.frame")
like image 841
Jeff Erickson Avatar asked Dec 24 '12 17:12

Jeff Erickson


People also ask

What is a density plot Ggplot?

A density plot is a representation of the distribution of a numeric variable. It is a smoothed version of the histogram and is used in the same kind of situation. Here is a basic example built with the ggplot2 library. Density Section Density theory. Density plots are built in ggplot2 thanks to the geom_density geom.

How density plot is created?

A density plot is constructed from a numeric variable. A second variable may be used to divide the first variable into groups (e.g., age group or gender). In the two-factor procedure, a third variable may be used to divide the groups into subgroups.


1 Answers

You can get the desired behavior with:

ggplot(sample, aes(value, colour=shortname, group=shortname)) + geom_density()

Note the letter labels at the bottom of the plot, which weren't present with the as.numeric solution:

enter image description here

like image 144
David Robinson Avatar answered Oct 03 '22 08:10

David Robinson