简体   繁体   中英

ggplot plot confusion with facet_wrap and fill

I have the following data frame:

x| y | z | d
1| 0 | A | 0.1
2| 1 | B | 0.01
3| 0 | C | 0.01

Where:

x are random numbers.
y is either 0 or 1.
z is finit model.
d is between 0 and 1.

I want to create facet_wrap using z , and fill it by y using the following code:

ggplot(df, aes(x = x, fill=y)) +
 geom_density(alpha = .5) +
 xlim(c(0,.10)) +
 theme_bw() +
 facet_wrap(~z)

How can plot it ?

Because your fill variable should be a factor or group. Here is a replication of your questions, I used different values for the dataframe:

x <- rnorm(99)
y <- rbinom(99,1,.5)
Z <- rep(c("A", "B","C"), 33)
d <- runif(99)

ggplot(dd) +
  geom_density(alpha = .5, aes(x = x, fill = as.factor(y))) +
  theme_bw() +
  facet_wrap(~z)

I took out xlim because the values are not all in that range, and here is the plot:

情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM