I have two binary-valued columns in foods dataframe as follows:
foods$veryHealthy:
"False" "False" "True" "True" "False" "False" "False" "True" "False" "False" "True" "False"
"True" "False" "False" "True" "False" "True" "False" "False" "True" "False" "False" "False" ...
foods$dairyFree:
"True" "True" "True" "True" "True" "True" "True" "True" "False" "True" "True" "False"
"True" "False" "False" "True" "True" "True" "True" "False" "True" "False" "True" "True" ...
I want to get some plot like:
for the x-axis instead of four categories I want to use binary values.
notice how thickness of bars changes based on the number of instances in each class.
I have tried the following code but it did not get me the result:
ggplot(foods, aes(x=veryHealthy, y=dairyFree, fill=dairyFree)) +
geom_col(position = "fill")
edit: I have to use ggplot2 and not any built-in or other libararies.
I guess you are looking for the package ggmosaic
here you can find the vingette for some examples:
# dummy data
df <- data.frame(veryHealthy = c("False","False","True","True","False","False","False","True","False","False","True","False","True","False","False","True","False","True","False","False","True","False","False","False"),
dairyFree = c("True","True","True","True","True","True","True","True","False","True","True","False","True","False","False","True","True","True","True","False","True","False","True","True"))
library(ggplot2)
library(ggmosaic)
# simple implementation with your dummy data
ggplot2::ggplot(data = df) +
ggmosaic::geom_mosaic(aes(x = product(veryHealthy, dairyFree), fill = veryHealthy ))
EDIT:
maybe this does the work for you (using the same dummy data as before):
plot(table(df))
And here you can find some "manual" aproaches
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.