My dataset looks like this:
library(ggplot2)
N = 10000
totalinc_02 <- rnorm(N)
totalinc_04 <- rnorm(N)
remained_agri <- rbinom(n = N, size = 1, prob = 0.05)
reallocated_manu <- rbinom(n = N, size = 1, prob = 0.05)
inc0204_p_agri <- data.frame(totalinc_02 , totalinc_04 , remained_agri, reallocated_manu)
I have made two kernal density plots as follows:
ggplot(inc0204_p_agri, aes(log(totalinc_02), fill = factor(remained_agri)))
geom_density(alpha = 0.2)
labs(x = "(log) Annual Income in 2001",
y = "Density")
scale_fill_discrete(name="",
breaks=c("1", "0"),
labels=c("Will not reallocate", "Will reallocate"))
ggplot(inc0204_p_agri, aes(log(totalinc_04), fill = factor(reallocated_manu)))
geom_density(alpha = 0.2)
labs(x = "(log) Annual Income in 2003",
y = "Density")
scale_fill_discrete(name="",
breaks=c("0", "1"),
labels=c("Did not reallocate", "Reallocated"))
Producing,
Is there a way that I can make the colour under the density curve for those who reallocated in the second plot correspond to the colour of under the density curve for those who will reallocate in the first plot?
Thank you.
CodePudding user response:
How about this:
library(ggplot2)
N = 10000
totalinc_02 <- rnorm(N)
totalinc_04 <- rnorm(N)
remained_agri <- rbinom(n = N, size = 1, prob = 0.05)
reallocated_manu <- rbinom(n = N, size = 1, prob = 0.05)
inc0204_p_agri <- data.frame(totalinc_02 , totalinc_04 , remained_agri, reallocated_manu)
ggplot(inc0204_p_agri, aes(log(totalinc_02), fill = factor(remained_agri)))
geom_density(alpha = 0.2)
labs(x = "(log) Annual Income in 2001",
y = "Density")
scale_fill_discrete(type = c("blue", "red"),
name="",
breaks=c("1", "0"),
labels=c("Will not reallocate", "Will reallocate"))
#> Warning in log(totalinc_02): NaNs produced
#> Warning in log(totalinc_02): NaNs produced
#> Warning: Removed 5022 rows containing non-finite values (stat_density).
ggplot(inc0204_p_agri, aes(log(totalinc_04), fill = factor(reallocated_manu)))
geom_density(alpha = 0.2)
labs(x = "(log) Annual Income in 2003",
y = "Density")
scale_fill_discrete(type = c("green", "blue"),
name="",
breaks=c("0", "1"),
labels=c("Did not reallocate", "Reallocated"))
#> Warning in log(totalinc_04): NaNs produced
#> Warning in log(totalinc_04): NaNs produced
#> Warning: Removed 5021 rows containing non-finite values (stat_density).