Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change a density curve without changing the legend in ggplot2?

I want to change the color of the density curve but when I change it, the border of the boxes from the legend changes too.

Original plot:
image 3

When I change the color of the density curve:
image 2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Also, if I add lwd=1.2 from the density curve, the legend changes too.
image 1

Does anyone know how to fix it?
(I want to change the line width and the colour of the density curve, but I don’t want to change how the legend looks).

This is the code:

val1 <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)

df1 <- data.frame(value = val1)   
df2 <- data.frame(value = val2)   

data <- bind_rows(lst(df1, df2), .id = 'id')

data %>% 
ggplot(aes(value)) +
  geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
  geom_density(lwd = 1.2,
               colour = "red") +
  facet_grid(id ~ .) + 
  scale_x_continuous(breaks=pretty(data$value, n=10)) +
  ggtitle("My histogram....") +
  guides(fill=guide_legend(title="My legend...")) +
  theme(strip.text.x = element_blank(),strip.text.y = element_blank()) 

Thanks very much in advance!

Regards

>Solution :

Add show.legend = FALSE to geom_density()

library(ggplot2)
library(dplyr)

data %>% 
  ggplot(aes(value)) +
  geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
  geom_density(lwd = 1.2, colour = "red", show.legend = FALSE) +
  facet_grid(id ~ .) + 
  scale_x_continuous(breaks=pretty(data$value, n=10)) +
  ggtitle("My histogram....") +
  guides(fill = guide_legend(title="My legend...")) +
  theme(strip.text.x = element_blank(),strip.text.y = element_blank()) 

Created on 2021-12-17 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading