我在 ggplot2 中創建了以下的翻轉條形圖(一個亮綠色的坐標翻轉分組條形圖)--其效果很好(代碼 1)。
然而,我試圖用不同的資料重新創建相同的繪圖結構,基本上是復制和調整第一個繪圖的代碼。一切都很好,除了顏色是灰色而不是比例填充手冊中所期望的綠色。老實說,我不明白為什么它在第一個情節中起作用而在第二個情節中不起作用。(代碼2)
代碼1(作業中的原始情節)
#LIBRARIES[/span
library (ggplot2)
library (dplyr)
library (data.table)
library (rdrop2)
library(tableHTML)
library (forcats)
df<- data.frame()
Komp = rep(c("Geschlecht"/span>。 "Alter", "Niveau")。 6),
groupW = c()
rep("1"。 3),
rep("2"/span>。 3),
rep("3"/span>。 3),
NA, NA, "4",
NA, NA, "5",
NA, NA, "6"/span
),
valuesW = c (45, 14。 9, 4.6, 54, 64. 9, 5.3, 1, 19. 8, 22. 2, NA, NA, 15. 4, NA, NA, 38。 2, NA, NA, 14。 2)
)
df%>%
ggplot( aes(x =) operator">= fct_rev(Komp)。 y = valuesW。 填充= fct_rev(groupW)))
geom_bar(位置= "dodge"。 stat = "identity", color = "#404040", show. legend = FALSE,.
#width = 1
)
#scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", paste("Durchschnitt", (Daten[toupper(input$Code) , "SP01_01"]-1), "-", (Daten[toupper(input$Code), "SP01_01"] 1), "Jahre" ), paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"] ) , Geschlecht)
xlab(")
ylab("Prozent")/span>
scale_fill_manual(values=rep( "#8DAE10"/span>。 6))
coord_flip() 。
#geom_hline(yintercept = 10, lty = 8, col = "Red")
geom_text()
aes (label = ifelse(is. na(valuesW)," , paste(valuesW, "%"))),
color = "black", hjust =-0。 35 , size = 4。 位置= position_dodge(width= 0。 9))
#scale_x_discrete(limit= rev(level(groupW)))
geom_text(aes(y=0。 標簽 = c(/span>)
"Weiblich", "10-13 Jahre"。 "Keine Ebene",
"Mxnnlich", "14-17 Jahre"。 "Bezirksebene",
"Div. ", "18-23 Jahre","Landesebene",
"", ""。 "landesxbergreifende Ebene" ,
"", "", "nationale Ebene", "nationale Ebene",
"","", "International Ebene")
),顏色="白色"。 hjust = 0。 大小= 5。 位置= position_dodge(width= 0。 9))
scale_y_continuous()
expand= c(0。 04,0),標簽=尺度。 :逗號,位置= "right"。
)
代碼2(一個灰色的情節,應該和第一個情節一樣是綠色的)
。#LIBRARIES。
library (ggplot2)
library (dplyr)
library (data.table)
library (rdrop2)
library(tableHTML)
library (forcats)
df<- data.frame()
Komp = rep(c("Smartphone"/span>。 "PC/Laptop"。 "平板電腦", "電視"。 "Spielkonsole"。 "Smart-Watch", "E-Book-Reader"。 "Andere")。 2),
groupW = c()
rep("ja"。 8),
rep("nein",/span> 8)
),
valuesW = c (100-0。 100-23. 9, 100-41. 3, 100-30。 9, 100-51. 5, 100-73。 2, 100-89. 8, 100-98. 9 ,0, 23。 9, 41.3, 30.9, 51. 5, 73.2, 89. 8, 98.9)。
)
df%>%
ggplot( aes(x =) operator">= fct_rev(Komp)。 y = valuesW。 填充= fct_rev(groupW)))
geom_bar(位置= "dodge"。 stat = "identity", color = "#8DAE10", show. legend = FALSE,.
#width = 1
)
#scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", paste("Durchschnitt", (Daten[toupper(input$Code) , "SP01_01"]-1), "-", (Daten[toupper(input$Code), "SP01_01"] 1), "Jahre" ), paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"] ) , Geschlecht)
xlab(")
ylab("Prozent")/span>
scale_fill_manual(values=rep( "#8DAE10"/span>。 16)。 限制 = c("Smartphone"。 "PC/Laptop"。 "平板電腦", "電視"。 "Spielkonsole", "Smart-Watch", "E-Book-Reader"。 "Andere"))
coord_flip() 。
#geom_hline(yintercept = 10, lty = 8, col = "Red")
geom_text()
aes (label = round(valuesW。 1)),
color = "black", hjust =-0。 35 , size = 4。 位置= position_dodge(width= 0。 9))
#scale_x_discrete(limit= rev(level(groupW)))
geom_text(aes(y=0。 標簽 = groupW
), color = "white"。 hjust = 0。 尺寸= 5。 位置= position_dodge(width= 0。 9))
scale_y_continuous()
expand= c(0。 04,0),標簽=尺度。 :逗號,位置= "right"。
)
有什么想法嗎,錯誤在哪里?
uj5u.com熱心網友回復:
如果你洗掉limits = c("Smartphone", "PC/Laptop", "Tablet", "TV", "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere")
它應該使所有的條形圖都變成想要的綠色。
library (ggplot2)
library (dplyr)
library (data.table)
library (rdrop2)
library(tableHTML)
library (forcats)
df<- data.frame()
Komp = rep(c("Smartphone"/span>。 "PC/Laptop"。 "平板電腦", "電視"。 "Spielkonsole"。 "Smart-Watch", "E-Book-Reader"。 "Andere")。 2),
groupW = c()
rep("ja"。 8),
rep("nein",/span> 8)
),
valuesW = c (100-0。 100-23. 9, 100-41. 3, 100-30。 9, 100-51. 5, 100-73。 2, 100-89. 8, 100-98. 9 ,0, 23。 9, 41.3, 30.9, 51. 5, 73.2, 89. 8, 98.9)。
)
df%>%
ggplot( aes(x =) operator">= fct_rev(Komp)。 y = valuesW。 填充= fct_rev(groupW)))
geom_bar(位置= "dodge"。 stat = "identity", color = "#8DAE10", show. legend = FALSE,.
#width = 1
)
#scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", paste("Durchschnitt", (Daten[toupper(input$Code) , "SP01_01"]-1), "-", (Daten[toupper(input$Code), "SP01_01"] 1), "Jahre" ), paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"] ) , Geschlecht)
xlab(")
ylab("Prozent")/span>
scale_fill_manual(values=rep( "#8DAE10"/span>。 16), )
coord_flip()
#geom_hline(yintercept = 10, lty = 8, col = "Red")
geom_text()
aes (label = round(valuesW。 1)),
color = "black", hjust =-0。 35 , size = 4。 位置= position_dodge(width= 0。 9))
#scale_x_discrete(limit= rev(level(groupW)))
geom_text(aes(y=0。 標簽 = groupW
), color = "white"。 hjust = 0。 尺寸= 5。 位置= position_dodge(width= 0。 9))
scale_y_continuous()
expand= c(0。 04,0),標簽=尺度。 :逗號,位置= "right"。
)
uj5u.com熱心網友回復:
你可以嘗試這些調整來 a) 實作綠條的顏色和 b) 簡化代碼
- 使用
geom_col而不是geom_bar與stat = "identity" 。
- 交換x和y值以及相關的刻度等,以避免使用coord_flip 。
- 在呼叫
scale_fill_manual時洗掉limits引數 。
- 將
Komp轉換為一個因子,以控制ggplot中數值的順序。
library(ggplot2)
library(forcats)
library(dplyr)
komp_cat < - c("Smartphone"/span>。 "PC/Laptop"。 "平板電腦", "電視"。 "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere")
df1<- data.frame()
Komp = rep(komp_cat, 2),
groupW = c()
rep("ja"。 8),
rep("nein"/span>。 8)),
valuesW = c (100-0。 100-23. 9, 100-41. 3, 100-30。 9, 100-51. 5, 100-73。 2, 100-89. 8, 100-98. 9 ,0, 23。 9, 41.3, 30.9, 51. 5, 73.2, 89. 8, 98.9)。
)
df1 %>%
mutate(Komp = factor(Komp, 水平= komp_cat)) %> %
ggplot( aes(x = valuesW。 y = fct_rev(Komp)/span>。 填充= rev(groupW)))
geom_col(位置= "dodge"。 顏色 = "#8DAE10", 顯示。 legend = FALSE)
xlab("Prozent")
ylab("")/span>
scale_fill_manual(values = rep("#8DAE10"/span>。 2))
geom_text(aes (label ) operator">= round(valuesW。 1)),
color = "black",
hjust = -0.35,
size = 4,
position = position_dodge(width = 0。 9))
geom_text(aes(x=0。 標簽 = groupW),
color = "white",
hjust = 0,
size = 5,
position = position_dodge(width = 0。 9))
scale_x_continuous(expand = c(0。 04, 0), 標簽= scale。 :逗號,位置= "底部")

創建于2021-09-22,由reprex包(v2.0.0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334039.html
標籤:
