[英]In Highchart in R how to change colors by negative and positive values in a bar chart
[英]Assign colors to negative and positive values in R barplot
我正在嘗試復制氣候指數(特別是 NPO)的時間序列條形圖,其中正值和負值有不同的 colors。
我使用的是不同的索引,所以值不同但概念是相同的。 數據在這里(我不知道如何鏈接您可以導入的數據集?很抱歉給您帶來不便)
我嘗試過 ggplot 和 zoo package 來重構數據:
library(tidyverse)
library(here)
library(zoo)
NPGO <- read_csv(here('data//NPGOindex_toJuly2020.csv'))
NPGO <- rename(NPGO, index=`NPGO index`)
glimpse(NPGO)
NPGO$DATE <- as.yearmon(paste(NPGO$YEAR, NPGO$MONTH), '%Y %m')
NPGO$color <- ifelse(NPGO$index<0, 'negative','positive')
ggplot(NPGO, aes(x=DATE, y=index)) +
geom_bar(stat='identity',
width=0.8, aes(fill=color)) +
scale_x_yearmon(format='%m %Y', expand=c(0,0)) +
scale_fill_manual(values=c(positive='red',negative='blue')) +
geom_line(aes(y=0), color='black') + theme_bw()
base barplot()
產生更多我正在尋找的東西,我嘗試使用看似回答了我的問題的代碼,但沒有骰子:
barplot(height=NPGO$index,
col=ifelse(NPGO$index>0,'red','blue'))
任何幫助將不勝感激,謝謝!
由於黑色border
,它們似乎不可見,並且因為它們很多,只需將它們關閉即可。
barplot(height=NPGO$index, col=ifelse(NPGO$index > 0, 2, 4), border=NA)
數據:
d <- read.csv("http://www.o3d.org/npgo/npgo.php", skip=29)[-(848:850),]
NPGO <- as.data.frame(`colnames<-`(type.convert(do.call(rbind, strsplit(d, "\\s+"))[,-1]),
c("year", "month", "index")))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.