2017-06-26 27 views
1

已經看到herehere但仍然出錯。 我想按客戶類別標註我的比杆:基於第三個變量的Barplot ggplot2中的標籤數據

這裏是dput()輸出:

structure(list(word = c("broadband", "broadband", "broadband", 
"call", "call", "call", "cheaper", "cheaper", "cheaper", "customer", 
"customer", "customer", "internet", "internet", "internet", "line", 
"line", "line", "price", "price", "price", "reliable", "reliable", 
"reliable", "service", "service", "service", "speed", "speed", 
"speed"), word_cust_props = c(0.395536562203229, 0.36372269705603, 
0.240740740740741, 0.486218302094818, 0.383682469680265, 0.130099228224917, 
0.361738148984199, 0.535553047404063, 0.102708803611738, 0.45317483328501, 
0.327051319222963, 0.219773847492027, 0.386215864759428, 0.360208062418726, 
0.253576072821847, 0.403946002076843, 0.407061266874351, 0.188992731048806, 
0.367229608336328, 0.435501257635645, 0.197269134028027, 0.178432893716059, 
0.245151280062064, 0.576415826221877, 0.315767973856209, 0.251429738562091, 
0.432802287581699, 0.383040935672515, 0.333333333333333, 0.283625730994152 
), NPS_Level = c("Detractor", "Passive", "Promoter", "Detractor", 
"Passive", "Promoter", "Detractor", "Passive", "Promoter", "Detractor", 
"Passive", "Promoter", "Detractor", "Passive", "Promoter", "Detractor", 
"Passive", "Promoter", "Detractor", "Passive", "Promoter", "Detractor", 
"Passive", "Promoter", "Detractor", "Passive", "Promoter", "Detractor", 
"Passive", "Promoter")), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -30L), vars = list(word, NPS_Level), drop = TRUE, .Names = c("word", 
"word_cust_props", "NPS_Level"), indices = list(0L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
    29L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    word = c("broadband", "broadband", "broadband", "call", "call", 
    "call", "cheaper", "cheaper", "cheaper", "customer", "customer", 
    "customer", "internet", "internet", "internet", "line", "line", 
    "line", "price", "price", "price", "reliable", "reliable", 
    "reliable", "service", "service", "service", "speed", "speed", 
    "speed"), NPS_Level = c("Detractor", "Passive", "Promoter", 
    "Detractor", "Passive", "Promoter", "Detractor", "Passive", 
    "Promoter", "Detractor", "Passive", "Promoter", "Detractor", 
    "Passive", "Promoter", "Detractor", "Passive", "Promoter", 
    "Detractor", "Passive", "Promoter", "Detractor", "Passive", 
    "Promoter", "Detractor", "Passive", "Promoter", "Detractor", 
    "Passive", "Promoter")), class = "data.frame", row.names = c(NA, 
-30L), vars = list(word, NPS_Level), drop = TRUE, .Names = c("word", 
"NPS_Level"))) 

這是我曾嘗試:

words_without_sw%>% 

    mutate(position = cumsum(word_cust_props) - 0.5*word_cust_props)%>% 
    ggplot(aes(word, word_cust_props,fill=NPS_Level)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    geom_text(aes(label = sprintf("%1.2f%%", 100*word_cust_props), y = position),colour = "white")+ 
    theme(plot.background = element_rect(fill = 'white'), 
     legend.position = "top", 
     axis.text.y=element_text(size=14, color="#6400aa",face = "bold"), 
     axis.text.x=element_text(size=12,color="#6400aa",face = "bold"), 
     plot.title = element_text(color="#6300aa", face="bold", size=20, hjust=0), 
     strip.text.x = element_text(size = 17, face = "bold",colour = "blue"), 
     legend.text=element_text(size=17,face = "bold"), 
     legend.title = element_text(size=17,face = "bold")) + 
    labs(x = "", 
     y = "word proportions by customer type", 
     title = "Top 10 words by customer type", 
     subtitle = "Calc. by taking the word count and proportions by customer type", 
     fill="")+ 
    scale_fill_manual(labels = c("Detractor","Passive","Promoter"), values = c("#E60014", "#333333","#14AA37")) 

但唱片公司仍混雜在一起。像這樣:enter image description here

不知道我在做什麼錯?

+0

嘗試用'geom_text(AES(標記= sprintf的( 「%1.2F %%」 更換您'geom_text'術語,100 * word_cust_props)),顏色= 「白色」,位置= position_stack(hjust = 0.5 )' –

+0

@ArewrewGustar'在position_stack(hjust = 0.5)時出錯:未使用的參數(hjust = 0.5)' – Shery

+0

用'vjust'代替'hjust'試試 –

回答

1
words_without_sw%>% 
ggplot(aes(word, word_cust_props,fill=NPS_Level,label = sprintf("%1.2f%%", 100*word_cust_props))) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    geom_text(size = 3, position = position_stack(vjust = 0.5),colour = "white")+ 
    theme(plot.background = element_rect(fill = 'white'), 
     legend.position = "top", 
     axis.title.y = element_blank(), 
     axis.title.x = element_text(color="#6300aa", face="bold", size=14), 
     axis.text.y=element_text(size=14, color="#6400aa",face = "bold"), 
     axis.text.x=element_blank(), 
     plot.title = element_text(color="#6300aa", face="bold", size=20, hjust=0), 
     strip.text.x = element_text(size = 17, face = "bold",colour = "blue"), 
     legend.text=element_text(size=17,face = "bold"), 
     legend.title = element_text(size=17,face = "bold")) + 
    labs(x = "", 
     y = "", 
     title = "Top 10 words by customer type", 
     subtitle = "Calc. by taking the word count and proportions by customer type", 
     fill="")+ 
    scale_fill_manual(labels = c("Detractor","Passive","Promoter"), values = c("#E60014", "#333333","#14AA37")) 
相關問題