我正在尋求有關 ggplot facetwraps 的幫助,以生成一個圖形,其中條形圖在每個刻面內居中,并且刻面的 x 軸上沒有標簽(因為它們已在上面標記)。
您將如何實作這一目標?
一、生成資料框
act <- c("Wildlife viewing", "Wildlife viewing", "Wildlife viewing",
"Birdwatching", "Birdwatching", "Birdwatching",
"Wildlife photography", "Wildlife photography", "Wildlife photography",
"Deer hunting", "Deer hunting", "Deer hunting",
"Trapping", "Trapping", "Trapping",
"Fishing", "Fishing", "Fishing")
ppart <- c(65.87091, 60.64611, 66.12500,
37.66578, 35.97651, 41.75000,
29.00088, 26.28488, 30.50000,
53.40407, 28.92805, 42.37500,
9.018568, 3.524229, 5.250000,
63.21839, 44.19971, 57.62500)
Region <- c("UP", "SL", "NL",
"UP", "SL", "NL",
"UP", "SL", "NL",
"UP", "SL", "NL",
"UP", "SL", "NL",
"UP", "SL", "NL")
df <- data.frame(act, ppart, Region) # create a df with % participation in various activities
然后,創建圖形。
ggplot(df, aes(x = act, y = ppart, fill = Region))
geom_bar(position = "dodge", stat = "identity")
facet_wrap(~act)
scale_fill_grey(start = 0.8, end = 0.2)
ylab("Percent participation in activity")
xlab("")
theme_bw()

正如你所看到的,當我使用上面的代碼生成圖表時,事情有點模糊,并且間距和標簽存在明顯的問題。我該如何解決這些問題?
具體來說,將每個構面內的條形圖居中并洗掉構面內的 x 軸標簽。請指教!謝謝你。
uj5u.com熱心網友回復:
我認為規范的方法是scales = "free_x"在構面中使用,并用breaks=NULL.顯式洗掉 x 軸刻度。
ggplot(df, aes(x = act, y = ppart, fill = Region))
geom_bar(position = "dodge", stat = "identity")
facet_wrap(~act, scales = "free_x") # changed
scale_fill_grey(start = 0.8, end = 0.2)
ylab("Percent participation in activity")
xlab("")
theme_bw()
scale_x_discrete(breaks = NULL) # new

uj5u.com熱心網友回復:
這就是你所追求的嗎?
ggplot(df, aes(x = 0, y = ppart, fill = Region))
geom_bar(position = "dodge", stat = "identity")
facet_wrap(~act)
scale_fill_grey(start = 0.8, end = 0.2)
ylab("Percent participation in activity")
xlab("")
theme_bw()
theme(
axis.text.x = element_blank()
)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407008.html
標籤:
上一篇:圍繞軸標簽添加圓圈
下一篇:ggplot2沒有繪制所有點
