0
我不明白爲什麼下面的代碼不顯示任何名稱爲我的情節的方面。如果任何人作爲一個暗示,將受到歡迎。在ggplot2上顯示方面名稱
data <- data.frame(id = rep(c("Large choice", "Low prices", "Service quality", "Product quality", "Convenience"), 4),
variable = rep(factor(c("OfficeStar", "Paper and Co", "Office Equipment", "Supermarket"),
levels = c("OfficeStar", "Paper and Co", "Office Equipment", "Supermarket")), each = 5),
value = sample(30, 20),
focus = as.factor(c(rep(1,5), rep(0, 15))));
nb_col <- 3
> head(data, 10)
id variable value focus
Large choice OfficeStar 5.2 1
Low prices OfficeStar 2.1 1
Service quality OfficeStar 4.2 1
Product quality OfficeStar 3.7 1
Convenience OfficeStar 2.7 1
Large choice Paper and Co 4.4 0
Low prices Paper and Co 4.5 0
Service quality Paper and Co 2.3 0
Product quality Paper and Co 2.6 0
Convenience Paper and Co 1.4 0
當我運行代碼:
ggplot(data = data, aes(x = variable, y = value, fill = focus)) +
geom_bar(stat = "identity") +
facet_wrap(~id, ncol = nb_col, drop = FALSE) +
scale_fill_manual(values = c("#77bb77", "#5599bb")) +
coord_flip() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
legend.position = "none")
我得到:
第一面應該在這裏命名爲大選擇的實例。 我還必須提到,數據表是通過用戶輸入獲得的,所以我不知道什麼樣的ID會看起來像,儘管我知道它們是字符串。
我試圖將級別應用於id,因爲沒有人,但它沒有工作,我試圖使用標籤功能,但沒有。
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] gridExtra_2.2.1 ggrepel_0.5 GMD_0.3.3 cluster_2.0.4 proto_0.3-10 lme4_1.1-12 Matrix_1.2-6 MASS_7.3-45 nnet_7.3-12
[10] sendmailR_1.2-1 RODBC_1.3-13 scales_0.4.0 ggplot2_2.1.0 gplots_3.0.1 stringr_1.1.0 digest_0.6.10 reshape2_1.4.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.6 magrittr_1.5 splines_3.3.1 munsell_0.4.3 lattice_0.20-33 colorspace_1.2-6 minqa_1.2.4 plyr_1.8.4
[9] caTools_1.17.1 tools_3.3.1 nlme_3.1-128 gtable_0.2.0 KernSmooth_2.23-15 gtools_3.5.0 nloptr_1.0.4 base64enc_0.1-3
[17] bitops_1.0-6 labeling_0.3 gdata_2.17.0 stringi_1.1.1
您對焦點有什麼疑問? –
哦,對不起,我沒有提供數據。修改版本(c(「大選擇」,「低價格」,「服務質量」,「產品質量」,「便利性」),4),變量= rep(因子(c 「OfficeStar」,「Paper and Co」,「Office Equipment」,「Supermarket」),levels = c(「OfficeStar」,「Paper and Co」,「Office Equipment」,「Supermarket」)) value = sample(30,20),focus = as.factor(c(rep(1,5),rep(0,15)))); nb_col < - 3'但是當我運行'ggplot'代碼時,我無法重現它。 – cuttlefish44
投票結束,因爲不可重複。 – Jaap