既然你沒有提供一個完整的可重複的例子(或你已經嘗試過的任何代碼),我做了一些假的數據顯示,你會如何做。
library(dplyr)
library(ggplot2)
fake.df <-
expand.grid(Country = c("US", "Canada", "Mexico"),
segments = LETTERS[1:5],
variable = c("Kerosene", "Petrol",
"Edible Oil", "LPG")) %>%
mutate(value = runif(length(Country), -20, 50))
fake.df.adj <-
fake.df %>%
mutate(xadj = ifelse(variable %in% c("Kerosene", "Edible Oil"), -0.33/2, 0.33/2),
yadj = ifelse(variable %in% c("Kerosene", "Petrol"), 0.33/2, -0.33/2),
xpos = as.numeric(factor(Country)) + xadj,
ypos = as.numeric(factor(segments)) + yadj)
mutate
代碼在您想要繪製的每個中心點周圍創建一個調整框。對於「煤油」和「食用油」,這些瓷磚向左移動了每個Country
(因此每個瓷磚的中心爲-0.33/2)之間的空間的1/3。其他人正確轉移。與y調整一樣,但上下左右,而不是左右。
然後,將您的因素(Country
和segments
)轉換爲數字位置並將它們相加。
fake.df.adj
Country segments variable value xadj yadj xpos ypos
1 US A Kerosene -11.8318607 -0.165 0.165 0.835 1.165
2 Canada A Kerosene 39.1181835 -0.165 0.165 1.835 1.165
3 Mexico A Kerosene 25.9354644 -0.165 0.165 2.835 1.165
4 US B Kerosene -19.3503525 -0.165 0.165 0.835 2.165
5 Canada B Kerosene -12.4459506 -0.165 0.165 1.835 2.165
6 Mexico B Kerosene 9.9017016 -0.165 0.165 2.835 2.165
7 US C Kerosene 37.9767562 -0.165 0.165 0.835 3.165
8 Canada C Kerosene 3.4687111 -0.165 0.165 1.835 3.165
9 Mexico C Kerosene 0.6280239 -0.165 0.165 2.835 3.165
10 US D Kerosene 43.4739464 -0.165 0.165 0.835 4.165
11 Canada D Kerosene 8.4671510 -0.165 0.165 1.835 4.165
12 Mexico D Kerosene 19.7357609 -0.165 0.165 2.835 4.165
13 US E Kerosene -2.6120700 -0.165 0.165 0.835 5.165
14 Canada E Kerosene -10.6013792 -0.165 0.165 1.835 5.165
15 Mexico E Kerosene -6.2059698 -0.165 0.165 2.835 5.165
16 US A Petrol 17.1079974 0.165 0.165 1.165 1.165
17 Canada A Petrol 46.8944785 0.165 0.165 2.165 1.165
18 Mexico A Petrol 9.3977815 0.165 0.165 3.165 1.165
19 US B Petrol 38.7547489 0.165 0.165 1.165 2.165
20 Canada B Petrol -14.4210703 0.165 0.165 2.165 2.165
21 Mexico B Petrol 32.5160861 0.165 0.165 3.165 2.165
22 US C Petrol -1.3750645 0.165 0.165 1.165 3.165
23 Canada C Petrol 17.0540527 0.165 0.165 2.165 3.165
24 Mexico C Petrol -16.8449931 0.165 0.165 3.165 3.165
25 US D Petrol 33.8465349 0.165 0.165 1.165 4.165
26 Canada D Petrol 43.7369153 0.165 0.165 2.165 4.165
27 Mexico D Petrol 32.2145640 0.165 0.165 3.165 4.165
28 US E Petrol -13.1811532 0.165 0.165 1.165 5.165
29 Canada E Petrol 46.1913082 0.165 0.165 2.165 5.165
30 Mexico E Petrol -7.5030316 0.165 0.165 3.165 5.165
31 US A Edible Oil 36.1643957 -0.165 -0.165 0.835 0.835
32 Canada A Edible Oil 26.9887728 -0.165 -0.165 1.835 0.835
33 Mexico A Edible Oil 9.2825143 -0.165 -0.165 2.835 0.835
34 US B Edible Oil 32.7455557 -0.165 -0.165 0.835 1.835
35 Canada B Edible Oil 9.0447777 -0.165 -0.165 1.835 1.835
36 Mexico B Edible Oil 21.5822081 -0.165 -0.165 2.835 1.835
37 US C Edible Oil 2.3906442 -0.165 -0.165 0.835 2.835
38 Canada C Edible Oil 25.2536855 -0.165 -0.165 1.835 2.835
39 Mexico C Edible Oil 5.0618632 -0.165 -0.165 2.835 2.835
40 US D Edible Oil 41.0292115 -0.165 -0.165 0.835 3.835
41 Canada D Edible Oil 8.4515698 -0.165 -0.165 1.835 3.835
42 Mexico D Edible Oil 11.9080938 -0.165 -0.165 2.835 3.835
43 US E Edible Oil 42.2518838 -0.165 -0.165 0.835 4.835
44 Canada E Edible Oil 25.6458033 -0.165 -0.165 1.835 4.835
45 Mexico E Edible Oil -19.3037443 -0.165 -0.165 2.835 4.835
46 US A LPG 25.8891215 0.165 -0.165 1.165 0.835
47 Canada A LPG -3.1028641 0.165 -0.165 2.165 0.835
48 Mexico A LPG 47.5884154 0.165 -0.165 3.165 0.835
49 US B LPG 11.3000701 0.165 -0.165 1.165 1.835
50 Canada B LPG 26.8041755 0.165 -0.165 2.165 1.835
51 Mexico B LPG -13.0209453 0.165 -0.165 3.165 1.835
52 US C LPG -5.5292231 0.165 -0.165 1.165 2.835
53 Canada C LPG 46.3101034 0.165 -0.165 2.165 2.835
54 Mexico C LPG 19.7622448 0.165 -0.165 3.165 2.835
55 US D LPG 8.5258791 0.165 -0.165 1.165 3.835
56 Canada D LPG 20.5856857 0.165 -0.165 2.165 3.835
57 Mexico D LPG -10.7589733 0.165 -0.165 3.165 3.835
58 US E LPG 14.8577291 0.165 -0.165 1.165 4.835
59 Canada E LPG -6.9590007 0.165 -0.165 2.165 4.835
60 Mexico E LPG -3.1651872 0.165 -0.165 3.165 4.835
fake.df.adj %>%
ggplot(aes(xpos, ypos, fill = value)) +
geom_tile(height = 0.33, width = 0.33) +
geom_text(aes(label = paste0(round(value, 1), "%"),
color = value < 30), show.legend = F) +
geom_text(data = fake.df.adj %>% filter(Country == "Mexico", segments == "E"),
aes(label = variable,
y = ypos + 1.75*yadj),
fontface = "bold") +
scale_fill_gradient2(labels = function(breaks) paste0(breaks, "%")) +
scale_color_manual(values = c("FALSE" = "white", "TRUE" = "black")) +
scale_x_continuous(breaks = 1:3, name = NULL,
labels = levels(fake.df$Country)) +
scale_y_continuous(breaks = 1:5, name = "Segments",
labels = levels(fake.df$segments)) +
theme_classic()
現在你可以使用繪圖geom_tile
,設置瓷磚的高度和寬度爲0.33。
我在每個圖塊的頂部添加了標籤,四捨五入到小數點後一位,並帶有%符號。爲了使它們很好地適應高價值的黑暗背景填充,我將標籤的顏色美學作爲邏輯表達式,然後將這些值進一步向下映射爲黑色和白色。
然後,無法判斷哪種瓷磚中有哪四種variable
屬於哪個瓷磚,因此我通過將數據過濾到墨西哥和E以及稍微移動y位置來標記右上角的那些瓷磚比使用與之前相同的調整值的瓦片的中心更遠。
然後我添加了一個發散的填充調色板並用%符號標記了色彩條。
然後,您必須指定x和y刻度的標籤,因爲它們現在只是數字位置。
謝謝@布賴恩..感謝您的幫助,接受作爲答案:-) – Sam