2017-12-09 4 views
2

샘플 데이터는 여기ggplot2면 - 강조 일치하는 열은

내가 ggplot를 사용하여이 그래프를 플롯하고이

brand product 
1  MS  OS 
2 Google Search 
3 Apple Iphone 
4  MS Search 
5  FB Network 
6 Apple  OS 
7 Oracle  DB 

문제 문처럼 보이는

brand=c('MS', 'Google', 'Apple', 'MS', 'FB', 'Apple', 'Oracle') 
product=c('OS', 'Search', 'Iphone', 'Search', 'Network', 'OS', 'DB') 
df= data.frame(brand, product) 

설정 코드

,210
library(dplyr) 
library(ggplot2) 
df %>% 
    group_by(brand) %>% 
    count(product, sort = TRUE) %>% 
    ggplot(aes(product, n)) + 
     geom_col() + 
     facet_wrap(~brand, ncol = 5, scales = "free_x") + 
     coord_flip() 
그것이 'MS'과 'Appple'에서 일반적 모두 '구글'과 'MS'와 'OS'에 일반적입니다 이제 우리는 '검색'볼 수있는이

같은 그래프를

줄 것이다 , 내가 원하는 두 막대를 모두 강조 표시하지만 어떻게 할 수 있는지 모르겠 으면 ggplot이 아닌 다른 그래픽 인터페이스로 가능하면 알려주십시오.

enter image description here

답변

1

일반적인 원칙을 사용하면 색상 정보를 보유하고 열을 프레임 데이터에 추가해야한다는 것입니다 just like @PoGibas did., 당신은을 포함하여, 당신이 원하는대로 접근하여 그릴 수 있습니다 선택한 geom_col() 접근 방식입니다.

brand=c('MS', 'Google', 'Apple', 'MS', 'FB', 'Apple', 'Oracle') 
product=c('OS', 'Search', 'Iphone', 'Search', 'Network', 'OS', 'DB') 
df= data.frame(brand, product) 

# Define color  
df$myColor <- NA 
foo <- df$product %in% names(which(table(df$product) > 1)) 
df$myColor[foo] <- df$product[foo] 

df 
# brand product myColor 
#1  MS  OS  4 
#2 Google Search  5 
#3 Apple Iphone  NA 
#4  MS Search  5 
#5  FB Network  NA 
#6 Apple  OS  4 
#7 Oracle  DB  NA 

원래 플로팅 코드는 약간 수정되어 있습니다. 각 공급 업체에 대해 각 제품을 한 번만 사용하는 경우 추가 코드를 추가하지 말고 aes() 문에 y = 1을 설정하는 것이 좋습니다.

enter image description here

library(dplyr) 
library(ggplot2) 
ggplot(df, aes(x = product, y = 1, fill = factor(myColor))) + 
    geom_col() + 
    facet_wrap(~brand, ncol = 5, scales = "free_x") + 
    guides(fill = "none") + 
    coord_flip() 
그리고 지금 우리는 주제로 일을 더 잘 볼 수 있도록 조금 주위를 재생할 수 있습니다. 당신이 사용하거나 geom_point()을 @PoGibas에 의해 제안하지만, geom_tile() 그러나

enter image description here

ggplot(df, aes(x = product, y = 1, fill = factor(myColor))) + 
    geom_col() + 
    facet_wrap(~brand, ncol = 5) + 
    guides(fill = "none") + 
    scale_y_continuous(breaks = NULL, name = "") + 
    scale_x_discrete(expand = c(0, 0)) + 
    coord_flip() + 
    theme(panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     axis.ticks = element_blank(), 
     axis.text = element_text(color = "black"), 
     strip.background = element_blank(), 
     panel.background = element_rect(fill="gray95", color="gray95"), 
     panel.spacing = unit(0, "pt")) 
,이 응용 프로그램에 대한 올바른 기하 구조는, 내 마음에,도 geom_col()입니다 :

ggplot(df, aes(x = brand, y = product, fill = factor(myColor))) + 
    geom_tile(width = 0.9, height = 0.9) + 
    guides(fill = "none") + 
    scale_x_discrete(name = "", position = "top") + 
    scale_y_discrete(name = "") + 
    theme(panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     axis.ticks = element_blank(), 
     axis.text = element_text(color = "black"), 
     panel.background = element_rect(fill="gray95")) 

enter image description here

geom_tile()을 사용하면 채워진 영역의 높이와 너비, 그리고 플롯의 크기를 조정하거나 가로 세로 비율을 변경하면 이상한 놀라움을 느끼지 않을 것입니다.

+0

설명 해 주셔서 감사합니다. 훌륭한 답변 ... – Vineet

2

이 문제에 대한 나의 접근 방식 : 대신 측면의 geom_point()를 사용하고 동일한 제품을 여러 번있을 경우 색상을 지정 (또한 약간의 시각적 인 비틀기를 추가). 당신이 열이 나면

library(ggplot2) 

# Define color  
df$myColor <- NA 
foo <- df$product %in% names(which(table(df$product) > 1)) 
df$myColor[foo] <- df$product[foo] 

ggplot(df, aes(brand, product, color = myColor)) + 
    geom_point(shape = 15, size = 20) + 
    scale_color_brewer(guide = FALSE, palette = "Dark2", na.value = "grey") + 
    labs(x = NULL, 
     y = NULL) + 
    theme_classic() + 
    theme(axis.ticks = element_blank(), 
      axis.text = element_text(size = 15, color = "black")) 

enter image description here

+0

대단히 고마워요. 저에게 도움이 될 것입니다. – Vineet

+0

@PoGibas 코드가 게시 된대로 실행되지 않습니다 (오류 : 연속 된 값이 개별 눈금에 제공됨).'color = myColor'를'color = factor (myColor)'로 대체해야합니다. –