我的資料框
> dput(wt)
structure(list(Teams = structure(1:21, .Label = c("Arsenal",
"Aston Villa", "Bournemouth", "Brighton", "Burnley", "Chelsea",
"Crystal Palace", "Draw", "Everton", "Leicester", "Liverpool",
"Man City", "Man United", "Newcastle", "Norwich", "Sheffield United",
"Southampton", "Tottenham", "Watford", "West Ham", "Wolves"), class = "factor"),
`Total Number Of Wins` = c(6L, 6L, 5L, 6L, 7L, 11L, 7L, 50L,
7L, 14L, 18L, 14L, 8L,7L, 3L, 7L, 7L, 8L, 4L, 6L, 7L)), class = "data.frame",
row.names = c(NA, -21L))
我的餅ggplot碼
pie = ggplot(wt, aes(x="", y="Total Number of Wins", fill="teams")) geom_bar(stat='identity',width = 1)
pie = pie coord_polar("y", start=0) geom_text(aes(label = paste0(round(value*1), "%")), position = position_stack(vjust = 0.5))
我得到的錯誤是:
paste0(round(value * 1), "%") 中的錯誤:找不到物件“值”。
我應該用什么替換“價值”?有人可以啟發我。
uj5u.com熱心網友回復:
我不確定您要在這里實作什么,但它看起來像是因為您在代碼中使用的“值”沒有分配變數。我基本上加載了您擁有的資料并保存了一個變數以使其作業:
value <- wt$`Total Number Of Wins`
然后使用您使用的相同繪圖腳本列印繪圖:
pie = ggplot(wt,
aes(x="",
y="Total Number of Wins",
fill="teams"))
geom_bar(stat='identity',
width = 1)
pie = pie
coord_polar("y",
start=0)
geom_text(aes(label = paste0(round(value*1),
"%")),
position = position_stack(vjust = 0.5))
這給了我這個:
![這個錯誤是什么意思[R Programming]?](https://img.uj5u.com/2022/01/21/5e5acacaf1044ba5994907cbd4ea3aab.png)
我認為你的意思是對它進行顏色編碼,你只需要再做一個小編輯。R 區分大小寫,因此您需要將原始餅圖代碼中的“Teams”變數大寫:
pie = ggplot(wt,
aes(x="",
y="Total Number of Wins",
fill=Teams)) # capitalized variable w/o quotes
geom_bar(stat='identity',
width = 1)
如果你現在運行兩個情節代碼,它會給你這個:
![這個錯誤是什么意思[R Programming]?](https://img.uj5u.com/2022/01/21/7d475c708d4c4a1b912f8c8aa5160a54.png)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416786.html
標籤:
