简体   繁体   English

R:ggplot彩色条形图按符号? 绿色 = 正极,红色 = 负极

[英]R: ggplot color bar graph by sign? Green = positive, Red = negative

I am looking to adjust my chart so that the returns below zero are in Red and the positive are green.我正在调整我的图表,使零以下的回报为红色,正数为绿色。 I put a comment next to the two lines I have been messing around with (the problem is in the last set of lines of code. Everything else was working fine beforehand.我在我一直在搞乱的两行旁边放了一条评论(问题出在最后一行代码中。其他一切都事先工作正常。

library(tidyverse)
library(tidyquant)
library(shiny)

#Stock assignments
stock_1<-'AMZN'
stock_2<-'CMG'
stock_3<-'TSS'  
stock_4<-'FB'
stock_5<-'T'

#Creating Input Boxes for Stocks and Weights of the Portfolio
ui <- fluidPage(
  titlePanel('Portfolio Daily Returns'),
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(6,textInput("S1",h3('Input Stock 1'),stock_1)),
        column(6,numericInput("w1",h3("Input Weight 1"),.25))
      ),
      fluidRow(
        column(6,textInput("S2",h3('Input Stock 2'),stock_2)),
        column(6,numericInput("w2",h3("Input Weight 2"),.25))
      ),
      fluidRow(
        column(6,textInput("S3",h3('Input Stock 3'),stock_3)),
        column(6,numericInput("w3",h3("Input Weight 3"),.2))
      ),
      fluidRow(
        column(6,textInput("S4",h3('Input Stock 4'),stock_4)),
        column(6,numericInput("w4",h3("Input Weight 4"),.2))
      ),
      fluidRow(
        column(6,textInput("S5",h3('Input Stock 5'),stock_5)),
        column(6,numericInput("w5",h3("Input Weight 5"),.1))
      ),
      dateRangeInput("D",h3("Input Start and End Dates"),
                     '2019-09-01',Sys.Date())
    ),
    mainPanel(plotOutput("plot"))
  )
)
server <- function(input,output){
  dataInput <- reactive({
    tq_get(c(input$S1,input$S2,input$S3,input$S4,input$S5),from=input$D[1],to=input$D[2])%>%
      group_by(symbol)%>%
      tq_transmute(select=volume,mutate_fun = periodReturn,period='daily',
                   col_rename = 'Daily_Vol')%>%
      tq_portfolio(assets_col=symbol,returns_col = Daily_Vol,weights=c(
        input$w1,input$w2,input$w3,input$w4,input$w5),col_rename = 'Port_Vol')
  })

  dataInput[["sign"]] = ifelse(dataInput[["value"]] >= 0, "positive", "negative") #Problem
  output$plot <- renderPlot({
    ggplot(dataInput(),aes(x=date,y=Port_Vol))+geom_col()+
      scale_fill_manual(values = c("positive"="green","negative"=red)) #Problem 2
  })
}

shinyApp(ui=ui,server=server)```

Problem 1: dataInput is a reactive object.问题 1:dataInput 是一个反应式 object。 You should always call it out with dataInput(), and you cannot alter it.您应该始终使用 dataInput() 调用它,并且不能更改它。 Either you mutate it while plotting or you simply add on to the data.frame before returning.要么在绘图时对其进行变异,要么在返回之前简单地添加到 data.frame 中。 So I added the positive /negative inside the dataInput call所以我在 dataInput 调用中添加了正/负

Problem 2: you need to quote "red".问题2:您需要引用“红色”。 Also you need to specify fill=sign in the aes您还需要在 aes 中指定 fill=sign

Below I have edited your server function, it should work, there is some warning messages about NA, and I am not so familiar with the zoo functions, so I leave you to sort it out下面我编辑了你的服务器function,应该可以了,有一些关于NA的警告信息,我对动物园功能不是很熟悉,所以留给你整理

server <- function(input,output){
  dataInput <- reactive({
    dat <- tq_get(c(input$S1,input$S2,input$S3,input$S4,input$S5),from=input$D[1],to=input$D[2])%>%
      group_by(symbol)%>%
      tq_transmute(select=volume,mutate_fun = periodReturn,period='daily',
                   col_rename = 'Daily_Vol')%>%
      tq_portfolio(assets_col=symbol,returns_col = Daily_Vol,weights=c(
        input$w1,input$w2,input$w3,input$w4,input$w5),col_rename = 'Port_Vol')
    dat$sign = ifelse(dat$Port_Vol >= 0, "positive", "negative")
    return(dat)
  })

  output$plot <- renderPlot({
    ggplot(dataInput(),aes(x=date,y=Port_Vol,fill=sign))+geom_col()+
      scale_fill_manual(values = c("positive"="green","negative"="red")) #Problem 2
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使所有正值条形图与ggplot中具有负值的条形图相同的颜色主题 - Make all positive value bar graph the same color theme as bar graph with negative values in ggplot xyplot时间序列,正值为绿色,负值为红色,R - xyplot time series with positive values in green, negative in red, in R R geom_bar 条件红绿表示正/负 - R geom_bar conditional red green for positive/minus ggplot - 如何将条形图设置为颜色渐变(绿色-&gt; 黄色-&gt; 红色) - ggplot - How to set the bar itself to be colored by a color gradient (green->yellow->red) 栅格颜色-正红色和负蓝色 - Raster color - positive red & negative blue 在 R 中使用带有负值的 ggplot 堆积条形图 - Stacked Bar Graph using ggplot with Negative Values in R 控制具有正值和负值的 ggplot::geom_bar position_stack 的图形和图例中的填充颜色顺序 - Control fill colour order in graph and legend for ggplot::geom_bar position_stack with positive and negative values 条形颜色由正值或负值区分的多面归一化条形图 - Multiple facet normalized bar graph with bar color differentiated by positive or negative value 在 R 中,我的线性模型的输出显示正相关,但我的 ggplot 图显示负相关? - In R, the output of my linear model shows a positive correlation but my ggplot graph indicates a negative correlation? 为正和负水平条形图添加颜色 - Add Color to Positive and Negative Horizontal Bar Chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM