0

The following code plots two partly overlapping density distributions from two independent dataframes with different lenghts.

library(ggplot2)
#Define colors to be used in plot for each group
mycolRO <- rgb(0.8, 0.2, 0, max = 1, alpha = 0.5) #Color for Group "Road"
mycolRA <- rgb(0.2, 0.6, 0.4, max = 1, alpha = 0.5)    #Color for Group "Rail"

#Create some data
dfRoad <- data.frame(DiffRO=2+rnorm(300))
dfRail <- data.frame(DiffRA=rnorm(500))

#Plot density distributions
  ggplot() +
  geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
  geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
  xlim(-6, 6) +
  theme_classic() +
  ggtitle("") +
  xlab("Value") +
  ylab("Density") +
  theme(plot.title = element_text(color="black", size=17, face="bold"),
  axis.title.x = element_text(color="black", size=17, face="bold"),
  axis.title.y = element_text(color="black", size=17, face="bold"),
  axis.text=element_text(size=15))+
  labs(fill = "Group")+
  theme(legend.title = element_text(color = "black", size = 15), legend.text = element_text(color = "black", size=12))+
  theme(legend.position = c(0.2,0.8), legend.direction = "vertical")+
  guides(alpha=FALSE)


The legend does show the correct base color, but not with the transparency (alpha) value defined above, which should be alpha=0.5. Furthermore I would like to see the correct variable names ("DiffRO" and "DiffRA") as legend entries instead of the color codes.

Thanks for any help.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
M. Brink
  • 43
  • 6

1 Answers1

2

Here are two ways of doing what you want.
Common points to both are:

  1. The colors are set manually with scale_fill_manual.
  2. theme calls are simplified, there is no need to call theme repeatedly.

First, I will recreate the data, this time setting the RNG seed before calling rnorm.

set.seed(1234)
dfRoad <- data.frame(DiffRO = 2 + rnorm(300))
dfRail <- data.frame(DiffRA = rnorm(500))

Your way, corrected.

The legend labels must also be set manually in scale_fill_manual.

#Plot density distributions
ggplot() +
  geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
  geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
  xlim(-6, 6) +
  ggtitle("") +
  xlab("Value") +
  ylab("Density") +
  scale_fill_manual(labels = c("Road", "Rail"),
                    values = c(mycolRO, mycolRA)) +
  theme_classic() +
  theme(plot.title = element_text(color="black", size=17, face="bold"),
        axis.title.x = element_text(color="black", size=17, face="bold"),
        axis.title.y = element_text(color="black", size=17, face="bold"),
        axis.text=element_text(size=15),
        legend.title = element_text(color = "black", size = 15), 
        legend.text = element_text(color = "black", size=12),
        legend.position = c(0.2,0.8), legend.direction = "vertical")+
  labs(fill = "Group") +
  guides(alpha = FALSE)

Another way, simpler.

The data is combined and reformated from two different data sets in one data set only. To do this I use package reshape2.

dflong <- reshape2::melt(dfRoad)
dflong <- rbind(dflong, reshape2::melt(dfRail))

Note that now only one call to geom_density is needed and that the legend labels are automatic.

ggplot(dflong, aes(x = value, group = variable, fill = variable, alpha = 0.5)) +
  geom_density() +
  xlim(-6, 6) +
  ggtitle("") +
  xlab("Value") +
  ylab("Density") +
  scale_fill_manual(values = c(mycolRA, mycolRO)) +
  theme_classic() +
  theme(plot.title = element_text(color="black", size=17, face="bold"),
        axis.title.x = element_text(color="black", size=17, face="bold"),
        axis.title.y = element_text(color="black", size=17, face="bold"),
        axis.text = element_text(size=15),
        legend.title = element_text(color = "black", size = 15), 
        legend.text = element_text(color = "black", size=12),
        legend.position = c(0.2,0.8), legend.direction = "vertical") +
  labs(fill = "Group") +
  guides(alpha = FALSE)

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • @ Rui Barradas: Thanks for the hint with scale_fill_manual. This helped me a lot! However, I had to slightly change the scale_fill_manual statement to produce the desired result to: scale_fill_manual(labels = c("Road", "Rail"), values = adjustcolor(c(mycolRO, mycolRA), alpha.f=0.33)) – M. Brink Jan 23 '20 at 15:25