我做了一個百分比直方圖(效果很好),但是當我試圖列印百分比的值時,Y軸的比例不再正確。
百分比直方圖的代碼(運行良好)如下:
data_impact_final %>%
ggplot(aes(x =) operator">= time_to_treat_month))
geom_histogram(aes(y = (. .count..)/sum(. .count. ))。 binwidth=6)
scale_y_continuous(labels =scales::percent)
然而,當我試圖用stat_bin列印圖表上的百分比時,Y軸的比例不再正確。下面是我用來列印百分比的代碼:
data_impact_final %>%
ggplot(aes(x =) operator">= time_to_treat_month))
geom_histogram(aes(y = (. .count..)/sum(. .count. ))。 binwidth=6)
stat_bin(binwidth=6。 geom='text'/span>。 color='white', aes(label =scales: :百分比((. .count. )/sum(. .count. )))。 position=position_stack(vjust = 0。 5))
scale_y_continuous(labels =scales::percent)
謝謝你的幫助
uj5u.com熱心網友回復:
問題是,標簽被放置在 使用
標籤: 上一篇:PHPEchoipv4和Ipv6
y=...count...。為了解決你的問題,在stat_bin中也使用y=.count./sum(.count.)/code>。
ggplot2::mpg作為示例資料:library(ggplot2)
library(dplyr)
mpg %>%。
ggplot(aes(x =) 運算子">=hwy))
geom_histogram(aes(y = (. .count..)/sum(. .count. ))。 binwidth=6)
scale_y_continuous(labels =scales::percent)

mpg %>%
ggplot(aes(x =) 運算子">=hwy))
geom_histogram(aes(y = (. .count..)/sum(. .count. ))。 binwidth=6)
stat_bin(binwidth=6。 geom='text'/span>。 color='white', aes(y = . .count./sum(.count. ),標簽=尺度。 :百分比((. .count. )/sum(. .count. )))。 position=position_stack(vjust = 0。 5))
scale_y_continuous(labels =scales::percent)

