[英]Creating bar plot with negative and positive value , using R
我試過重新創建你的數據,因為你沒有在你的問題中提供任何數據。 如果您的數據編碼為數字,這應該不是問題,如下所示。
#### Load Tidyverse ####
library(tidyverse)
#### Recreate Data ####
fuel_type <- c("DIESEL",
"DIESEL/ELECTRIC",
"LPG",
"PETROL",
"PETROL/ELECTRIC")
changes <- c(-1097.82,
92.96,
59.35,
-55.15,
88.04)
df <- data.frame(fuel_type,
changes)
#### Plot ####
df %>%
ggplot(aes(x=fuel_type,
y=changes))+
geom_col()
但是,您的changes
變量可能編碼錯誤。 在這里,我展示了相同的 plot,但將數據編碼為一個因子。 如果您的變量被編碼為字符,也會出現這種情況。
#### Coded as Factor ####
df %>%
ggplot(aes(x=fuel_type,
y=factor(changes)))+
geom_col()
要解決此問題,請更改數據框中數據的 class(此處我已將其保存為df2
:
df2 <- df %>%
mutate(changes = as.numeric(changes))
...或 plot 將它作為帶有as.numeric
的數字,就像原始df
數據框一樣:
df %>%
ggplot(aes(x=fuel_type,
y=as.numeric(changes)))+
geom_col()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.