[英]Creating squares with ggplot (what is the height of my y-axis when using categorical variable in y-scale?)
我有這樣的數據:
class date color
<chr> <date> <chr>
1 two 2021-01-21 #00FF00
2 two 2021-02-01 #83FF83
3 two 2021-02-11 #CDFFCD
4 two 2021-02-21 #7BFF7B
5 two 2021-03-03 #94FF94
6 two 2021-03-13 #F6FFF6
7 two 2021-03-23 #EEFFEE
8 two 2021-04-02 #C5FFC5
9 two 2021-04-12 #8BFF8B
10 two 2021-04-22 #F6FFF6
11 one 2021-01-21 #00FF00
12 one 2021-02-01 #83FF83
13 one 2021-02-11 #CDFFCD
14 one 2021-02-21 #7BFF7B
15 one 2021-03-03 #94FF94
16 one 2021-03-13 #F6FFF6
17 one 2021-03-23 #EEFFEE
18 one 2021-04-02 #C5FFC5
19 one 2021-04-12 #8BFF8B
20 one 2021-04-22 #EEFFEE
您可以使用此 dput 重現它:
structure(list(class = c("two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one"), date = structure(c(18648,
18659, 18669, 18679, 18689, 18699, 18709, 18719, 18729, 18739,
18648, 18659, 18669, 18679, 18689, 18699, 18709, 18719, 18729,
18739), class = "Date"), color = c("#00FF00", "#83FF83", "#CDFFCD",
"#7BFF7B", "#94FF94", "#F6FFF6", "#EEFFEE", "#C5FFC5", "#8BFF8B",
"#F6FFF6", "#00FF00", "#83FF83", "#CDFFCD", "#7BFF7B", "#94FF94",
"#F6FFF6", "#EEFFEE", "#C5FFC5", "#8BFF8B", "#EEFFEE")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), groups = structure(list(
class = c("one", "two"), .rows = structure(list(11:20, 1:10), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
我想要創建的是類似“github”式的外觀。 所以我想在整個日期范圍內創建方形圖塊。 對於每個 class(一個和兩個)。 日期范圍是相同的。
因此,例如,我希望該正方形具有高度 == 寬度,以便它填充 plot 的整個寬度。 所以我這樣做了:
ggplot() +
geom_tile(data = df,
aes(x = date,
y = class,
fill = color),
show.legend = F) +
scale_fill_identity()
結果是這樣的:
我不明白y=class
究竟做了什么。 它將class
變量放在 y 軸的什么位置。 假設它使用 1 / 的范圍(不管有多少類別),我給它的高度為 0.5(什么單位?),如下所示:
ggplot() +
geom_tile(data = df,
aes(x = date,
y = class,
height=.5,
fill = color),
show.legend = F) +
scale_fill_identity()
結果是(更好,但不完全是我想要的)
所以我刪除了height=.5
並認為我應該使用coord_equal
參數來完成它。 我只是不知道怎么做。 只需去掉固定高度並添加一個coord_equal
,如下所示:
ggplot() +
geom_tile(data = df,
aes(x = date,
y = class,
fill = color),
show.legend = F) +
scale_fill_identity() +
coord_equal()
產生這個:(
因此,如果有人對如何完成這項工作有任何想法,我將不勝感激:)
回答 OP 問題的關鍵是修復 plot 的縱橫比。 OP 顯示的圖像的縱橫比可能與 RStudio 或控制台中的直接 output 或控制台(OP 正在使用的特定圖形設備的默認設置)相匹配。 要解決此問題,您需要更改圖形 output 的width=
和height=
或使用ggsave('filename.png', width=..., height=...)
類的內容保存文件,或者修復基於數據的比率。
您可以使用特定的寬度/高度數字並使用coord_fixed()
修復坐標,但我更喜歡以編程方式執行此操作以允許擴展任何數據。
根據 OP 共享的數據,該比率應近似為數據集中的觀察數(天數)和df$class
的唯一值數。 您可能還想調整geom_tile()
的一些元素並通過scale_*
函數和expand=
參數控制圖塊外部的面板空間。
這是代碼:
ggplot(df, aes(x=date, y=class, fill=color)) +
geom_tile(height=0.8, width=7) +
scale_fill_identity() +
scale_x_date(expand=expansion(mult=c(0.05))) +
scale_y_discrete(expand=expansion(mult=c(0.7))) +
coord_fixed(ratio = nrow(df)/length(unique(df$class)))
和 output:
如果您願意,可以使用ggsave(..., width=... and height=...)
修復 plot 周圍的空白區域。
請注意,我在這里或多或少地通過反復試驗得出了geom_tile()
的 select width=
和height=
值。 高度是有道理的(因為正如@teunbrand 指出的那樣,離散值被約 1 個單位分隔)。 對於寬度,我想因為日期可能被映射為 1 天 = 1 個單位,並且這里一個月有 4 個……每個塊大約是 7 天。 似乎效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.