我是一名大學生,我們必須使用 Rstudio 創建一些圖表來完成作業。我已經成功完成了第一項任務的核心目標,即使用提供的資料創建一個簡單的條形圖;但是,我無法用所需的文本標記軸。
這是代碼:
library (ggplot2)
Country = as.factor(c("slovakia", "iceland", "lithuania"))
Waste = c(2.0, 3.7, 2.1, 2.3, 1.7, 2.5)
Year = as.factor(c("2004", "2018"))
data = data.frame(Country, Waste, Year)
ggplot(data,
aes(x = Country,
y = Waste,
fill = Year))
geom_bar(stat = "identity",
position = "dodge")
xlab("Country")
ylab("Tons of waste per capita")
這是輸出:

但我希望軸標簽有這個特定的文本(用 Word 制作):

請注意,我們被特別要求使用ggplot.
uj5u.com熱心網友回復:
使用ggplot2,您必須 在代碼行的末尾使用以將其他圖層添加到您的繪圖中。如果不是,那么 R 只會將這些代碼行作為獨立行讀取,這將引發錯誤。geom_bar因此,在您的代碼中, and末尾缺少符號xlab,這意味著 x 和 y 標簽線永遠不會添加到您的繪圖中。
library(ggplot2)
ggplot(data,
aes(x = Country,
y = Waste,
fill = Year))
geom_bar(stat = "identity",
position = "dodge")
xlab("Country")
ylab("Tons of waste per capita")
輸出

資料
data <- structure(list(Country = structure(c(3L, 1L, 2L, 3L, 1L, 2L), class = "factor", levels = c("iceland",
"lithuania", "slovakia")), Waste = c(2, 3.7, 2.1, 2.3, 1.7, 2.5
), Year = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", levels = c("2004",
"2018"))), class = "data.frame", row.names = c(NA, -6L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471579.html
上一篇:跨多個方面注釋ggplot2
下一篇:在R中繪制分組日期值時出現問題
