我有以下df
df <- data.frame(x = c(rep(seq(1:3),3)), y = c(rep(1,3),rep(2,3),rep(3,3)),
l = c(rep("1 km",3),rep("10 km",3),rep("30 km",3)),
m = c(rep(c("urban","semi urban","rural"),3)),
p = c(2.3,2.2,3.3,3.3,3.5,3.9,3.6,3.5,6.6),
q = c("Cargo Bike","Diesel Van", "Electric Van", "Single Delivery AV", "Multiple Delivery AV","Cargo Bike","Diesel Van","Diesel Van", "Electric Van"))
我正在嘗試制作一個具有 3x3 區域(即總共 9 個框)的圖表。在每個區域中,我需要“p”的值和文本“q”。我想把盒子關上。
amaze <- ggplot(df, aes(x-0.5,y-0.5))
geom_text(aes(label = q, color = factor(q)), nudge_y = 0.1, check_overlap = TRUE, show.legend = FALSE)
geom_text(aes(label = p, colour = factor(q)), show.legend = FALSE)
geom_label(aes(label = l), nudge_x = -0.4,nudge_y = 0.4)
geom_label(aes(label = m), nudge_x = 0.35,nudge_y = -0.4)
xlab("Density of Deliveries") ylab("Distance from warehouse to first delivery")
amaze geom_vline(xintercept = c(1,2,3)) geom_hline(yintercept = c(1,2,3))
#theme(panel.border = element_rect(color = "black", size = 2))
scale_x_continuous(limits = c(0,3))
scale_y_continuous(limits = c(0,3)) theme_bw()
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank())
這就是我得到的:

我想洗掉 yaxis 上方和 x 軸右側和下方的所有空間(因此精確地有一個 3 x 3 的框)。 我怎樣才能做到這一點?
在此先感謝您的幫助!
uj5u.com熱心網友回復:
由于您需要每個平面三個網格,因此您應該只使用兩個網格yintercept(geom_vline出于美學原因,您可以保留它,但這不是必需的)。
此外,expand = c(0, 0)兩者都設定scale_x_continuous并scale_y_continuous有助于洗掉填充。
library(ggplot2)
ggplot(df, aes(x-0.5,y-0.5))
geom_text(aes(label = q, color = factor(q)), nudge_y = 0.1, check_overlap = TRUE, show.legend = FALSE)
geom_text(aes(label = p, colour = factor(q)), show.legend = FALSE)
geom_label(aes(label = l), nudge_x = -0.4,nudge_y = 0.4)
geom_label(aes(label = m), nudge_x = 0.35,nudge_y = -0.4)
xlab("Density of Deliveries")
ylab("Distance from warehouse to first delivery")
geom_vline(xintercept = c(1,2)) geom_hline(yintercept = c(1,2))
scale_x_continuous(limits = c(0,3), expand = c(0, 0))
scale_y_continuous(limits = c(0,3), expand = c(0, 0))
theme_bw()
theme(axis.ticks = element_blank(), axis.text.y = element_blank(),
axis.text.x = element_blank())

uj5u.com熱心網友回復:
您可以將expand引數添加到呼叫中scale_continuous
amaze geom_vline(xintercept = c(1,2,3)) geom_hline(yintercept = c(1,2,3))
#theme(panel.border = element_rect(color = "black", size = 2))
scale_x_continuous(limits = c(0,3), expand= c(0,0)) ### here
scale_y_continuous(limits = c(0,3), expand= c(0,0)) ### here
theme_bw()
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427660.html
上一篇:箱線圖問題:函式創建中的平線
下一篇:如何在我的條形圖中覆寫單個資料點
