cmirian
cmirian

Reputation: 2253

Why is a part of this geom_density left out of the plot?

Please, find my data p.plot below

I have made this plot

enter image description here

Unfortunately, it looks like the initial parts of each geom_density around x=0 have been left out. The plot should look like this:

enter image description here

The plot was previously made with help from StackOverflow, where it seemed to be working. However, I have no clue what is wrong with the current code/datamanagement.

My code

cols = c("#1C73C2", "red", "black")

ggplot(p.plot, aes(time.recur.years, colour=ki67in, fill=ki67in)) +
  geom_density(aes(y=..count..), alpha=0.2) + 
  #geom_bar(alpha=0.7) +
  geom_rug(aes(y=0), position=position_jitter(width=0.05, height=0), 
           length=unit(0.05, "npc"), show.legend=FALSE) +
  coord_cartesian(xlim=c(0,11)) +
  scale_x_continuous(name="Years to recurrence", breaks=0:11, expand=c(0,0)) +
  scale_y_continuous(name="Number of recurrences", limits=c(0, 6), breaks=0:6, expand=c(0,0)) +
  scale_colour_manual(values=cols) +
  scale_fill_manual(values=cols) + 
  labs(colour="", fill="") +
  theme_classic() +
  theme(panel.grid.major = element_line(colour = "gray98"),
        panel.grid.minor = element_line(colour = "gray98"),
        legend.position=c(0.7,0.8))

My data

p.plot <- structure(list(time.recur.years = c(0.5, 0.666666666666667, 1, 
1.25, 3.16666666666667, 4.08333333333333, 4.41666666666667, 4.75, 
4.83333333333333, 4.83333333333333, 5, 6.16666666666667, 7.41666666666667, 
7.5, 7.66666666666667, 10.3333333333333, 0.25, 0.333333333333333, 
0.416666666666667, 0.666666666666667, 0.75, 1.41666666666667, 
3.25, 4.5, 9.83333333333333, 0.166666666666667, 0.333333333333333, 
0.583333333333333, 0.666666666666667, 4.66666666666667), ki67in = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("Ki-67 proliferative index: 0 - 4%", 
"Ki-67 proliferative index: 5 - 9%", "Ki-67 proliferative index: ≥10%"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-30L))

Upvotes: 2

Views: 101

Answers (1)

tjebo
tjebo

Reputation: 23757

That seems new in ggplot 3.3.0 - and a new way of not drawing full polygons. I find this actually quite nice!

If you want a full polygon - there is now a new argument outline.type. See also ?geom_density

I also added a row with data for 0, so to draw the polygon really until 0.

library(tidyverse)

df_add <- rbind(p.plot, data.frame(time.recur.years = 0, ki67in =unique(p.plot$ki67in)))

ggplot(df_add, aes(time.recur.years, colour=ki67in, fill=ki67in)) +
  geom_density(aes(y=..count..), alpha=0.2, outline.type = 'full') 

Created on 2020-03-28 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions