library(ComplexUpset)
library(AER)
## Загрузка требуемого пакета: car
## Загрузка требуемого пакета: carData
## Загрузка требуемого пакета: lmtest
## Загрузка требуемого пакета: zoo
## 
## Присоединяю пакет: 'zoo'
## Следующие объекты скрыты от 'package:base':
## 
##     as.Date, as.Date.numeric
## Загрузка требуемого пакета: sandwich
## Загрузка требуемого пакета: survival
library(ggplot2)
## Warning: пакет 'ggplot2' был собран под R версии 4.2.1
data("DoctorVisits")

summary(DoctorVisits)
##      visits          gender          age             income      
##  Min.   :0.0000   male  :2488   Min.   :0.1900   Min.   :0.0000  
##  1st Qu.:0.0000   female:2702   1st Qu.:0.2200   1st Qu.:0.2500  
##  Median :0.0000                 Median :0.3200   Median :0.5500  
##  Mean   :0.3017                 Mean   :0.4064   Mean   :0.5832  
##  3rd Qu.:0.0000                 3rd Qu.:0.6200   3rd Qu.:0.9000  
##  Max.   :9.0000                 Max.   :0.7200   Max.   :1.5000  
##     illness         reduced            health       private    freepoor  
##  Min.   :0.000   Min.   : 0.0000   Min.   : 0.000   no :2892   no :4968  
##  1st Qu.:0.000   1st Qu.: 0.0000   1st Qu.: 0.000   yes:2298   yes: 222  
##  Median :1.000   Median : 0.0000   Median : 0.000                        
##  Mean   :1.432   Mean   : 0.8619   Mean   : 1.218                        
##  3rd Qu.:2.000   3rd Qu.: 0.0000   3rd Qu.: 2.000                        
##  Max.   :5.000   Max.   :14.0000   Max.   :12.000                        
##  freerepat  nchronic   lchronic  
##  no :4099   no :3098   no :4585  
##  yes:1091   yes:2092   yes: 605  
##                                  
##                                  
##                                  
## 
#private - Does the individual have private health insurance?
#freepoor - Does the individual have free government health insurance due to low income?
#freerepat - Does the individual have free government health insurance due to old age, disability or veteran status?
#nchronic - Is there a chronic condition not limiting activity?
#lchronic - Is there a chronic condition limiting activity?

data1 <- DoctorVisits[,c(2,8:12)]
data1$gender <- ifelse(data1$gender == "male", 1, 0)
data1$private <- ifelse(data1$private == "yes", 1, 0)
data1$freepoor <- ifelse(data1$freepoor == "yes", 1, 0)
data1$freerepat <- ifelse(data1$freerepat == "yes", 1, 0)
data1$nchronic <- ifelse(data1$nchronic == "yes", 1, 0)
data1$lchronic <- ifelse(data1$lchronic == "yes", 1, 0)

columns <- colnames(data1)

upset(data1, columns,  base_annotations = list("Intersection size" = intersection_size(fill = "purple4")), 
      queries = list(upset_query(set = 'gender', fill = 'palegreen4'),
                     upset_query(set = 'private', fill = '#693450'), 
                     upset_query(set = 'nchronic', fill = '#ca540c'), 
                     upset_query(set = 'freerepat', fill = '#ffef03'), 
                     upset_query(set = 'lchronic', fill = '#285678'), 
                     upset_query(set = 'freepoor', fill = '#5d3277')), 
      matrix=(intersection_matrix(geom=geom_point(shape='circle filled', size=3)) + scale_color_manual(values=c('gender'='palegreen4', 'private'='#693450', 'nchronic'='#ca540c', 'freerepat'='#ffef03', 'lchronic'='#285678', 'freepoor'='#5d3277'))))