如何向 x 軸(圖表左側)上的條形組添加標簽?標記整個軸很容易,或者允許根據資料生成標簽,但是如果有意義的話,我無法弄清楚如何標記每組條形。
我知道我可以將專案資料重新編碼成完整的句子,但這與對 ggplot 代碼進行一些更改相比似乎不太優雅。
我曾嘗試使用此站點上類似問題的代碼(
ETA something for the millionth time - I figured out what works for me, which is using
ggtitle("How acceptable are each of the following scenarios to you?")
coord_flip()
geom_bar(position = "dodge", stat = "identity")
# facet_wrap(~item, scales = "free_x") # changed
scale_fill_manual(values=cbPalette)
# scale_fill_grey(start = 0.8, end = 0.2)
ylab("1 = highly unacceptable, 7 = highly acceptable")
xlab("")
theme_bw()
#theme(legend.position="bottom")
scale_x_discrete(breaks=c("none", "low", "moderatelow", "moderate", "greatest"),
labels=c("Control", "Treat 1", "Treat 2", "slkdj", "adkljf"))
labs(fill="reg")
Thank you so much to those of you who commented! Your help led me to the answer.
ETA - okay here I am, back again. It was pointed out to me by @Gregor Thomas that my scale limits are set incorrectly, along with the unnecessary nature of some of my code. This feedback is much appreciated. Using the guidance of commentors, I was able to resolve the labeling issue that existed.
But, now I cannot figure out how to adjust the limits of the axis in the new code format. Given the following, how can I set the scale from 1-7 to reflect the nature of the likert scale people responded to? See code below.
ggplot(mydata, aes(y = item, x = mean, fill = reg))
geom_col(position = "dodge")
scale_fill_manual(values = cbPalette)
scale_y_discrete(breaks=c("none", "low", "moderatelow", "moderate", "greatest"),
labels=c("No wolves", "Very low numbers of wolves", "Moderately low numbers of wolves", "Moderate numbers of wolves", "Greatest numbers of wolves that can be sustained")
)
scale_x_continuous(expand = expansion(mult = c(0, .05)))
labs(
title = "How acceptable are each of the following scenarios to you?",
x = "1 = highly unacceptable, 7 = highly acceptable",
y = "",
fill = "population"
)
theme_bw()
theme(
legend.position = "bottom",
panel.grid.major.y = element_blank()
)
uj5u.com熱心網友回復:
這是我清理代碼的方法。我跳過coord_flip,只是根據需要映射 x 和 y 變數。我將所有標簽合并到 中labs(),并scale_y_discrete(labels = )用于標簽。
my_labels = rev(paste("Scenario", LETTERS[1:5]))
ggplot(mydata, aes(y = item, x = mean, fill = reg))
geom_col(position = "dodge")
scale_fill_manual(values = cbPalette)
scale_y_discrete(
labels = my_labels
)
labs(
title = "How acceptable are each of the following scenarios to you?",
x = "1 = highly unacceptable, 7 = highly acceptable",
y = "",
fill = "population"
)
theme_bw()
theme(legend.position = "bottom")

如果這是我的情節,我會調整 x 比例以洗掉 0 以下的填充,并且我會洗掉 y 網格線,如下所示:
ggplot(mydata, aes(y = item, x = mean, fill = reg))
geom_col(position = "dodge")
scale_fill_manual(values = cbPalette)
scale_y_discrete(
labels = my_labels
)
scale_x_continuous(expand = expansion(mult = c(0, .05)))
labs(
title = "How acceptable are each of the following scenarios to you?",
x = "1 = highly unacceptable, 7 = highly acceptable",
y = "",
fill = "population"
)
theme_bw()
theme(
legend.position = "bottom",
panel.grid.major.y = element_blank()
)

雖然如果 1 是“非常不可接受的”,我不知道如何解釋 0 ......整個 x 比例似乎令人困惑。也許您應該將 x 限制設定為從 1 到 7,而不是從 0 到最大資料(即 5)?如果是這樣,請使用scale_x_continuous(expand = expansion(mult = c(0, .05)), limits = c(1, 7)).
uj5u.com熱心網友回復:
我們可以添加 facet_grid(item ~ ., scales="free_y", space="free_y", switch="y")
ggplot(mydata, aes(x = item, y = mean, fill = reg))
ggtitle("How acceptable are each of the following scenarios to you?")
coord_flip()
geom_bar(position = "dodge", stat = "identity")
# facet_wrap(~item, scales = "free_x") # changed
scale_fill_manual(values=cbPalette)
# scale_fill_grey(start = 0.8, end = 0.2)
ylab("1 = highly unacceptable, 7 = highly acceptable")
xlab("")
theme_bw()
theme(legend.position="bottom")
scale_x_discrete(breaks = NULL)
labs(fill="population")
facet_grid(item ~ ., scales="free_y", space="free_y", switch="y")
guides(fill=FALSE)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/404921.html
標籤:
