2017-08-17 36 views
0

我試圖以圖形方式表示每個這些點的「熱」數據,即每個位置的單變量整數。如何繪製ggplot中的「​​交錯」點的熱圖

的形象是:

enter image description here

我沒有數據,但是我會像這樣,其中,同意與數據獲取,我搖搖晃晃在x軸的位置幫助我在空間上知道我正在看的地方(其中x和y實際上是同時在線的T是溫度)。

x y T 
1 1 5 
3 1 5 
5 1 6 
7 1 5 
9 1 6 
11 1 7 
2 2 7 
4 2 5 
6 2 4 
8 2 5 
10 2 6 
1 3 7 
3 3 8 
5 3 8 
7 3 7 
9 3 8 
11 3 9 
2 4 9 
4 4 13 
6 4 13 
8 4 9 
10 4 9 

我怎樣才能最好地使用ggplot(或類似的工具)在視覺上代表這張熱圖?我只是想要有空格的盒子(例如(1,2)不存在),但是項目團隊並不需要這些!

我不在乎如果點是圓的,方形/矩形很好。

在此先感謝,希望我的問題很明確。

+0

如果項目團隊不希望空格,你想要什麼?請詳細說明所需的輸出,以便回答問題。具體要求是什麼? – MrFlick

+0

在圖像中,由於容器是圓形的,堆疊時它們自然交錯,所以我想表示那個交錯的位置。我有每個位置的數據點(不僅僅是標記的,如果這是令人困惑的道歉) – SNOYL

+0

但情節怎麼知道哪些值丟失與哪些值不存在? x和y的「範圍」是什麼? – MrFlick

回答

1

我將更新@ TTNK的答案使用圓形點,像你的桶,並以固定的座標匹配圓形填料,而不是正方形。

ggplot(df, aes(x, y, fill = Temp)) + 
    geom_point(shape = 21, size = 21, stroke = 3) + 
    scale_y_continuous(expand = c(0.25,0)) + 
    scale_x_continuous(breaks = 1:11, expand = c(0.25,0)) + 
    coord_fixed(ratio = tan(pi/3)) + 
    theme_classic() 

enter image description here

雖然這讓你更接近你的電路圖,但你必須擺弄size =stroke =的論點,讓你的桶觸摸。如果您希望是自動的,並在合適的寬高比,但不關心一樣關於圓度,去geom_hex

ggplot(df, aes(x, y, fill = Temp)) + 
    geom_hex(stat = "identity", colour = "white") + 
    scale_y_continuous(expand = c(0.25,0)) + 
    scale_x_continuous(breaks = 1:11, expand = c(0.25,0)) + 
    coord_fixed(ratio = tan(pi/3)) + 
    theme_classic() 

enter image description here

+2

coord_fixed(ratio = tan( pi/3))。絕對藏起來,以備將來使用! 我認爲我的方式一半在那裏,但我的三角太生鏽了。 :) – TTNK

+0

@TTNK,我總是要畫一個三角形來勾畫勾股定理並得到實際的數字。 – Brian

2

也許這樣的事?使用從package::ggplot2geom_point功能(包括在tidyverse,有一個超大的字形:

library(tidyverse) 

# Generating a data frame with alternating odds and evens 
# Probably overwrought but makes the example cleaner 

> df <- data.frame(x = rep(c(seq(1, 11, by = 2), seq(2, 10, by = 2)), 2), 
       y = rep(1:4, times = c(6, 5, 6, 5)), 
       'T' = sample(5:13, 22, replace = TRUE)) 

> head(df, 10) 
    x y T 
1 1 1 10 
2 3 1 12 
3 5 1 8 
4 7 1 10 
5 9 1 12 
6 11 1 12 
7 2 2 9 
8 4 2 9 
9 6 2 6 
10 8 2 10 

> df %>% ggplot(aes(x, y)) + 
    geom_point(aes(fill = T), size = 20, shape = 23) + 
    ylim(0, 5) + 
    xlim(0, 12) + 
    coord_fixed(ratio = 1) + 
    theme_minimal() 

enter image description here